集百家之长,成一家之言。 

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

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.  
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.java</groupId>
  8. <artifactId>activemq</artifactId>
  9. <version>1.0.0</version>
  10.  
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.5.RELEASE</version>
  15. </parent>
  16.  
  17. <dependencies>
  18.  
  19. <!-- Spring Boot -->
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-activemq</artifactId>
  27. </dependency>
  28.  
  29. <!-- 热部署 -->
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>springloaded</artifactId>
  33. <version>1.2.8.RELEASE</version>
  34. <scope>provided</scope>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-devtools</artifactId>
  39. <scope>provided</scope>
  40. </dependency>
  41.  
  42. </dependencies>
  43.  
  44. <build>
  45. <finalName>${project.artifactId}</finalName>
  46. <plugins>
  47. <plugin>
  48. <groupId>org.apache.maven.plugins</groupId>
  49. <artifactId>maven-compiler-plugin</artifactId>
  50. <configuration>
  51. <source>1.8</source>
  52. <target>1.8</target>
  53. <encoding>UTF-8</encoding>
  54. </configuration>
  55. </plugin>
  56.  
  57. <plugin>
  58. <groupId>org.springframework.boot</groupId>
  59. <artifactId>spring-boot-maven-plugin</artifactId>
  60. <executions>
  61. <execution>
  62. <goals>
  63. <goal>repackage</goal>
  64. </goals>
  65. </execution>
  66. </executions>
  67. </plugin>
  68. </plugins>
  69. </build>
  70. </project>

4、ActiveMQStarter.java

  1. package com.java;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
  6. /**
  7. * 主启动类
  8. *
  9. * @author Logan
  10. * @version 1.0.0
  11. * @createDate 2019-05-08
  12. *
  13. */
  14. @SpringBootApplication
  15. public class ActiveMQStarter {
  16.  
  17. public static void main(String[] args) {
  18. SpringApplication.run(ActiveMQStarter.class, args);
  19. }
  20.  
  21. }

5、SendMessageController.java

  1. package com.java.controller;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.jms.annotation.JmsListener;
  5. import org.springframework.jms.core.JmsMessagingTemplate;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. /**
  10. * 发送消息类
  11. *
  12. * @author Logan
  13. * @version 1.0.0
  14. * @createDate 2019-05-08
  15. *
  16. */
  17. @RestController
  18. public class SendMessageController {
  19.  
  20. @Autowired
  21. private JmsMessagingTemplate jmsMessagingTemplate;
  22.  
  23. /**
  24. * 发送到的队列名
  25. */
  26. private String destinationName = "handle-queue";
  27.  
  28. @GetMapping("/send")
  29. public String send(String params) {
  30. System.out.println("[ 收到请求 ]");
  31.  
  32. jmsMessagingTemplate.convertAndSend(destinationName, params);
  33.  
  34. System.out.println("[ 返回响应 ]");
  35. return "您的任务已提交";
  36. }
  37.  
  38. @JmsListener(destination = "result-queue") // 监听处理结果队列
  39. public void handle(String message) {
  40. System.out.println("[ 收到消息处理结果 ]" + System.currentTimeMillis());
  41.  
  42. System.out.println(message);
  43. }
  44.  
  45. }

6、MessageHandler.java

  1. package com.java.listener;
  2.  
  3. import org.springframework.jms.annotation.JmsListener;
  4. import org.springframework.messaging.handler.annotation.SendTo;
  5. import org.springframework.stereotype.Component;
  6.  
  7. /**
  8. * 任务处理器,监听ActiveMQ队列中的消息,消费并处理
  9. *
  10. * @author Logan
  11. * @version 1.0.0
  12. * @createDate 2019-05-08
  13. *
  14. */
  15. @Component
  16. public class MessageHandler {
  17.  
  18. @SendTo("result-queue") // 处理结果发送到 "result-queue" 队列中
  19. @JmsListener(destination = "handle-queue") // 监听处理消息队列
  20. public String handle(String message) {
  21. System.out.println("[ 处理器开始处理消息 ]" + System.currentTimeMillis());
  22.  
  23. try {
  24. Thread.sleep(5000);
  25. } catch (InterruptedException e) {
  26. e.printStackTrace();
  27. }
  28.  
  29. System.out.println(message);
  30.  
  31. System.out.println("[ 处理器处理消息完成 ]" + System.currentTimeMillis());
  32.  
  33. return "消息“" + message + "”处理完成";
  34. }
  35.  
  36. }

7、application.properties

  1. spring.activemq.broker-url=tcp://127.0.0.1:61616
  2. spring.activemq.user=admin
  3. spring.activemq.password=admin
  4. spring.activemq.in-memory=true
  5. spring.activemq.pool.enabled=false

8、启动

运行 activemq.bat 启动ActiveMQ 服务

