SpringBoot整合ActiveMQ

一. 安裝ActiveMQhtml

到Apache官方網站下載最新的ActiveMQ的安裝包,並解壓到本地目錄下,下載連接以下:http://activemq.apache.org/download.html,解壓後的目錄結構以下:java

bin目錄結構以下:spring

若是咱們是32位的機器,就雙擊win32目錄下的activemq.bat,若是是64位機器,則雙擊win64目錄下的activemq.bat,運行結果以下:apache

啓動成功!成功以後在瀏覽器輸入http://127.0.0.1:8161/地址,能夠看到ActiveMQ的管理頁面,用戶名和密碼默認都是admin,以下:瀏覽器

2、新建spring boot工程,並加入JMS(ActiveMQ)依賴springboot

 

3、工程結構app

 

pom依賴以下:maven

 1     <?xml version="1.0" encoding="UTF-8"?>  
 2     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
 4         <modelVersion>4.0.0</modelVersion>  
 5       
 6         <groupId>com.chhliu.springboot.jms</groupId>  
 7         <artifactId>springboot-jms</artifactId>  
 8         <version>0.0.1-SNAPSHOT</version>  
 9         <packaging>jar</packaging>  
10       
11         <name>springboot-jms</name>  
12         <description>Demo project for Spring Boot Jms</description>  
13       
14         <parent>  
15             <groupId>org.springframework.boot</groupId>  
16             <artifactId>spring-boot-starter-parent</artifactId>  
17             <version>1.4.3.RELEASE</version>  
18             <relativePath/> <!-- lookup parent from repository -->  
19         </parent>  
20       
21         <properties>  
22             <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
23             <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
24             <java.version>1.7</java.version>  
25         </properties>  
26       
27         <dependencies>  
28             <dependency>  
29                 <groupId>org.springframework.boot</groupId>  
30                 <artifactId>spring-boot-starter-activemq</artifactId>  
31             </dependency>  
32       
33             <dependency>  
34                 <groupId>org.springframework.boot</groupId>  
35                 <artifactId>spring-boot-starter-test</artifactId>  
36                 <scope>test</scope>  
37             </dependency>  
38         </dependencies>  
39       
40         <build>  
41             <plugins>  
42                 <plugin>  
43                     <groupId>org.springframework.boot</groupId>  
44                     <artifactId>spring-boot-maven-plugin</artifactId>  
45                 </plugin>  
46             </plugins>  
47         </build>  
48     </project>  

4、修改application.properties配置文件tcp

1     ## URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616`  
2     # failover:(tcp://localhost:61616,tcp://localhost:61617)  
3     # tcp://localhost:61616  
4     spring.activemq.broker-url=tcp://localhost:61616  
5     spring.activemq.in-memory=true  
6     spring.activemq.pool.enabled=false  

5、消息生產者spring-boot

 1 package com.chhliu.springboot.jms;  
 2   
 3 import javax.jms.Destination;  
 4   
 5 import org.springframework.beans.factory.annotation.Autowired;  
 6 import org.springframework.jms.core.JmsMessagingTemplate;  
 7 import org.springframework.stereotype.Service;  
 8   
 9 @Service("producer")  
10 public class Producer {  
11     @Autowired // 也能夠注入JmsTemplate,JmsMessagingTemplate對JmsTemplate進行了封裝  
12     private JmsMessagingTemplate jmsTemplate;  
13     // 發送消息,destination是發送到的隊列,message是待發送的消息  
14     public void sendMessage(Destination destination, final String message){  
15         jmsTemplate.convertAndSend(destination, message);  
16     }  
17 } 

 

6、消息消費者

 1     package com.chhliu.springboot.jms;  
 2       
 3     import org.springframework.jms.annotation.JmsListener;  
 4     import org.springframework.stereotype.Component;  
 5       
 6     @Component  
 7     public class Consumer {  
 8             // 使用JmsListener配置消費者監聽的隊列,其中text是接收到的消息  
 9         @JmsListener(destination = "mytest.queue")  
10         public void receiveQueue(String text) {  
11             System.out.println("Consumer收到的報文爲:"+text);  
12         }  
13     }  

 

 消費者2的代碼同上,注意,消息消費者的類上必須加上@Component,或者是@Service,這樣的話,消息消費者類就會被委派給Listener類,原理相似於使用SessionAwareMessageListener以及MessageListenerAdapter來實現消息驅動POJO

