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 ...
随机推荐
- JAVA企业级开发-BOM&DOM(03)
一.BOM对象介绍 BOM对象:Browser Object Model 浏览器对象模型.把整个浏览器当做一个对象来处理. 一个浏览器对象中又包含了其他的对象. 重点介绍:window.history ...
- Python服务Debian打包新思路
此文已由作者张耕源授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Debian 打包一直是比较冷僻的技术,大部分同学都不会接触到它. 但是我们 Debian 服务器上安装的各 ...
- unity2017分离动作
http://tsubakit1.hateblo.jp/entry/2015/06/01/235939 using UnityEngine; using UnityEditor; using Syst ...
- luogu P5358 [SDOI2019]快速查询【模拟(?)】
把有单点修改和查询的点离散进一个数组,然后单点修改直接改,记录一个修改时间t,维护一个sm表示这些离散的点的和,val表示出了离散点其他点的值,因为都是一样的所以只记录这一个值即可,记录ljlc为加法 ...
- IT兄弟连 JavaWeb教程 使用AJAX发送POST请求并获取响应
POST请求用于向服务器发送应该被保存的数据,因此POST请求天然比GET请求多需要一份需要被保存的数据.那么这些数据应该放在何处呢?毕竟,我们的open()方法接收的三个参数都没有合适的位置. 答案 ...
- 初识java线程(Thread)
<1>.概念问题 线程的状态:1.NEW : 没有start的线程 2.RUNNING :可运行线程,可能正在执行,也可能正在等待操作系统中的其他资源,比如cpu时间片 3.BlOCKED ...
- BZOJ 4236: JOIOJI map瞎搞
分别记录J,O,I,的个数 cnt[char][i] 表示处理到第i位,char的个数 显然当且仅当 cnt[J][i] - cnt[O][i] == cnt[J][j-1] - cnt[O][j-1 ...
- 转 event 'utl_file I/O':
The ASH report shows tables and data files with wait event 'utl_file I/O': CHANGES No changes. CAUSE ...
- Table行合并操作
此方法不可取,但几天心血 保留,已有新想法,稍后会出一个完善的Table行列合并方法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- webpack.config.js====entry入口文件的配置
1. 一般是采用对象语法: entry: { index: './src/default/js/index.js' }, https://webpack.css88.com/concepts/ent ...