一、引入maven依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-activemq</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10.  
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-tomcat</artifactId>
  14. <scope>provided</scope>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-test</artifactId>
  19. <scope>test</scope>
  20. </dependency>
  21.  
  22. </dependencies>

二、配置application.properties

  1. # activemq
  2. spring.activemq.broker-url=tcp://localhost:61616
  3. spring.activemq.user=admin
  4. spring.activemq.password=admin
  5. #默认为true表示使用内存的activeMQ,不需要安装activeMQ server
  6. spring.activemq.in-memory=false
  7.  
  8. #如果此处设置为true,需要加如下的依赖包 <groupId>org.apache.activemq</groupId>
  9. # <artifactId>activemq-pool</artifactId> ,否则会自动配置失败,报JmsMessagingTemplate注入失败
  10. spring.activemq.pool.enabled=false
  • 注意value后面不能加空格,即spring.activemq.pool.enabled=false[空格]

三、在启动类中使用异步消息服务@EnableJms注解

  1. @EnableJms
  2. @SpringBootApplication
  3. public class SpringbootActiveMqApplication {
  4.  
  5. public static void main(String[] args) {
  6. SpringApplication.run(SpringbootActiveMqApplication.class, args);
  7. }
  8. }

四、编写消息生产者

  1. package com.shyroke.jms;
  2.  
  3. import javax.jms.Destination;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.jms.core.JmsMessagingTemplate;
  7. import org.springframework.stereotype.Service;
  8.  
  9. @Service("producer")
  10. public class Producer {
  11. @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
  12. private JmsMessagingTemplate jmsTemplate;
  13.  
  14. // 发送消息,destination是发送到的队列,message是待发送的消息
  15. public void sendMessage(Destination destination, final String message) {
  16. jmsTemplate.convertAndSend(destination, message);
  17. }
  18. }

五、编写两个消费者

  • 消费者一
  1. package com.shyroke.jms;
  2.  
  3. import org.springframework.jms.annotation.JmsListener;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Component
  7. public class Consumer {
  8. // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
  9. @JmsListener(destination = "mytest.queue")
  10. public void receiveQueue(String text) {
  11. System.out.println("Consumer收到的报文为:"+text);
  12. System.out.println("=================");
  13. }
  14. }
  • 消费者二
  1. package com.shyroke.jms;
  2.  
  3. import org.springframework.jms.annotation.JmsListener;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Component
  7. public class Consumer2 {
  8. // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
  9. @JmsListener(destination = "mytest.queue")
  10. public void receiveQueue(String text) {
  11. System.out.println("Consumer2收到的报文为:"+text);
  12. }
  13. }

六、测试

  1. package com.shyroke;
  2.  
  3. import javax.jms.Destination;
  4.  
  5. import org.apache.activemq.command.ActiveMQQueue;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.test.context.SpringBootTest;
  10. import org.springframework.test.context.junit4.SpringRunner;
  11.  
  12. import com.shyroke.jms.Producer;
  13.  
  14. @RunWith(SpringRunner.class)
  15. @SpringBootTest
  16. public class SpringbootActiveMqApplicationTests {
  17.  
  18. @Autowired
  19. private Producer producer;
  20.  
  21. @Test
  22. public void contextLoads() {
  23.  
  24. Destination destination=new ActiveMQQueue("mytest.queue");
  25.  
  26. for(int i=0; i<5; i++){
  27. producer.sendMessage(destination, "生产者发送了消息"+i);
  28. }
  29.  
  30. }
  31.  
  32. }
  • 结果

