解决Springboot整合ActiveMQ发送和接收topic消息的问题
环境搭建
1.创建maven项目(jar)
2.pom.xml添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</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-activemq</artifactId>
</dependency>
</dependencies>
3.编写引导类
@SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
4.在resources下的application.properties配置文件中添加对应的配置
spring.activemq.broker-url=tcp://192.168.25.131:61616
#此url为activeMQ所在服务器的链接
5.在类中注入JmsMessageTemplate
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
6.调用JmsMessageTemplate的方法发送消息
jmsMessagingTemplate.convertAndSend(“queue消息名称”,"消息内容");
设置消息发送类型
在引导类中配置消息类型
Queue
@Bean(name="queue")
public Destination getQueue(){
return new ActiveMQQueue("queue_test");
}
Topic
@Bean(name="topic")
public Destination getTopic(){
return new ActiveMQTopic("topic_test");
}
类中注入发送消息类型
在发送消息的类中注入发送消息类型对象Destination
Queue
@Resource(name="queue")
private Destination queue;
Topic
@Autowired
@Qualifier(value="topic")
private Destination topic;
消息发送
jmsMessageTemplate.convertAndSend(queue,"消息内容");
jmsMessageTemplate.convertAndSend(topic,"消息内容");
编写消费者
@Component
public class ActiveMQConsumer {
//接收queue消息
@JmsListener(destination = "queue_test")
public void handler(String message){
System.out.println(message);
}
//接收topic消息
@JmsListener(destination = "topic_test")
public void handlerTopic(String msessage){
System.out.println(msessage);
}
}
启动测试
controller类的方法中添加
@RequestMapping("/send")
public void sendQueue(){
jmsMessagingTemplate.convertAndSend(queue,"这是Queue的消息");
jmsMessagingTemplate.convertAndSend(topic,"这是Topic的消息");
}
运行后,在控制台只输出了Queue的消息

问题
Springboot整合ActiveMQ模式只能监听Queue队列的消息进行处理,所以如何处理topic消息?
解决:
在Springboot的application.properties文件中添加如下内容
spring.jms.pub-sub-domain=true //默认是false,开启发布订阅模式
启动测试

经过上述修改,又只能监听topic的消息,queue的消息又无法获取。
解决:
只有通过自定义监听器类来处理
在监听器类的@JmsListener添加connectionFactory属性
@Component
public class ActiveMQConsumer {
//接收queue消息
@JmsListener(destination = "queue_test",containerFactory =
"queueListenerContainerFactory")
public void handler(String message){
System.out.println(message);
}
//接收topic消息
@JmsListener(destination = "topic_test",containerFactory =
"topicListenerContainerFactory")
public void handlerTopic(String msessage){
System.out.println(msessage);
}
}
创建一个配置类,在配置类中提供两个监听工厂配置
@Configuration
public class ConsumerConfiguration { @Value("${spring.activemq.broker-url}")
private String host; @Bean
public ConnectionFactory getActiveMqConnection(){
return new ActiveMQConnectionFactory(host);
} @Bean(name="queueListenerContainerFactory")
public JmsListenerContainerFactory queueListenerContailerFactory(ConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(false);
return factory;
}
@Bean(name="topicListenerContainerFactory")
public JmsListenerContainerFactory topicListenerContainerFactory(ConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(true);
return factory;
}
}
运行测试

