最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot Activemq整合過程代碼圖解

 更新時間:2020年02月21日 13:41:46   作者:流氓大隊長  
這篇文章主要介紹了Springboot Activemq整合過程代碼圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了Springboot Activemq整合過程代碼圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

Springboot+Activemq整合

1 導(dǎo)入整合所需要的依賴:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>

2 創(chuàng)建application.properties文件

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

3.自定義配置文件QueueConfig 讀取配置文件的隊列名,根據(jù)隊列名字創(chuàng)建一個Queue

package com.example.demo;

import javax.jms.Queue;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

@Configuration
public class QueueConfig {

  @Value("${queue}")
  private String queue;

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

4.創(chuàng)建生產(chǎn)者,可以直接使用提供的模板JmsMessagingTemplate 進(jìn)行消息的發(fā)送:

package com.example.demo.producter;

import javax.jms.Queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import com.example.demo.SpringbootActivemqApplication;

@Component
public class Producter {
  @Autowired
  private JmsMessagingTemplate jmsMessagingTemplate;
  @Autowired
  private Queue queue;
  private static Logger logger = LoggerFactory.getLogger(
Producter 
.class); public void send() { String str = "生產(chǎn)者生產(chǎn)數(shù)據(jù):" + System.currentTimeMillis(); jmsMessagingTemplate.convertAndSend(queue, str); logger.info("生產(chǎn)者數(shù)據(jù):{}", str); } }

5.啟動類:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.example.demo.producter.Producter;
import com.example.demo.producter.consumer.Consumer;

@SpringBootApplication
@EnableScheduling
public class SpringbootActivemqApplication implements ApplicationListener<ContextRefreshedEvent> {
  @Autowired
  public Producter producter;
  @Autowired
  public Consumer consumer;

  public static void main(String[] args) {
    SpringApplication.run(SpringbootActivemqApplication.class, args);
    //onApplicationEvent方法 在啟動springboot的時候 會運行該方法,可根據(jù)項目實際情況 選擇合適調(diào)用消息發(fā)送方法

  }

  @Override
  public void onApplicationEvent(ContextRefreshedEvent event) {
    producter.send();
  }

}

6.啟動項目,控制臺輸出內(nèi)容:

7.創(chuàng)建消費者,創(chuàng)建消費者比較容易,只需要監(jiān)聽隊列就可以:

package com.example.demo.producter.consumer;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

  @JmsListener(destination = "${queue}")
  public void receive(String msg) {
    System.out.println("監(jiān)聽器收到msg:" + msg);
  }

}

8.最后結(jié)果:

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

相關(guān)文章

  • 使用Spring Cache設(shè)置緩存條件操作

    使用Spring Cache設(shè)置緩存條件操作

    這篇文章主要介紹了使用Spring Cache設(shè)置緩存條件操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Idea如何導(dǎo)入java mysql驅(qū)動包

    Idea如何導(dǎo)入java mysql驅(qū)動包

    本文介紹了如何在IntelliJ IDEA中配置MySQL數(shù)據(jù)庫連接,首先下載MySQL Connector/J驅(qū)動并解壓,然后在Idea項目中創(chuàng)建lib文件夾并將.jar文件復(fù)制到該文件夾,接著,將.jar文件添加為項目庫,通過這些步驟,可以成功配置MySQL數(shù)據(jù)庫連接
    2024-12-12
  • Java之MyBatis入門詳解

    Java之MyBatis入門詳解

    這篇文章主要介紹了Java之MyBatis入門詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 解決spring-boot2.0.6中webflux無法獲得請求IP的問題

    解決spring-boot2.0.6中webflux無法獲得請求IP的問題

    這幾天在用 spring-boot 2 的 webflux 重構(gòu)一個工程,寫到了一個需要獲得客戶端請求 IP 的地方,在寫的過程中遇到很多問題,下面小編通過一段代碼給大家介紹解決spring-boot2.0.6中webflux無法獲得請求IP的問題,感興趣的朋友跟隨小編一起看看吧
    2018-10-10
  • Java快速排序與歸并排序及基數(shù)排序圖解示例

    Java快速排序與歸并排序及基數(shù)排序圖解示例

    快速排序是基于二分的思想,對冒泡排序的一種改進(jìn)。主要思想是確立一個基數(shù),將小于基數(shù)的數(shù)放到基數(shù)左邊,大于基數(shù)的數(shù)字放到基數(shù)的右邊,然后在對這兩部分進(jìn)一步排序,從而實現(xiàn)對數(shù)組的排序
    2022-09-09
  • ScheduledThreadPoolExecutor巨坑解決

    ScheduledThreadPoolExecutor巨坑解決

    這篇文章主要為大家介紹了使用ScheduledThreadPoolExecutor遇到的巨坑解決示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Spring超出最大會話數(shù)(Max?sessions?limit?reached:?10000)

    Spring超出最大會話數(shù)(Max?sessions?limit?reached:?10000)

    在Spring系統(tǒng)中遇到的Maxsessionslimitreached:10000錯誤,該錯誤由于會話數(shù)超過默認(rèn)限制10000而觸發(fā),下面就來介紹一下解決方法,感興趣的可以了解一下
    2024-12-12
  • 微信、支付寶二碼合一掃碼支付實現(xiàn)思路(java)

    微信、支付寶二碼合一掃碼支付實現(xiàn)思路(java)

    這篇文章主要為大家詳細(xì)介紹了微信、支付寶二碼合一掃碼支付實現(xiàn)思路,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Java裝飾者模式實例詳解

    Java裝飾者模式實例詳解

    這篇文章主要介紹了Java裝飾者模式,結(jié)合實例形式詳細(xì)分析了裝飾著模式的原理與java具體實現(xiàn)技巧,需要的朋友可以參考下
    2017-09-09
  • Java實現(xiàn)代碼塊耗時測算工具類

    Java實現(xiàn)代碼塊耗時測算工具類

    這篇文章主要為大家介紹了如何利用Java語言編寫一個工具類,用來測算代碼塊的耗時,同時還能顯示進(jìn)度,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-05-05

最新評論

大英县| 来凤县| 岱山县| 大连市| 厦门市| 嘉荫县| 镇沅| 延安市| 桂平市| 南平市| 侯马市| 兰西县| 农安县| 武城县| 西昌市| 霸州市| 时尚| 芮城县| 磐安县| 嵊泗县| 庆元县| 阳新县| 遵义市| 五寨县| 库车县| 敦煌市| 泊头市| 谷城县| 花莲市| 佛教| 五寨县| 沙田区| 巴南区| 西乌| 玉门市| 新泰市| 叶城县| 南丰县| 济阳县| 清远市| 白玉县|