Springboot Activemq整合過程代碼圖解
這篇文章主要介紹了Springboot Activemq整合過程代碼圖解,文中通過示例代碼介紹的非常詳細,對大家的學(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 進行消息的發(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)文章
解決springmvc項目中使用過濾器來解決請求方式為post時出現(xiàn)亂碼的問題
這篇文章主要介紹了springmvc項目中使用過濾器來解決請求方式為post時出現(xiàn)亂碼的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
springboot druid數(shù)據(jù)庫連接池連接失敗后一直重連的解決方法
本文主要介紹了springboot druid數(shù)據(jù)庫連接池連接失敗后一直重連的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Spring FreeMarker整合Struts2過程詳解
這篇文章主要介紹了Spring FreeMarker整合Struts2過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
關(guān)于mybatis3中幾個@Provider的使用方式
這篇文章主要介紹了關(guān)于mybatis3中幾個@Provider的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
java實現(xiàn)圖片轉(zhuǎn)ascii字符畫的方法示例
這篇文章主要介紹了java實現(xiàn)圖片轉(zhuǎn)ascii字符畫的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
SpringBoot內(nèi)嵌tomcat處理有特殊字符轉(zhuǎn)義的問題
這篇文章主要介紹了SpringBoot內(nèi)嵌tomcat處理有特殊字符轉(zhuǎn)義的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