解决Springboot整合ActiveMQ发送和接收topic消息的问题的更多相关文章
- SpringBoot整合ActiveMq实现Queue和Topic两种模式(看不懂你来打我)
目录 一.前言 二.ActiveMq的下载和使用 三.依赖准备 四.yml文件配置 五.配置Bean 六.创建生产者(Queue+Topic) 七.创建消费者(Topic模式下) 八.测试结果(Top ...
- SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...
- ActiveMQ 笔记(四)Spring\SpringBoot 整合 Activemq
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.Spring 整合Activemq 1.所需jar包 <dependencies> &l ...
- Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ
集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Liste ...
- SpringBoot整合ActiveMQ快速入门
Spring Boot 具有如下特性: 为基于 Spring 的开发提供更快的入门体验 开箱即用,没有代码生成,也无需 XML 配置.同时也可以修改默认值来满足特定的需求. 提供了一些大型项目中常见的 ...
- SpringBoot整合ActiveMQ和开启持久化
一.点对点 1.提供者目录展示 2.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...
- 2.技巧: 用 JAXM 发送和接收 SOAP 消息—Java API 使许多手工生成和发送消息方面必需的步骤自动化
转自:https://www.cnblogs.com/chenying99/archive/2013/05/23/3094128.html 技巧: 用 JAXM 发送和接收 SOAP 消息—Java ...
- (七)发送、接收SOAP消息(以HttpClient方式)(2)
一.为什么要用soap 原本我们使用web服务都是根据wsdl生成客户端(生成一堆java文件)然后再调用,本章节讲解如何用soap消息来替代这种方式. 二.SOAP消息格式 SOAP(简单对象访问协 ...
- SpringBoot整合ActiveMQ,看这篇就够了
ActiveMQ是Apache提供的一个开源的消息系统,完全采用Java来实现,因此它能很好地支持JMS(Java Message Service,即Java消息服务)规范:本文将详细介绍下Activ ...
随机推荐
- node调试工具--nodemon使用简介
这个工具和node-supervisor基本上是一致的,但是其功能比较强大,个人觉得在开发环境还是用 nodemon,因为配置比较方便,文档也很清晰.所以这里先主要讲 nodemon. nodemon ...
- 基于C#的机器学习--微基准测试和激活功能
本章我们将学习以下内容: l 什么是微基准测试 l 如何将它应用到代码中 l 什么是激活函数 l 如何绘制和基准测试激活函数 每个开发人员都需要有一个好的基准测试工具.质量基准无处不在;你们每 ...
- 【bfs】单向公路-C++
描述 某地区有许多城镇,但并不是每个城镇都跟其他城镇有公路连接,并且有的公路并不能双向行驶.现在我们把这些城镇间的公路分布及允许的行驶方向告诉你,你需要编程解决通过公路是否可以从一个城镇到达另一个城镇 ...
- 【机器学习理论】换底公式--以e,2,10为底的对数关系转化
我们在推导机器学习公式时,常常会用到各种各样的对数,但是奇怪的是--我们往往会忽略对数的底数是谁,不管是2,e,10等. 原因在于,lnx,log2x,log10x,之间是存在常数倍关系. 回顾学过的 ...
- vue2.0 富文本组件(基于wangeditor)
1. 本组件基于 wangeditor http://www.wangeditor.com/ 如有侵权 请告知, 2. 效果图 3. 依赖安装 package.json 中 devDependenci ...
- MapReduce之提交job源码分析 FileInputFormat源码解析
MapReduce之提交job源码分析 job 提交流程源码详解 //runner 类中提交job waitForCompletion() submit(); // 1 建立连接 connect(); ...
- [PTA] 数据结构与算法题目集 6-3 求链式表的表长
Length( List L ){ int res=0; while(L!=NULL){ res++; L=L->Next; } return res; }
- .Net微信网页开发之使用微信JS-SDK自定义微信分享内容
第一步.微信JS-SDK的使用步骤,配置信息的生成获取讲解: 关于JS-SDK的使用步骤和timestamp(时间戳),nonceStr(随机串),signature(签名),access_token ...
- python UUID
UUID介绍 UUID是128位的全局唯一标识符,通常由32字节的字符串表示.它可以保证时间和空间的唯一性,也称为GUID,全称为:UUID ―― Universally Unique IDentif ...
- java - 解释内存中的栈(stack)、堆(heap)和方法区(method area)的用法
通常我们定义一个基本数据类型的变量,一个对象的引用,还有就是函数调用的现场保存都使用JVM中的栈空间: 而通过new关键字和构造器创建的对象则放在堆空间,堆是垃圾收集器管理的主要区域,由于现在的垃圾收 ...