Spring boot 集成ActiveMQ(包含双向队列实现)
集百家之长,成一家之言。
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(包含双向队列实现)
.
Spring boot 集成ActiveMQ(包含双向队列实现)的更多相关文章
- spring boot集成activemq
spring boot集成activemq 转自:https://blog.csdn.net/maiyikai/article/details/77199300
- 86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】
在Spring Boot中集成ActiveMQ相对还是比较简单的,都不需要安装什么服务,默认使用内存的activeMQ,当然配合ActiveMQ Server会更好.在这里我们简单介绍怎么使用,本节主 ...
- Spring Boot与ActiveMQ的集成
Spring Boot对JMS(Java Message Service,Java消息服务)也提供了自动配置的支持,其主要支持的JMS实现有ActiveMQ.Artemis等.本节中,将以Active ...
- 如何集成 Spring Boot 和 ActiveMQ?
对于集成 Spring Boot 和 ActiveMQ,我们使用依赖关系. 它只需要很少的配置,并且不需要样板代码.
- 【ActiveMQ】Spring Jms集成ActiveMQ学习记录
Spring Jms集成ActiveMQ学习记录. 引入依赖包 无论生产者还是消费者均引入这些包: <properties> <spring.version>3.0.5.REL ...
- Spring boot集成Rabbit MQ使用初体验
Spring boot集成Rabbit MQ使用初体验 1.rabbit mq基本特性 首先介绍一下rabbitMQ的几个特性 Asynchronous Messaging Supports mult ...
- 在Spring下集成ActiveMQ
1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...
- Spring boot集成swagger2
一.Swagger2是什么? Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格 ...
- spring boot 集成 zookeeper 搭建微服务架构
PRC原理 RPC 远程过程调用(Remote Procedure Call) 一般用来实现部署在不同机器上的系统之间的方法调用,使得程序能够像访问本地系统资源一样,通过网络传输去访问远程系统资源,R ...
随机推荐
- List Control控件中及时捕获checkbox被选中的消息的解决方案
转自:http://blog.csdn.net/vycode/article/details/7345073 我的功能需求是:用户可以在List Control里添加item,当无选项被选中(即Che ...
- 3d全景图
http://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Aggarwal_Panoramic_Stereo_Videos_CV ...
- 2017-9-22 NOIP模拟赛[xxy][数论]
XXY 的 的 NOIP 模拟赛 4 4 —— 数学专场 A Description定义 f(x)表示 x 的约数和,例:f(12)=1+2+3+4+6+12=28给出 x,y,求Σf(i),i∈[x ...
- UIActionSheet的最后一项点击失效
在开发过程中,发现有时候UIActionSheet的最后一项点击失效,点最后一项的上半区域时有效,这是在特定情况下才会发生,这个场景就是试用了UITabBar的时候才有.解决办法: 在showView ...
- Java基础笔记(二)——配置环境变量
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 到此处下载jdk,并安装.(选 ...
- Python-14-抽象及关键字参数
可使用内置函数callable判断某个对象是否可调用 >>> import math >>> x = 1 >>> y = math.sqrt &g ...
- WEB安全字体(Web Safe Fonts)-网页设计用什么字体兼容性好?
效果:http://sandbox.runjs.cn/show/qgdljvh4 1 Arial微软公司的网页核心字体之一,最常用的sans serif字体,当字号很小时不容易阅读.但是,大写的“I” ...
- JSPs only permit GET POST or HEAD的解决方案(REST风格)
问题:原文链接 https://blog.csdn.net/tiberroot/article/details/76615727 看到很多人解决办法使用 @ResponseBody注解 这个意思是按照 ...
- Linux —— shell认识与基础命令
shell 基础 shell路径: /etc/shells 系统shell版本: $SHELL 在父shell中可以调用子shell echo 把指定内容输出到屏幕上 操作选项: -e: 支持反斜杠控 ...
- atcoder square869120Contest#3 F 寿司
省选round1的时候dalao的推荐——atcoder的题目码量不大,但很巧妙,题目比较难找,挂个链冷静一下:http://s8pc-3.contest.atcoder.jp/tasks/s8pc_ ...