原文链接: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消息的更多相关文章

  1. SpringBoot配置ActiveMQ

    1.添加依赖 <!-- activeMQ --> <dependency> <groupId>org.springframework.boot</groupI ...

  2. 解决Springboot整合ActiveMQ发送和接收topic消息的问题

    环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...

  3. activemq安装运行及其在springboot中的queue和topic使用

    安装activemq 运行 springboot使用 依赖 配置 Producer Consumer ComsumerTopic 使用 安装activemq http://activemq.apach ...

  4. SpringBoot整合ActiveMq实现Queue和Topic两种模式(看不懂你来打我)

    目录 一.前言 二.ActiveMq的下载和使用 三.依赖准备 四.yml文件配置 五.配置Bean 六.创建生产者(Queue+Topic) 七.创建消费者(Topic模式下) 八.测试结果(Top ...

  5. ActiveMQ的queue以及topic两种消息处理机制分析

    1    queue与topic的技术特点对比 对比项 Topic Queue 概要 Publish Subscribe messaging 发布订阅消息 Point-to-Point 点对点 有无状 ...

  6. ActiveMQ——activemq的详细说明,queue、topic的区别(精选)

    JMS中定义了两种消息模型:点对点(point to point, queue)和发布/订阅(publish/subscribe,topic).主要区别就是是否能重复消费. 点对点:Queue,不可重 ...

  7. ActiveMQ queue和topic,持久订阅和非持久订阅

    消息的 destination 分为 queue 和 topic,而消费者称为 subscriber(订阅者).queue 中的消息只会发送给一个订阅者,而 topic 的消息,会发送给每一个订阅者. ...

  8. 【ActiveMQ入门-5】ActiveMQ学习-Queue与Topic的比较

    Queue与Topic的比较 1.JMS Queue执行load balancer语义: 一条消息仅能被一个consumer收到. 如果在message发送的时候没有可用的consumer,那么它将被 ...

  9. SpringBoot JMS(ActiveMQ) 使用实践

    ActiveMQ 1. 下载windows办的activeMQ后,在以下目录可以启动: 2. 启动后会有以下提示 3. 所以我们可以通过http://localhost:8161访问管理页面,通过tc ...

随机推荐

  1. 如何安全的从LVM中移除磁盘

        学习如何安全的从LVM中移除磁盘,当磁盘卷中有磁盘出现问题或是想把磁盘卷中的磁盘重新使用时就显得十分有用.本教程将重点关注以下问题: 如何安全的从LVM中移除磁盘 如何联机从VG中移除磁盘 如 ...

  2. Codeforces Round #589 (Div. 2)

    目录 Contest Info Solutions A. Distinct Digits B. Filling the Grid C. Primes and Multiplication D. Com ...

  3. 从零开始实现SSD目标检测(pytorch)(一)

    目录 从零开始实现SSD目标检测(pytorch) 第一章 相关概念概述 1.1 检测框表示 1.2 交并比 第二章 基础网络 2.1 基础网络 2.2 附加网络 第三章 先验框设计 3.1 引言 3 ...

  4. 链家网爬虫同步VS异步执行时间对比

    异步执行时间 import time import asyncio import aiohttp from lxml import etree start_time = time.time() asy ...

  5. CentOS7 升级Python2.x到3.x

    CentOS 7 中默认安装了 Python,版本比较低(2.7.5),为了使用新版 3.x,需要对旧版本进行升级.由于很多基本的命令.软件包都依赖旧版本,比如:yum.所以,在更新 Python 时 ...

  6. meshing-轴

    原视频下载地址:https://yunpan.cn/cqrJRm32dMmAL  访问密码 9dd9

  7. 【2018.07.27】(字符串/找相同)学习KMP算法小记

    虽然说原理很好理解,但是代码理解了花费我一个下午的时间,脑阔痛 该注释的地方都标记了,希望以后看到这些代码我还能好好理解吧 学习的链接地址:https://www.cnblogs.com/teble/ ...

  8. PyTricks-函数参数的解包操作

    def myfunc(x, y, z): print(x, y, z) tuple_vec = (1, 0, 1) dict_vec = {'x': 1, 'y': 0, 'z': 1} >&g ...

  9. selenium反爬机制

    使用selenium模拟浏览器进行数据抓取无疑是当下最通用的数据采集方案,它通吃各种数据加载方式,能够绕过客户JS加密,绕过爬虫检测,绕过签名机制.它的应用,使得许多网站的反采集策略形同虚设.由于se ...

  10. 实现一个简单的Tomcat

    实现一个简单的Tomcat 1. Tomcat作用 我们的web应用会运行在Tomcat中,那么显然请求必定是先到达Tomcat的,Tomcat对于请求实际上会进行如下的处理: 提供Socket服务: ...