7、測試

 1     package com.chhliu.springboot.jms;  
 2       
 3     import javax.jms.Destination;  
 4       
 5     import org.apache.activemq.command.ActiveMQQueue;  
 6     import org.junit.Test;  
 7     import org.junit.runner.RunWith;  
 8     import org.springframework.beans.factory.annotation.Autowired;  
 9     import org.springframework.boot.test.context.SpringBootTest;  
10     import org.springframework.test.context.junit4.SpringRunner;  
11       
12     @RunWith(SpringRunner.class)  
13     @SpringBootTest  
14     public class SpringbootJmsApplicationTests {  
15           
16         @Autowired  
17         private Producer producer;  
18           
19         @Test  
20         public void contextLoads() throws InterruptedException {  
21             Destination destination = new ActiveMQQueue("mytest.queue");  
22               
23             for(int i=0; i<100; i++){  
24                 producer.sendMessage(destination, "myname is chhliu!!!");  
25             }  
26         }  
27       
28     }  

測試結果以下:

 1     Consumer2收到的報文爲:myname is chhliu!!!  
 2     Consumer收到的報文爲:myname is chhliu!!!  
 3     Consumer2收到的報文爲:myname is chhliu!!!  
 4     Consumer收到的報文爲:myname is chhliu!!!  
 5     Consumer2收到的報文爲:myname is chhliu!!!  
 6     Consumer收到的報文爲:myname is chhliu!!!  
 7     Consumer2收到的報文爲:myname is chhliu!!!  
 8     Consumer收到的報文爲:myname is chhliu!!!  
 9     Consumer2收到的報文爲:myname is chhliu!!!  
10     Consumer收到的報文爲:myname is chhliu!!!  
11     Consumer2收到的報文爲:myname is chhliu!!!  
12     Consumer收到的報文爲:myname is chhliu!!!  
13     Consumer2收到的報文爲:myname is chhliu!!!  

通過上面的幾個步驟,spring boot和Jms就基本上整合完成了,是否是使用起來很方便了!

8、實現雙向隊列

一、下面首先來對Consumer2這個消費者來進行下改造,代碼以下:

 1     package com.chhliu.springboot.jms;  
 2       
 3     import org.springframework.jms.annotation.JmsListener;  
 4     import org.springframework.messaging.handler.annotation.SendTo;  
 5     import org.springframework.stereotype.Component;  
 6       
 7     @Component  
 8     public class Consumer2 {  
 9       
10         @JmsListener(destination = "mytest.queue")  
11         @SendTo("out.queue")  
12         public String receiveQueue(String text) {  
13             System.out.println("Consumer2收到的報文爲:"+text);  
14             return "return message"+text;  
15         }  
16     }  

從上面的代碼能夠看出,咱們在receiveQueue方法上面多加了一個註解@SendTo("out.queue"),該註解的意思是將return回的值,再發送到"out.queue"隊列中,該隊列中的消息,就是咱們返回的值!

9、對Producer進行改造

經過上面的示例,咱們如今對Producer進行改造,使其既能生產報文,又能消費隊列中的報文,代碼以下:

 1     package com.chhliu.springboot.jms;  
 2       
 3     import javax.jms.Destination;  
 4       
 5     import org.springframework.beans.factory.annotation.Autowired;  
 6     import org.springframework.jms.annotation.JmsListener;  
 7     import org.springframework.jms.core.JmsMessagingTemplate;  
 8     import org.springframework.stereotype.Service;  
 9       
10     @Service("producer")  
11     public class Producer {  
12         @Autowired  
13         private JmsMessagingTemplate jmsTemplate;  
14           
15         public void sendMessage(Destination destination, final String message){  
16             jmsTemplate.convertAndSend(destination, message);  
17         }  
18           
19         @JmsListener(destination="out.queue")  
20         public void consumerMessage(String text){  
21             System.out.println("從out.queue隊列收到的回覆報文爲:"+text);  
22         }  
23     }  

測試結果以下:

 1     從out.queue隊列收到的回覆報文爲:return messagemyname is chhliu!!!  
 2     Consumer收到的報文爲:myname is chhliu!!!  
 3     Consumer2收到的報文爲:myname is chhliu!!!  
 4     從out.queue隊列收到的回覆報文爲:return messagemyname is chhliu!!!  
 5     Consumer收到的報文爲:myname is chhliu!!!  
 6     Consumer2收到的報文爲:myname is chhliu!!!  
 7     從out.queue隊列收到的回覆報文爲:return messagemyname is chhliu!!!  
 8     Consumer收到的報文爲:myname is chhliu!!!  
 9     Consumer2收到的報文爲:myname is chhliu!!!  
10     從out.queue隊列收到的回覆報文爲:return messagemyname is chhliu!!!  
相關文章
相關標籤/搜索