天天看點

【ActiveMQ】2.spring Boot下使用ActiveMQ

在spring boot下使用ActiveMQ,需要一下幾個條件

======================================================================================================================

好了下面正式開始吧:

  1.pom.xml檔案添加maven依賴

  2.application.properties配置檔案配置

  3.消息生産者Producer.java

【ActiveMQ】2.spring Boot下使用ActiveMQ
【ActiveMQ】2.spring Boot下使用ActiveMQ

View Code

  4.消息消費者Consumer.java

【ActiveMQ】2.spring Boot下使用ActiveMQ
【ActiveMQ】2.spring Boot下使用ActiveMQ

  5.控制層調用Controller【localhost:8080/log消息生産】【localhost:8080/log2消息消費】

【ActiveMQ】2.spring Boot下使用ActiveMQ
【ActiveMQ】2.spring Boot下使用ActiveMQ

=============================================================================================================================

上面已經可以使用了,但是消息隊列中對于消息的消費,如果能實作自動監聽,自動處理那就更好了,給【消息消費者Consumer】修改一下

Consumer.java

【ActiveMQ】2.spring Boot下使用ActiveMQ
【ActiveMQ】2.spring Boot下使用ActiveMQ

消息生産者依舊 不變,然後Controller.java往隊列中存放消息即可。

【ActiveMQ】2.spring Boot下使用ActiveMQ
【ActiveMQ】2.spring Boot下使用ActiveMQ

============================================================================================================================

最後,如果想要實作雙向消息傳輸呢?

什麼意思呢,就是【消息消費者Consumer】處理完消息之後,可以帶回給【消息生産者Producer 】一些有用的資訊.

這樣的話,Consumer.java就該這麼寫:

這樣在監聽到jms.queue消息隊列中的消息之後,處理完成,将傳回的消息放入reback.queue消息隊列中

【ActiveMQ】2.spring Boot下使用ActiveMQ
【ActiveMQ】2.spring Boot下使用ActiveMQ

而Producer.java應該再多加一個監聽,監聽傳回消息隊列中的消息:

【ActiveMQ】2.spring Boot下使用ActiveMQ
【ActiveMQ】2.spring Boot下使用ActiveMQ

最後Controller.java中通路測試一下:

【ActiveMQ】2.spring Boot下使用ActiveMQ
【ActiveMQ】2.spring Boot下使用ActiveMQ
【ActiveMQ】2.spring Boot下使用ActiveMQ

====================================================================================

結束!!!