集百家之长,成一家之言。
1、 下载ActiveMQ
https://mirrors.tuna.tsinghua.edu.cn/apache/activemq/5.15.9/apache-activemq-5.15.9-bin.zip
2、新建 Maven 项目 activemq
3、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java</groupId> <artifactId>activemq</artifactId> <version>1.0.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <!-- 热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.8.RELEASE</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
4、ActiveMQStarter.java
package com.java; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 主启动类 * * @author Logan * @version 1.0.0 * @createDate 2019-05-08 * */ @SpringBootApplication public class ActiveMQStarter { public static void main(String[] args) { SpringApplication.run(ActiveMQStarter.class, args); } }
5、SendMessageController.java
package com.java.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.annotation.JmsListener; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * 发送消息类 * * @author Logan * @version 1.0.0 * @createDate 2019-05-08 * */ @RestController public class SendMessageController { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; /** * 发送到的队列名 */ private String destinationName = "handle-queue"; @GetMapping("/send") public String send(String params) { System.out.println("[ 收到请求 ]"); jmsMessagingTemplate.convertAndSend(destinationName, params); System.out.println("[ 返回响应 ]"); return "您的任务已提交"; } @JmsListener(destination = "result-queue") // 监听处理结果队列 public void handle(String message) { System.out.println("[ 收到消息处理结果 ]" + System.currentTimeMillis()); System.out.println(message); } }
6、MessageHandler.java
package com.java.listener; import org.springframework.jms.annotation.JmsListener; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.stereotype.Component; /** * 任务处理器,监听ActiveMQ队列中的消息,消费并处理 * * @author Logan * @version 1.0.0 * @createDate 2019-05-08 * */ @Component public class MessageHandler { @SendTo("result-queue") // 处理结果发送到 "result-queue" 队列中 @JmsListener(destination = "handle-queue") // 监听处理消息队列 public String handle(String message) { System.out.println("[ 处理器开始处理消息 ]" + System.currentTimeMillis()); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(message); System.out.println("[ 处理器处理消息完成 ]" + System.currentTimeMillis()); return "消息“" + message + "”处理完成"; } }
7、application.properties
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
8、启动
运行 activemq.bat 启动ActiveMQ 服务
运行ActiveMQStarter.java 启动项目
9、测试
浏览器输入 http://127.0.0.1:8080/send?params=Hello
浏览器快速收到响应 “您的任务已提交”,控制台日志如下:
[ 收到请求 ]
[ 处理器开始处理消息 ]1557311835328
[ 返回响应 ]
Hello
[ 处理器处理消息完成 ]1557311840353
[ 收到消息处理结果 ]1557311840369
消息“Hello”处理完成
双向通信正常!
查看消息队列服务
浏览器输入 http://127.0.0.1:8161
登录 admin/admin
可查看所有队列以及生产者和消费者情况
截图如下:
Spring boot 集成ActiveMQ(包含双向队列实现)
.