Springboot整合ActiveMQ實現(xiàn)消息隊列的過程淺析
確保你啟動了自己電腦的activemq。
pom中導(dǎo)入坐標(biāo)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>書寫yml配置
spring:
activemq:
broker-url: tcp://localhost:61616
jms:
template:
default-destination: my #消息隊列的默認(rèn)名字
pub-sub-domain: false #是否開啟消息訂閱模式
業(yè)務(wù)層代碼
@Service
public class MessageServiceActive implements MessageService {
@Autowired
private JmsMessagingTemplate template;
@Override
public void getMessage(String id) {
template.convertAndSend("AA",id); //自定義消息隊列名字
System.out.println("待發(fā)送短信的訂單已納入處理隊列,id:"+id);
}
@Override
public String doMessage() {
String id=template.receiveAndConvert("AA",String.class);
return id;
}此時,你書寫控制層代碼就可以實現(xiàn)消息隊列了,不過有一個缺陷,那就是處理消息的時候必須手動書寫網(wǎng)址,調(diào)用執(zhí)行對應(yīng)的方法才可以,不過我們可以通過監(jiān)聽器來實現(xiàn)自動化,就是一旦有消息產(chǎn)生就會立即處理。
監(jiān)聽器代碼
@Component
public class MessageListener {
@Autowired
private JmsMessagingTemplate template;
@JmsListener(destination = "AA") //要監(jiān)聽的消息隊列名稱
@SendTo(value = "BB") //自動化處理完,還可以把該消息傳遞給下一了消息隊列
public String receive(){
String id=template.receiveAndConvert("AA",String.class);
System.out.println("已完成短信發(fā)送業(yè)務(wù),id:"+id+"\n");
return "new_"+id; //利用返回值轉(zhuǎn)發(fā)給下一個
}
}業(yè)務(wù)層代碼
@Service
public class MessageServiceActive implements MessageService {
@Autowired
private JmsMessagingTemplate template;
@Override
public void getMessage(String id) {
template.convertAndSend("AA",id);
System.out.println("待發(fā)送短信的訂單已納入處理隊列,id:"+id);
}
}這樣我們的消息就可以流水線似的處理了,這是點(diǎn)對點(diǎn)的消息隊列方式,還有訂閱式的。
到此這篇關(guān)于Springboot整合ActiveMQ實現(xiàn)消息隊列的過程淺析的文章就介紹到這了,更多相關(guān)Springboot整合ActiveMQ內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談JAVA 線程狀態(tài)中可能存在的一些誤區(qū)
這篇文章主要介紹了淺談JAVA 線程狀態(tài)中可能存在的一些誤區(qū),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
springboot?vue項目管理后端實現(xiàn)接口新增
這篇文章主要為大家介紹了springboot?vue項目管理后端實現(xiàn)接口新增,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
關(guān)于mybatis callSettersOnNulls 配置解析
這篇文章主要介紹了關(guān)于mybatis callSettersOnNulls 配置,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下2018-06-06
一文帶你熟練掌握J(rèn)ava中的日期時間相關(guān)類
我們在開發(fā)時,除了數(shù)字、數(shù)學(xué)這樣的常用API之外,還有日期時間類,更是會被經(jīng)常使用,比如我們項目中必備的日志功能,需要記錄異常等信息產(chǎn)生的時間,本文就帶各位來學(xué)習(xí)一下相關(guān)的日期時間類有哪些2023-05-05
SpringBoot+SseEmitter和Vue3+EventSource實現(xiàn)實時數(shù)據(jù)推送
本文主要介紹了SpringBoot+SseEmitter和Vue3+EventSource實現(xiàn)實時數(shù)據(jù)推送,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
java使用spring實現(xiàn)發(fā)送mail的方法
這篇文章主要介紹了java使用spring實現(xiàn)發(fā)送mail的方法,涉及java基于spring框架發(fā)送郵件的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10

