怎么在SpringBoot中整合ActiveMQ

這篇“怎么在SpringBoot中整合ActiveMQ”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“怎么在SpringBoot中整合ActiveMQ”文章吧。

堅守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都成都OPP膠袋小微創(chuàng)業(yè)公司專業(yè)提供成都定制網(wǎng)頁設(shè)計營銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺設(shè)計、底層架構(gòu)、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。

目錄結(jié)構(gòu)

怎么在SpringBoot中整合ActiveMQ

引入 maven依賴

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/> 
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

引入 application.yml配置

spring:
 activemq:
  broker-url: tcp://127.0.0.1:61616
  user: admin
  password: admin
queue: springboot-queue
server:
 port: 8080

創(chuàng)建QueueConfig

@Configuration
public class QueueConfig {
  @Value("${queue}")
  private String queue;

  @Bean
  public Queue logQueue() {
    return new ActiveMQQueue(queue);
  }

  @Bean
  public JmsTemplate jmsTemplate(ActiveMQConnectionFactory activeMQConnectionFactory, Queue queue) {
    JmsTemplate jmsTemplate = new JmsTemplate();
    jmsTemplate.setDeliveryMode(2);// 進(jìn)行持久化配置 1表示非持久化,2表示持久化</span>
    jmsTemplate.setConnectionFactory(activeMQConnectionFactory);
    jmsTemplate.setDefaultDestination(queue); // 此處可不設(shè)置默認(rèn),在發(fā)送消息時也可設(shè)置隊列
    jmsTemplate.setSessionAcknowledgeMode(4);// 客戶端簽收模式</span>
    return jmsTemplate;
  }

  // 定義一個消息監(jiān)聽器連接工廠,這里定義的是點對點模式的監(jiān)聽器連接工廠
  @Bean(name = "jmsQueueListener")
  public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory(
      ActiveMQConnectionFactory activeMQConnectionFactory) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(activeMQConnectionFactory);
    // 設(shè)置連接數(shù)
    factory.setConcurrency("1-10");
    // 重連間隔時間
    factory.setRecoveryInterval(1000L);
    factory.setSessionAcknowledgeMode(4);
    return factory;
  }
}

創(chuàng)建生產(chǎn)者:

@SpringBootApplication
@Component
@EnableScheduling
public class Producer {
  
  @Autowired
  private JmsMessagingTemplate jmsMessagingTemplate;
  
  @Autowired
  private Queue queue;
  
  @Scheduled(fixedDelay=3000)
  public void send() {
    String result = System.currentTimeMillis()+"---測試";
    System.out.println("result"+result);
    jmsMessagingTemplate.convertAndSend(queue,result);
  }
  public static void main(String[] args) {
    SpringApplication.run(Producer.class, args);
  }
}

創(chuàng)建消費(fèi)者的application.yml

spring:
 activemq:
  broker-url: tcp://127.0.0.1:61616
  user: admin
  password: admin
queue: springboot-queue
server:
 port: 8081

創(chuàng)建消費(fèi)者:

@Component
@SpringBootApplication
public class consumer {

  private int count =0;
  
  @JmsListener(destination = "${queue}")
  public void receive(TextMessage textMessage,Session session) throws JMSException {
    String text = textMessage.getText();
    
    System.out.println("消費(fèi):"+text+"第幾次獲取消息count:"+(++count));
    
    System.out.println();
    String jmsMessageID = textMessage.getJMSMessageID();
  }
 
  public static void main(String[] args) {
    SpringApplication.run(consumer.class,args);
  }
}

結(jié)果顯示:

怎么在SpringBoot中整合ActiveMQ

以上就是關(guān)于“怎么在SpringBoot中整合ActiveMQ”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章標(biāo)題:怎么在SpringBoot中整合ActiveMQ
當(dāng)前地址:http://muchs.cn/article0/jehcio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名定制網(wǎng)站、Google、網(wǎng)站設(shè)計公司、外貿(mào)網(wǎng)站建設(shè)移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司