(十七)SpringBoot之使用异步消息服务jms之ActiveMQ的更多相关文章

  1. JAVA消息服务JMS规范及原理详解

    JAVA消息服务JMS规范及原理详解 一.简介 JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应 ...

  2. 【转载】JAVA消息服务JMS规范及原理详解

    转载:https://www.cnblogs.com/molao-doing/articles/6557305.html 作者: moyun- 一.简介 JMS即Java消息服务(Java Messa ...

  3. ActiveMQ学习总结(5)——Java消息服务JMS详解

    JMS: Java消息服务(Java Message Service) JMS是用于访问企业消息系统的开发商中立的API.企业消息系统可以协助应用软件通过网络进行消息交互. JMS的编程过程很简单,概 ...

  4. 1,Java消息服务-JMS

    一,消息服务 消息服务指的是两个应用程序之间进行异步通信的API,它为标准消息协议和消息服务提供了一组通用接口,包括创建.发送.读取消息等,用于支持应用程序开发.在Java中,当两个应用程序使用JMS ...

  5. 消息服务jms

    jms是java消息服务的规范,也即接口,activemq是实现.队列模型和发布订阅模型很像,区别就是队列,多个消费者消费不同的消息(这些消费者整体消费完生产者生产的队列的消息),发布订阅模型是多个消 ...

  6. 消息中间件--ActiveMQ&JMS消息服务

    ### 消息中间件 ### ---------- **消息中间件** 1. 消息中间件的概述 2. 消息中间件的应用场景 * 异步处理 * 应用解耦 * 流量削峰 * 消息通信   --------- ...

  7. 新鲜出炉,这是全网讲的最详细的springboot整合消息服务了吧,建议收藏!

    springboot整合activeMq ActiveMq是Apache提供的开源消息系统采用java实现, 很好地支持JMS(Java Message Service,即Java消息服务) 规范 A ...

  8. Spring消息之JMS.

    一.概念 异步消息简介 与远程调用机制以及REST接口类似,异步消息也是用于应用程序之间通信的. RMI.Hessian.Burlap.HTTP invoker和Web服务在应用程序之间的通信机制是同 ...

  9. 从零讲解搭建一个NIO消息服务端

    本文首发于本博客,如需转载,请申明出处. 假设 假设你已经了解并实现过了一些OIO消息服务端,并对异步消息服务端更有兴趣,那么本文或许能带你更好的入门,并了解JDK部分源码的关系流程,正如题目所说,笔 ...

随机推荐

  1. qt mvc2

    继续上次的例子,对于list才说只有行,讨论列是没有意义的. bool insertRows(int row, int count, const QModelIndex &parent); b ...

  2. RVS PA-1800 功放参数

        RVS PA-1800大功率功放技术参数:     文章来源:外星人来地球 欢迎关注,有问题一起学习欢迎留言.评论

  3. centos6.6 ftp 配置 修改默认端口等

    常规下21端口容易遭到别人的扫描.带来了一定程度的不安全.所以,最好的就是把21端口修改掉. 默认修改为6069 一.修改vsftp的配置文件 vi /etc/vsftpd/vsftpd.conf 在 ...

  4. Pattern 和 Matcher

    作用:应用这个 Pattern 和 Matcher 可以完成字符串获取功能 使用: // 获取模式器对象 Pattern p = Pattern.compile("a*b") ; ...

  5. ABAP ole操作

    1.ole 如何保存和退出call method of sheetname 'saves' exporting #1 = filepath #2 = 1. call method of applica ...

  6. 【Leetcode_easy】633. Sum of Square Numbers

    problem 633. Sum of Square Numbers 题意: solution1: 可以从c的平方根,注意即使c不是平方数,也会返回一个整型数.然后我们判断如果 i*i 等于c,说明c ...

  7. react 生命周期函数的一些心得体会

    一.理论 组件本质上是状态机,输入确定,输出一定确定 生命周期的三个阶段,三者时间是不固定的,只是在逻辑上的分类: 二.初始化阶段: getDefaultProps:获取实例的默认属性(即使没有生成实 ...

  8. Docker Machine(十五)

    目录 一.Docker Machine 总览 1.Docker Engine VS Docker Machine 2.环境准备 二.安装 Docker Machine 1.Install Machin ...

  9. 用Python做一个简单的小游戏

    学习总是枯燥的,对于Python小白的我来讲,更是乏味的.为了提高学习的兴趣,今天我就来写一个小程序练练手. 数字猜谜游戏相信大家都不陌生,A给出最小值最大值,B写一个该范围内的数,A猜测写下的是多少 ...

  10. Flutter 踩坑之build函数返回了null

    今天遇到一个bug,内容都正常显示没问题,但是控制台里报错,如图: 翻译了下,说是函数不能返回空值,搜索了下,网上相同问题的是少写了个return,我检查了下也没发现少return的,后来突然发现if ...