运行ActiveMQStarter.java 启动项目

9、测试

浏览器输入 http://127.0.0.1:8080/send?params=Hello

浏览器快速收到响应 “您的任务已提交”,控制台日志如下:

  1. [ 收到请求 ]
  2. [ 处理器开始处理消息 ]1557311835328
  3. [ 返回响应 ]
  4. Hello
  5. [ 处理器处理消息完成 ]1557311840353
  6. [ 收到消息处理结果 ]1557311840369
  7. 消息“Hello”处理完成

双向通信正常!

查看消息队列服务

浏览器输入 http://127.0.0.1:8161

登录 admin/admin

可查看所有队列以及生产者和消费者情况

截图如下:

Spring boot 集成ActiveMQ(包含双向队列实现)

.

Spring boot 集成ActiveMQ(包含双向队列实现)的更多相关文章

  1. spring boot集成activemq

    spring boot集成activemq 转自:https://blog.csdn.net/maiyikai/article/details/77199300

  2. 86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】

    在Spring Boot中集成ActiveMQ相对还是比较简单的,都不需要安装什么服务,默认使用内存的activeMQ,当然配合ActiveMQ Server会更好.在这里我们简单介绍怎么使用,本节主 ...

  3. Spring Boot与ActiveMQ的集成

    Spring Boot对JMS(Java Message Service,Java消息服务)也提供了自动配置的支持,其主要支持的JMS实现有ActiveMQ.Artemis等.本节中,将以Active ...

  4. 如何集成 Spring Boot 和 ActiveMQ?

    对于集成 Spring Boot 和 ActiveMQ,我们使用依赖关系. 它只需要很少的配置,并且不需要样板代码.

  5. 【ActiveMQ】Spring Jms集成ActiveMQ学习记录

    Spring Jms集成ActiveMQ学习记录. 引入依赖包 无论生产者还是消费者均引入这些包: <properties> <spring.version>3.0.5.REL ...

  6. Spring boot集成Rabbit MQ使用初体验

    Spring boot集成Rabbit MQ使用初体验 1.rabbit mq基本特性 首先介绍一下rabbitMQ的几个特性 Asynchronous Messaging Supports mult ...

  7. 在Spring下集成ActiveMQ

    1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...

  8. Spring boot集成swagger2

    一.Swagger2是什么? Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格 ...

  9. spring boot 集成 zookeeper 搭建微服务架构

    PRC原理 RPC 远程过程调用(Remote Procedure Call) 一般用来实现部署在不同机器上的系统之间的方法调用,使得程序能够像访问本地系统资源一样,通过网络传输去访问远程系统资源,R ...

随机推荐

  1. css样式也技巧

    目录 关于iPhone的点击事件绑定无效的处理方法 https://blog.csdn.net/u014477038/article/details/52527194 去掉a.button.input ...

  2. uva11357 Matches

    Matches UVA - 11375 题意: 给你n根matches, 你可以拼出多少个数字0~9. 不必全部用完. 解题思路: 1. 计数题, 本题可以用图来理解. 把"已经使用了i根m ...

  3. MySQL审计工具Audit Plugin安装使用

    本实验的审计插件均是安装在 mysql-community-server-5.7.9 的服务器上. 插件安装(社区版) 插件下载地址: https://bintray.com/mcafee/mysql ...

  4. Vue培训项目总结

    昨天是最后一次给同事进行Vue的培训,这次培训主要是以基础入门为主. 整篇讲义参考了一些文章:https://gitbook.cn/gitchat/column/5a4af1c5658b7c0d9eb ...

  5. SPA单页应用前后分离微信授权

    项目基于微信公众号开发,业务完全依赖微信授权,也就是用户进入页面已经完成授权获取到用户的OpenId. 需要有一个授权中间页:author.vue 基本实现思路: 无论使用哪个url进入页面都会先触发 ...

  6. 关于Struts漏洞工具的使用

    最新struts-scan资源: https://www.cesafe.com/3486.html 一,将资源下载后,放入liunx系统中,并且需要具备python2的操作环境 二,打开终端使用如下命 ...

  7. P1308-道路修建 (noi 2011)

    题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1 条双向道路. 每条道 ...

  8. sql server添加sa用户和密码

    昨天给网站“搬家”(更换服务器),我是在win7上安装的 sql server2012,安装过程很顺利,用“Windows 身份验证” 也可正常访问.但是用sa用户访问数据库出现了 错误:18456. ...

  9. maven 参考

    系列文章,通俗易懂,可以看看 http://www.cnblogs.com/AlanLee/category/918828.html

  10. Java工程打包成jar可执行文件

    将一个工程中的类打包成jar文件,步骤参考如下: 1.选择file -> project structure 2. 选择Arifacts->JAR->form modules wit ...