SpringBoot整合ActiveMQ
一. 安装ActiveMQ
到Apache官方网站下载最新的ActiveMQ的安装包,并解压到本地目录下,下载链接如下:http://activemq.apache.org/download.html,解压后的目录结构如下:
bin目录结构如下:
如果我们是32位的机器,就双击win32目录下的activemq.bat,如果是64位机器,则双击win64目录下的activemq.bat,运行结果如下:
启动成功!成功之后在浏览器输入http://127.0.0.1:8161/地址,可以看到ActiveMQ的管理页面,用户名和密码默认都是admin,如下:
二、新建spring boot工程,并加入JMS(ActiveMQ)依赖
三、工程结构
pom依赖如下:
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>
四、修改application.properties配置文件
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
五、消息生产者
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 }
六、消息消费者
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
七、测试
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就基本上整合完成了,是不是使用起来很方便了!
八、实现双向队列
1、下面首先来对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"队列中,该队列中的消息,就是我们返回的值!
九、对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!!!