Springboot Activemq整合過程代碼圖解
這篇文章主要介紹了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)文章
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
解決spring-boot2.0.6中webflux無法獲得請求IP的問題
這幾天在用 spring-boot 2 的 webflux 重構(gòu)一個工程,寫到了一個需要獲得客戶端請求 IP 的地方,在寫的過程中遇到很多問題,下面小編通過一段代碼給大家介紹解決spring-boot2.0.6中webflux無法獲得請求IP的問題,感興趣的朋友跟隨小編一起看看吧2018-10-10
ScheduledThreadPoolExecutor巨坑解決
這篇文章主要為大家介紹了使用ScheduledThreadPoolExecutor遇到的巨坑解決示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Spring超出最大會話數(shù)(Max?sessions?limit?reached:?10000)
在Spring系統(tǒng)中遇到的Maxsessionslimitreached:10000錯誤,該錯誤由于會話數(shù)超過默認(rèn)限制10000而觸發(fā),下面就來介紹一下解決方法,感興趣的可以了解一下2024-12-12
微信、支付寶二碼合一掃碼支付實現(xiàn)思路(java)
這篇文章主要為大家詳細(xì)介紹了微信、支付寶二碼合一掃碼支付實現(xiàn)思路,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08

