一、引入maven依赖

    <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependencies>

二、配置application.properties

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

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

@EnableJms
@SpringBootApplication
public class SpringbootActiveMqApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootActiveMqApplication.class, args);
}
}

四、编写消息生产者

package com.shyroke.jms;

import javax.jms.Destination;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service; @Service("producer")
public class Producer {
@Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
private JmsMessagingTemplate jmsTemplate; // 发送消息,destination是发送到的队列,message是待发送的消息
public void sendMessage(Destination destination, final String message) {
jmsTemplate.convertAndSend(destination, message);
}
}

五、编写两个消费者

  • 消费者一
package com.shyroke.jms;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; @Component
public class Consumer {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text) {
System.out.println("Consumer收到的报文为:"+text);
System.out.println("=================");
}
}
  • 消费者二
package com.shyroke.jms;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; @Component
public class Consumer2 {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text) {
System.out.println("Consumer2收到的报文为:"+text);
}
}

六、测试

package com.shyroke;

import javax.jms.Destination;

import org.apache.activemq.command.ActiveMQQueue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import com.shyroke.jms.Producer; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootActiveMqApplicationTests { @Autowired
private Producer producer; @Test
public void contextLoads() { Destination destination=new ActiveMQQueue("mytest.queue"); for(int i=0; i<5; i++){
producer.sendMessage(destination, "生产者发送了消息"+i);
} } }
  • 结果

(十七)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. Postgresql使用coalesce实现类似oracle的NVL方法

    COALESCE (expression_1, expression_2, ...,expression_n)依次参考各参数表达式,遇到非null值即停止并返回该值. 如果所有的表达式都是空值,最终将 ...

  2. IIS URL Rewriting and ASP.NET Routing

    IIS URL Rewriting and ASP.NET Routing With the release of the URL Rewrite Module for IIS and the inc ...

  3. 你了解 Virtual DOM 吗?解释一下它的工作原理

    Virtual DOM 是一个轻量级的 JavaScript 对象,它最初只是 real DOM 的副本.它是一个节点树,它将元素.它们的属性和内容作为对象及其属性. React 的渲染函数从 Rea ...

  4. chrome浏览器备忘

    记录日常使用Chrome遇到的问题. audio控件播放音频问题 打开http://www.cdfive.com,发现音乐没有自动播放,F12打开控制台发现有如下报错: Uncaught (in pr ...

  5. 知乎千万级高性能长连接网关 https://zhuanlan.zhihu.com/p/66807833

    知乎千万级高性能长连接网关揭秘 9 天前 · 来自专栏 知乎技术专栏 实时的响应总是让人兴奋的,就如你在微信里看到对方正在输入,如你在王者峡谷里一呼百应,如你们在直播弹幕里不约而同的 666,它们的背 ...

  6. WhereHows编译时报错EINVRES Request to https://bower.herokuapp.com/packages/ace-builds failed with 502

    先说明一下,简单点讲就是bower的仓库地址换掉了.解决方案如下: 在.bowerrc文件中增加这么一句: { "registry": "https://registry ...

  7. 《高性能mysql》笔记(第一章,mysql的架构与历史)

    mysql的服务器逻辑架构图如下: 目前工作用的5.5版本,5.5版本开始mysql开始将innoDB作为默认的存储引擎,innoDB的表是基于聚簇索引建立的. mysql的存储引擎锁管理非常重要,在 ...

  8. Packetbeat简介

    Packetbeat简介 抓包示例 下载packetbeat 抓取elasticsearch的包 ①启动elasticsearch 启动packetbeat 配置es.yml ############ ...

  9. Mysql的安全配置向导命令mysql_secure_installation

    mysql_secure_installation安全配置向导 [root@youxi1 ~]# mysql_secure_installation Securing the MySQL server ...

  10. caffe dropout解读

    先上caffe dropout_layer.cpp源码,如下: // LayerSetUp DCHECK(threshold_ > 0.); DCHECK(threshold_ < 1.) ...