欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合ActiveMQ過程解析

 更新時間:2019年09月23日 11:55:10   作者:小魚。。  
這篇文章主要介紹了SpringBoot整合ActiveMQ過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

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

引入 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è)置隊(duì)列
    jmsTemplate.setSessionAcknowledgeMode(4);// 客戶端簽收模式</span>
    return jmsTemplate;
  }

  // 定義一個消息監(jiān)聽器連接工廠,這里定義的是點(diǎn)對點(diǎ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é)果顯示:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 一文掌握Spring?Boot?日志文件

    一文掌握Spring?Boot?日志文件

    日志是程序的重要組成部分,日志對于我們來說,最主要的用途就是排除和定位問題,這篇文章主要介紹了Spring?Boot?日志文件,需要的朋友可以參考下
    2023-03-03
  • 關(guān)于JSqlparser使用攻略(高效的SQL解析工具)

    關(guān)于JSqlparser使用攻略(高效的SQL解析工具)

    這篇文章主要介紹了關(guān)于JSqlparser使用攻略(高效的SQL解析工具),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Java字符串寫入文件三種方式的實(shí)現(xiàn)

    Java字符串寫入文件三種方式的實(shí)現(xiàn)

    這篇文章主要介紹了 Java字符串寫入文件三種方式的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 顯示IntelliJ IDEA工具的Run Dashboard功能圖文詳解

    顯示IntelliJ IDEA工具的Run Dashboard功能圖文詳解

    這篇文章主要介紹了顯示IntelliJ IDEA工具的Run Dashboard功能,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • Java中的HashMap集合源碼詳細(xì)解讀

    Java中的HashMap集合源碼詳細(xì)解讀

    這篇文章主要介紹了Java中的HashMap集合源碼詳細(xì)解讀,hash表是一種數(shù)據(jù)結(jié)構(gòu),它擁有驚人的效率,它的時間復(fù)雜度低到接近O(1)這樣的常數(shù)級,需要的朋友可以參考下
    2023-11-11
  • JVM GC 垃圾收集梳理總結(jié)

    JVM GC 垃圾收集梳理總結(jié)

    這篇文章主要介紹了JVM GC 垃圾收集梳理總結(jié),GC是一種自動的存儲管理機(jī)制。當(dāng)一些被占用的內(nèi)存不再需要時,就應(yīng)該予以釋放,這種存儲資源管理,稱為垃圾回收
    2022-07-07
  • 使用springboot整合websocket實(shí)現(xiàn)群聊教程

    使用springboot整合websocket實(shí)現(xiàn)群聊教程

    websocket怎么說呢,就是服務(wù)器可以主動向客戶端發(fā)起對話,下面就是springboot整合websocket實(shí)現(xiàn)群聊的操作代碼,一起來看一下get新技能吧
    2021-08-08
  • Java弱引用集合WeakHashMap總結(jié)

    Java弱引用集合WeakHashMap總結(jié)

    這篇文章主要介紹了Java弱引用集合WeakHashMap總結(jié),WeakHashMap利用WeakReference的弱引用特性讓用戶在使用的過程中不會因?yàn)闆]有釋放Map中的資源而導(dǎo)致內(nèi)存泄露,WeakHashMap實(shí)現(xiàn)了Map接口,使用方式和其他的Map相同,需要的朋友可以參考下
    2023-09-09
  • springmvc下實(shí)現(xiàn)登錄驗(yàn)證碼功能示例

    springmvc下實(shí)現(xiàn)登錄驗(yàn)證碼功能示例

    本篇文章主要介紹了springmvc下實(shí)現(xiàn)登錄驗(yàn)證碼功能示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • IDEA新建Springboot項(xiàng)目(圖文教程)

    IDEA新建Springboot項(xiàng)目(圖文教程)

    下面小編就為大家?guī)硪黄狪DEA新建Springboot項(xiàng)目(圖文教程)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07

最新評論