springboot使用activemq同时接收queue和topic消息
原文链接:https://blog.csdn.net/jia_costa/article/details/79354478
新建springboot项目, pom文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>activemq-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>activemq-test</name>
<description></description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <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-devtools</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
配置类
package com.example.config; import javax.jms.ConnectionFactory;
import javax.jms.Destination; import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory; @Configuration
public class ActivemqConfig { /**
* 自定义了4个Destination,两个queue,两个topic
*/
@Bean
public Destination queue1() {
return new ActiveMQQueue("queue-1");
} @Bean
public Destination queue2() {
return new ActiveMQQueue("queue-2");
} @Bean
public Destination topic1() {
return new ActiveMQTopic("topic-1");
} @Bean
public Destination topic2() {
return new ActiveMQTopic("topic-2");
} /**
* JmsListener注解默认只接收queue消息,如果要接收topic消息,需要设置containerFactory
*/
@Bean
public JmsListenerContainerFactory<?> topicListenerContainer(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory topicListenerContainer = new DefaultJmsListenerContainerFactory();
topicListenerContainer.setPubSubDomain(true);
topicListenerContainer.setConnectionFactory(activeMQConnectionFactory);
return topicListenerContainer;
} } controller package com.example.controller; import javax.annotation.Resource;
import javax.jms.Destination; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; /**
* 通过页面访问的方式生产消息
*/
@RestController
public class ProviderController { @Autowired
private JmsTemplate jmsTemplate; @Resource(name = "queue1")
private Destination queue1; @Resource(name = "topic1")
private Destination topic1; @GetMapping("/send/queue1")
public String send1() {
jmsTemplate.convertAndSend(queue1, "hello queue-1");
return "ok";
} @GetMapping("/send/topic1")
public String send2() {
jmsTemplate.convertAndSend(topic1, "hello topic-1");
return "ok";
} }
接收消息
package com.example.message; import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; /**
* 接收处理消息
*/
@Component
public class MessageHandler { @JmsListener(destination="queue-1")
public void recieve1(String message) {
System.out.println("###################" + message + "###################");
} @JmsListener(destination="topic-1", containerFactory="topicListenerContainer")
public void recieve2(String message) {
System.out.println("###################" + message + "###################");
} }
配置文件application.properties
spring.activemq.broker-url=tcp://192.168.32.128:61616
启动类
package com.example; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ActivemqTestApplication { public static void main(String[] args) {
SpringApplication.run(ActivemqTestApplication.class, args);
}
}
访问http://localhost:8080/send/queue1或者http://localhost:8080/send/topic1,可以看到控制台的输出
springboot使用activemq同时接收queue和topic消息的更多相关文章
- SpringBoot配置ActiveMQ
1.添加依赖 <!-- activeMQ --> <dependency> <groupId>org.springframework.boot</groupI ...
- 解决Springboot整合ActiveMQ发送和接收topic消息的问题
环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...
- activemq安装运行及其在springboot中的queue和topic使用
安装activemq 运行 springboot使用 依赖 配置 Producer Consumer ComsumerTopic 使用 安装activemq http://activemq.apach ...
- SpringBoot整合ActiveMq实现Queue和Topic两种模式(看不懂你来打我)
目录 一.前言 二.ActiveMq的下载和使用 三.依赖准备 四.yml文件配置 五.配置Bean 六.创建生产者(Queue+Topic) 七.创建消费者(Topic模式下) 八.测试结果(Top ...
- ActiveMQ的queue以及topic两种消息处理机制分析
1 queue与topic的技术特点对比 对比项 Topic Queue 概要 Publish Subscribe messaging 发布订阅消息 Point-to-Point 点对点 有无状 ...
- ActiveMQ——activemq的详细说明,queue、topic的区别(精选)
JMS中定义了两种消息模型:点对点(point to point, queue)和发布/订阅(publish/subscribe,topic).主要区别就是是否能重复消费. 点对点:Queue,不可重 ...
- ActiveMQ queue和topic,持久订阅和非持久订阅
消息的 destination 分为 queue 和 topic,而消费者称为 subscriber(订阅者).queue 中的消息只会发送给一个订阅者,而 topic 的消息,会发送给每一个订阅者. ...
- 【ActiveMQ入门-5】ActiveMQ学习-Queue与Topic的比较
Queue与Topic的比较 1.JMS Queue执行load balancer语义: 一条消息仅能被一个consumer收到. 如果在message发送的时候没有可用的consumer,那么它将被 ...
- SpringBoot JMS(ActiveMQ) 使用实践
ActiveMQ 1. 下载windows办的activeMQ后,在以下目录可以启动: 2. 启动后会有以下提示 3. 所以我们可以通过http://localhost:8161访问管理页面,通过tc ...
随机推荐
- umeditor+粘贴word图片
图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码 目前限chrome浏览器使用,但是项目要求需要支持所有的浏览器,包括Windows和macOS系统.没有办 ...
- JavaBean转Map
1.需要的jar包 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava ...
- zookeeper 随记
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务. zookeeper的几种模式: 1.单点模式 2.分布式集群模式,节点运行在多台机器 3.单点多实例 在这里只介绍单点多实例安装. ...
- 上传本地文件到github仓库
第一步:新建仓库 给仓库一个名字,备注 得到仓库地址: https://github.com/Lucasli2018/java-1-mybatis.git 第二步:进入要上传的文件夹,初始化上传文件夹 ...
- 十一月百度杯pwnme 详细wp
目录 程序基本信息 程序溢出点 整体思路 exp脚本 成功获得flag 参考 程序基本信息 可以看到开启了栈不可执行和got表不可写保护. 程序溢出点 在函数sub_400AF7中,v8可以读入0x1 ...
- sonar,jiar,xray,jenkins[cli] [sudoers]
curl -n -X POST http://52.83.39.59:8080'/job/CLA_SSO/buildWithParameters?token=11d710a8eac8012bea28b ...
- 分区工具parted的使用方法
一. parted的用途及说明 概括使用说明: parted用于对磁盘(或RAID磁盘)进行分区及管理,与fdisk分区工具相比,支持2TB以上的磁盘分区,并且允许调整分区的大小. ...
- oracle中的cursor
游标是SQL的一个内存工作区,由系统或用户以变量的形式定义.游标的作用就是用于临时存储从数据库中提取的数据块.在某些情况下,需要把数据从存放在磁盘的表中调到计算机内存中进行处理,最后将处理结果显示出来 ...
- SQL语句里合并两个select查询结果
SQL UNION 操作符UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 ...
- 让群辉支持DTS音轨
让群晖Video Station支持DTS音轨的方法原因:因版权问题,群晖Video Station默认不支持DTS音轨,因此默认不能播放使用DTS音轨的影片. 网上搜到好多解决办法,通常是让添加源h ...