1. 程式人生 > >springboot入門_ActiveMq

springboot入門_ActiveMq

方法 過多 一個 目標地址 模型 1.5 except XML 連接

ActiveMq是apache的一個消息隊列,在應用程序中主要被用來實現程序的解耦和異步執行,它主要有2中消息模型,一對一的隊列模型和一對多的訂閱模型,此處我們不做過多的解釋了,不清楚的朋友可以去ActiveMq官網了解。本文主要記錄下在springboot中使用activemq.

springboot提供了spring-boot-starter-activemq來對activemq做支持,所以在項目中我們需要引入spring-boot-starter-activemq依賴

首先我們創建springboot工程,並添加相應的依賴,pom.xml如下:

 1 <dependencies>
 2
<dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter</artifactId> 5 <version>1.5.10.RELEASE</version> 6 </dependency> 7 8 <dependency> 9 <groupId>org.springframework.boot</groupId> 10
<artifactId>spring-boot-starter-test</artifactId> 11 <version>1.5.10.RELEASE</version> 12 <scope>test</scope> 13 </dependency> 14 15 <!-- activemq依賴 --> 16 <dependency> 17 <groupId>org.springframework.boot</groupId> 18
<artifactId>spring-boot-starter-activemq</artifactId> 19 <version>1.5.10.RELEASE</version> 20 </dependency> 21 22 <dependency> 23 <groupId>junit</groupId> 24 <artifactId>junit</artifactId> 25 <version>4.12</version> 26 <scope>test</scope> 27 </dependency> 28 </dependencies>

下來我們需要和ActiveMq服務其進行連接,在application.properties添加MQ服務器配置信息,如下:

1 spring.activemq.broker-url=tcp://192.168.1.109:61616
2 spring.activemq.user=admin
3 spring.activemq.password=admin
4 
5 # 消息類型 默認值為false,  false:queue true:topic
6 #spring.jms.pub-sub-domain=true

下來就是發送和接收消息了,springboot提供了JmsTemplate供我們發送消息,

 1 @Service
 2 public class CustomJmsTemplate {
 3     
 4     @Autowired
 5     private JmsTemplate jmsTemplate;
 6     
 7     private int count = 0;
 8     
 9     /**
10      * 發送隊列消息
11      * @param destinationName 隊列名稱
12      * @param content 消息內容
13      */
14     public void sendQueueMessage(String destinationName, String content){
15         jmsTemplate.convertAndSend(destinationName, content);
16     }
17     
18     @JmsListener(destination = "active_mq")
19     public void processMessage(String content) {
20         count++;
21         System.out.println("隊列消費者1 接收到第   "+count+" 個消息,內容:"+content);
22     }
23     
24 }

使用jmsTemplate的convertAndSend方法我們可以向指定隊列發送消息,消息接收放可以使用監聽器@JmsListener(destination = "active_mq") 對指定隊列(destination )進行監聽,有消息時進行處理,至此我們就完成了發送和接收消息。寫個測試方法測試消息的發送和接收

 1 @RunWith(SpringRunner.class)
 2 @SpringBootTest
 3 public class CustomJmsTemplateTest {
 4     
 5     @Autowired
 6     private CustomJmsTemplate customJmsTemplate;
 7     
 8     @Test
 9     public void sendQueueMessageTest() throws InterruptedException{
10         for(int i=0;i<10;i++){
11             String text = "測試使用activemq發送第"+i+"消息";
12             System.out.println(text);
13             customJmsTemplate.sendQueueMessage("active_mq", text);
14             Thread.sleep(2000);//發送後暫停2秒以便觀察消費者接收消息
15         }
16     }
17     
18 }

執行測試方法後可以看到執行結果

技術分享圖片

這樣我們就實現了發送一對一的隊列消息,如果我們想發送一對多的訂閱消息怎麽辦呢?我們需要將配置文件中的spring.jms.pub-sub-domain設置為true,用來說明當前發送的是訂閱消息,默認值為false,代表是隊列消息。spring.jms.pub-sub-domain=true,隊列消息只能有一個消費者,訂閱消息可以有多個消費者,寫個方法發送訂閱消息:

/**
     * 發送訂閱消息
     * @param destinationName 消息目標地址
     * @param message 消息內容
     */
    public void sendTopicMessage(String destinationName, String message){
        jmsTemplate.convertAndSend(destinationName, message);
    }

多個訂閱消息的消費者,即多個監聽器同時監聽一個消息

1 @JmsListener(destination = "active_mq_topic")
2     public void processMessage1(String content) {
3         System.out.println("消費者1 接收到消息內容:"+content);
4     }
5     
6     @JmsListener(destination = "active_mq_topic")
7     public void processMessage2(String content) {
8         System.out.println("消費者2 接收到消息內容:"+content);
9     }

測試發送topic消息

1 @Test
2     public void sendTopicMessageTest() throws InterruptedException{
3         for(int i=0;i<10;i++){
4             String text = "發送第"+i+"個topic消息";
5             System.out.println(text);
6             customJmsTemplate.sendTopicMessage("active_mq_topic", text);
7             Thread.sleep(2000);//發送後暫停2秒以便觀察消費者接收消息
8         }
9     }

執行測試方法後可以看到,每一個消費者都接收到了消息

技術分享圖片

現在我們分別實現了發送一對一的隊列消息和一對多的訂閱消息,在發送不同模式的消息時,我們修改了spring.jms.pub-sub-domain的值,但是在項目中這個都是固定的,在項目啟動時都已經確定了,不會被更改,而一般我們在項目中會同時用到兩種不同模式的消息,所以接下來的問題就是如何來兼容這兩種模式的消息呢,研究了半天,在網上也找了些資料都沒有能實現。後期有時間再繼續研究這個,如果哪位朋友有實現的方案或者思路,麻煩留言指導下。

springboot入門_ActiveMq