Springboot Activemq整合過(guò)程代碼圖解
這篇文章主要介紹了Springboot Activemq整合過(guò)程代碼圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
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 讀取配置文件的隊(duì)列名,根據(jù)隊(duì)列名字創(chuàng)建一個(gè)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.啟動(dòng)類:
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方法 在啟動(dòng)springboot的時(shí)候 會(huì)運(yùn)行該方法,可根據(jù)項(xiàng)目實(shí)際情況 選擇合適調(diào)用消息發(fā)送方法 } @Override public void onApplicationEvent(ContextRefreshedEvent event) { producter.send(); } }
6.啟動(dòng)項(xiàng)目,控制臺(tái)輸出內(nèi)容:
7.創(chuàng)建消費(fèi)者,創(chuàng)建消費(fèi)者比較容易,只需要監(jiān)聽隊(duì)列就可以:
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é)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)簡(jiǎn)單的客戶信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單的客戶信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06解決springmvc項(xiàng)目中使用過(guò)濾器來(lái)解決請(qǐng)求方式為post時(shí)出現(xiàn)亂碼的問(wèn)題
這篇文章主要介紹了springmvc項(xiàng)目中使用過(guò)濾器來(lái)解決請(qǐng)求方式為post時(shí)出現(xiàn)亂碼的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08springboot druid數(shù)據(jù)庫(kù)連接池連接失敗后一直重連的解決方法
本文主要介紹了springboot druid數(shù)據(jù)庫(kù)連接池連接失敗后一直重連的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Spring FreeMarker整合Struts2過(guò)程詳解
這篇文章主要介紹了Spring FreeMarker整合Struts2過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10關(guān)于mybatis3中幾個(gè)@Provider的使用方式
這篇文章主要介紹了關(guān)于mybatis3中幾個(gè)@Provider的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07java實(shí)現(xiàn)圖片轉(zhuǎn)ascii字符畫的方法示例
這篇文章主要介紹了java實(shí)現(xiàn)圖片轉(zhuǎn)ascii字符畫的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08SpringBoot內(nèi)嵌tomcat處理有特殊字符轉(zhuǎn)義的問(wèn)題
這篇文章主要介紹了SpringBoot內(nèi)嵌tomcat處理有特殊字符轉(zhuǎn)義的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06java中用String.Join美化代碼的實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于java中用String.Join美化代碼的實(shí)例講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2020-12-12