解决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.js热部署代码,实现修改代码后自动重启服务方便实时调试
写PHP等脚本语言的时候,已经习惯了修改完代码直接打开浏览器去查看最新的效果.而Node.js 只有在第一次引用时才会去解析脚本文件,以后都会直接访问内存,避免重复载入,这种设计虽然有利于提高性能,却 ...
- python接口自动化(二十八)--html测试 报告——下(详解)
简介 五一小长假已经结束了,想必大家都吃饱喝足玩好了,那就继续学习吧.一天不学习,自己知道:两天不学习,对手知道:三天不学习,大家知道:一周不学习,智商输给猪.好了开个玩笑都逗大家一乐,但是想想还是有 ...
- Bzoj 1229: [USACO2008 Nov]toy 玩具 题解 三分+贪心
1229: [USACO2008 Nov]toy 玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 338 Solved: 136[Submit] ...
- Spring Cloud学习(一):Eureka服务注册与发现
1.Eureka是什么 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的. Eureka ...
- 【题解】旅行-C++
Description 某趟列车的最大载客容量为V人,沿途共有n个停靠站,其中始发站为第1站,终点站为第n站.在第1站至第n-1站之 间,共有m个团队申请购票搭乘,若规定:(1)对于某个团队的购票申请 ...
- 【分治】黑白棋子的移动-C++
题目描述 有2n个棋子(n≥4)排成一行,开始为位置白子全部在左边,黑子全部在右边,如下图为n=5的情况: ○○○○○●●●●● 移动棋子的规则是:每次必须同时移动相邻的两个棋子,颜色不限,可以左移也 ...
- Java中的单例模式(Singleton Pattern in Java)
Introduction 对于系统中的某个类来说,只有一个实例是很重要的,比如只有一个timer和ID Producer.又比如在服务器程序中,配置信息保留在一个文件中,这些配置信息由一个单例对象统一 ...
- Amdahl定律理解
其中,a为并行计算部分所占比例,k为并行处理的个数. 当1-a=0时,(没有串行,只有并行)最大加速比s=n: 当a=0时,(只有串行,没有并行)最小加速比s=1: 当k→∞时,s → 1 /(1-a ...
- storm trident 的介绍与使用
一.trident 的介绍 trident 的英文意思是三叉戟,在这里我的理解是因为之前我们通过之前的学习topology spout bolt 去处理数据是没有问题的,但trident 的对spou ...
- java练习---4
//程序员:罗元昊 2017.9.17 今天好累吖咦吖咦吖 package demo;import java.util.Scanner;public class Lk { public static ...