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 ...
随机推荐
- css样式也技巧
目录 关于iPhone的点击事件绑定无效的处理方法 https://blog.csdn.net/u014477038/article/details/52527194 去掉a.button.input ...
- uva11357 Matches
Matches UVA - 11375 题意: 给你n根matches, 你可以拼出多少个数字0~9. 不必全部用完. 解题思路: 1. 计数题, 本题可以用图来理解. 把"已经使用了i根m ...
- MySQL审计工具Audit Plugin安装使用
本实验的审计插件均是安装在 mysql-community-server-5.7.9 的服务器上. 插件安装(社区版) 插件下载地址: https://bintray.com/mcafee/mysql ...
- Vue培训项目总结
昨天是最后一次给同事进行Vue的培训,这次培训主要是以基础入门为主. 整篇讲义参考了一些文章:https://gitbook.cn/gitchat/column/5a4af1c5658b7c0d9eb ...
- SPA单页应用前后分离微信授权
项目基于微信公众号开发,业务完全依赖微信授权,也就是用户进入页面已经完成授权获取到用户的OpenId. 需要有一个授权中间页:author.vue 基本实现思路: 无论使用哪个url进入页面都会先触发 ...
- 关于Struts漏洞工具的使用
最新struts-scan资源: https://www.cesafe.com/3486.html 一,将资源下载后,放入liunx系统中,并且需要具备python2的操作环境 二,打开终端使用如下命 ...
- P1308-道路修建 (noi 2011)
题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1 条双向道路. 每条道 ...
- sql server添加sa用户和密码
昨天给网站“搬家”(更换服务器),我是在win7上安装的 sql server2012,安装过程很顺利,用“Windows 身份验证” 也可正常访问.但是用sa用户访问数据库出现了 错误:18456. ...
- maven 参考
系列文章,通俗易懂,可以看看 http://www.cnblogs.com/AlanLee/category/918828.html
- Java工程打包成jar可执行文件
将一个工程中的类打包成jar文件,步骤参考如下: 1.选择file -> project structure 2. 选择Arifacts->JAR->form modules wit ...