Springboot中怎么對ActiveMQ進行整合,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
蔡甸網站制作公司哪家好,找創新互聯建站!從網頁設計、網站建設、微信開發、APP開發、響應式網站建設等網站項目制作,到程序開發,運營維護。創新互聯建站2013年至今到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創新互聯建站。
1、首先新建一個springboot工程(新建過程略),本文springboot版本 是2.1.1.RELEASE
2、在pom.xml文件添加activemq的相關依賴
org.springframework.boot spring-boot-starter-activemq org.apache.activemq activemq-pool org.messaginghub pooled-jms
其中連接池相關的依賴可以不用配置
3、配置application.properties 或者application.yml 本文以application.properties為例
spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin #默認情況下activemq提供的是queue模式,若要使用topic模式需要配置下面配置 #spring.jms.pub-sub-domain=true #true 表示使用內置的MQ,false則連接服務器 spring.activemq.in-memory=false #true表示使用連接池;false時,每發送一條數據創建一個連接 spring.activemq.pool.enabled=true #連接池最大連接數 spring.activemq.pool.max-connections=10 #空閑的連接過期時間,默認為30秒 spring.activemq.pool.idle-timeout=15000
spring.activemq.pool.enabled=true時要在pom文件中添加連接池pool相關的依賴,為false時不用添加連接池pool相關的依賴;
若使用連接池pool配置時,注意兩種依賴的配置否則啟動失敗。
工程結構如下圖
Demo代碼如下
package com.example.acmpp.config; import javax.jms.Queue; import javax.jms.Topic; import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQTopic; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class BeanConfig { //定義存放消息的隊列 @Bean public Queue queue() { return new ActiveMQQueue("ActiveMQQueue"); } //定義存放消息的隊列 @Bean public Topic topic() { return new ActiveMQTopic("ActiveMQTopic"); } }
消息生產者代碼
import javax.jms.Queue; import javax.jms.Topic; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /* * * 消息生產者 */ @RestController public class ProviderController { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; @Autowired private Topic topic; /** * 消息生產者 Queue模式 * */ @RequestMapping("/sendQ") public void sendQ(String msg) { //方法一:添加消息到消息隊列 jmsMessagingTemplate.convertAndSend(queue, msg); //方法二:這種方式不需要手動創建queue,系統會自行創建名為test的隊列 //jmsMessagingTemplate.convertAndSend("testQ", msg); } /** * 消息生產者 Topic模式 * @param msg */ @RequestMapping("/sendT") public void sendT(String msg) { // 指定消息發送的目的地及內容 System.out.println("@@@@@@@@@@@@@@" + msg); this.jmsMessagingTemplate.convertAndSend(this.topic, msg); } }
消息消費者代碼
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.annotation.JmsListener; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.stereotype.Component; /** * 消息消費者 * @author FFF * */ @Component public class ConsumerService { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; /** * 消費ActiveMQQueue */ // 使用JmsListener配置消費者監聽的隊列,其中name是接收到的消息 @JmsListener(destination = "ActiveMQQueue") // SendTo 會將此方法返回的數據, 寫入到 OutQueue 中去. @SendTo("SQueue") public String handleMessage(String name) { System.out.println("ActiveMQQueue成功接受Name" + name); return "ActiveMQQueue成功接受Name" + name; } /** * 消費ActiveMQ.DLQ */ // 使用JmsListener配置消費者監聽的隊列,其中name是接收到的消息 @JmsListener(destination = "ActiveMQ.DLQ") public void DLQ(String name) { System.out.println("ActiveMQ.DLQ成功接受Name==" + name); } /** * 消費SQueue */ // 使用JmsListener配置消費者監聽的隊列,其中name是接收到的消息 @JmsListener(destination = "SQueue") public void SQueue(String name) { System.out.println("SQueue成功接受Name==" + name); } /** * 消費testQ */ // 使用JmsListener配置消費者監聽的隊列,其中name是接收到的消息 @JmsListener(destination = "testQ") public void testQMessage(String name) { System.out.println("testQ成功接受Name" + name); } /** * 消費topic * */ // 使用JmsListener配置消費者監聽的隊列,其中name是接收到的消息 @JmsListener(destination = "ActiveMQTopic") public void topicMessage(String name) { System.out.println("topicMessage成功接受Name" + name); } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創新互聯行業資訊頻道,感謝您對創新互聯的支持。