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

字节拾贝
• 阅读 1484

一、前言

最近小编在学习消息队列,然后选中了ActiveMq,来进行学习.于是探索了好久,来整理一下自己的学习心得!大家一起学习,希望对你有用.我把一些我自己的理解写在注释里了注意看!!

二、ActiveMq的下载和使用

    <!-- activemq -->
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>

四、yml文件配置

spring:
 activemq:
  broker-url: tcp://127.0.0.1:61616
  user: admin
  password: admin
  jms:
   pub-sub-domain: true # 默认为false:queue   true:topic
queue: queue_mq # 点对点消费名字
topic: topic_mq # 订阅式消费名字

五、配置Bean

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;

import javax.jms.Queue;
import javax.jms.Topic;

@Configuration
@EnableJms
public class ActiveMqConfig {

    @Value("${queue}")//对应yml文件中定义的queue
    private String queue;

    @Value("${topic}")//对应yml文件中定义的topic
    private String topic;
    /**
     * 创建点对点的队列  一个消息只能被一个消费者消费  --- 一对一
     * @return
     */
    @Bean
    public Queue queue(){
        return new ActiveMQQueue(queue);
    }
    /**
     * 创建订阅式的队列  一个消息可以被多个消费者消费 --- 一对多
     * @return
     */
    @Bean
    public Topic topic(){
        return new ActiveMQTopic(topic);
    }
}

六、创建生产者(Queue+Topic)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;
import javax.jms.Topic;

@RestController
public class ProducerController {

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 点对点的消息队列的生产者
     * @param string
     */
    @GetMapping("/queue")
    public void sendMsgQueue(@RequestParam String string){
        System.out.println("消息已经发送,准备被消费,消息为 ---> "+string);
        jmsMessagingTemplate.convertAndSend(queue,string);
    }

    /**
     * 一对多的消息队列的生产者
     * @param string
     */
    @GetMapping("/topic")
    public void sendMsgTopic(@RequestParam String string){
        System.out.println("消息已经发送,准备被消费,消息为 ---> "+string);
        jmsMessagingTemplate.convertAndSend(topic,string);
    }

}

七、创建消费者(Topic模式下)

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class TopicConsumer {
    
    /**
     * 监听消息,名字为生产者发送的名字,要一致,不然监听不到.
     * 因为是订阅者模式,可以有多个消费者,我们这里举两个来进行测试
     * @param string
     */
    @JmsListener(destination = "${topic}")
    public void consumerTopicOne(String string){

        System.out.println("我是消费者一号:消费消息成功,信息为---> "+string);

    }

    @JmsListener(destination = "${topic}")
    public void consumerTopicTwo(String string){

        System.out.println("我是消费者二号:消费消息成功,信息为---> "+string);

    }
}

八、测试结果(Topic模式下)

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

九、ActiveMq网页版查看是否成功(Topic模式下)

网站地址 http://127.0.0.1:8161/admin/ 账号密码都是admin

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

十、创建消费者(Queue模式下)

首先把yml文件中的配置修改为Queue:pub-sub-domain: false
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class QueueConsumer {

    /**
     * 监听消息,名字为生产者发送的名字,要一致,不然监听不到.
     * 因为是队列模式,只能消费者
     * @param string
     */
    @JmsListener(destination = "${queue}")
    public void consumerQueue(String string){

        System.out.println("消费消息成功,信息为---> "+string);
    }

}

十一、测试结果(Queue模式下)

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

十二、ActiveMq网页版查看是否成功(Queue模式下)

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

十三、总结

这样我们就搭建好了,并且测试没有问题,有问题留言哦.比较合适刚刚学习的童鞋们,期待您的关注,一起学习,一起提高哦!!!
点赞
收藏
评论区
推荐文章
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
九路 九路
5年前
Swift版UITextView自定义占位词,最大长度
最近这段时间在搞一个Swift的项目,算是帮朋友做的吧,虽然有点累但是自己也是从中学到了很多东西,其中自己也封装了一些常用的控件,最近忙完公司的项目以后就整理一下自己的心得。。希望大家一起学习!最近项目需要,很多的地方都用到了UITextView来实现一些需求,需要设置占位词和最大的长度,这里我是简单的封装了一下,希望大家多多指正,话不多说,上代码:
Stella981 Stella981
4年前
React Hooks实现异步请求实例—useReducer、useContext和useEffect代替Redux方案
<blockquote本文是学习了2018年新鲜出炉的ReactHooks提案之后,针对<strong异步请求数据</strong写的一个案例。注意,本文假设了:<br1.你已经初步了解<codehooks</code的含义了,如果不了解还请移步<ahref"https://reactjs.org/docs/hooksintro.html
Wesley13 Wesley13
4年前
MQ 消息中间件梳理
!(https://oscimg.oschina.net/oscnet/c44a8479ca04449e89ed30c37058117a.jpg)_来源:_blog.csdn.net/qq\_29676623/article/details/85108070为什么使用消息队列?消息队列的优点和缺点?kafka、activemq
可莉 可莉
4年前
20200202 ActiveMQ 3. Java编码实现ActiveMQ通讯
ActiveMQ3.Java编码实现ActiveMQ通讯3.1.队列(Queue)目的地(Destination)分为:点对点的队列(Queue)一对多的主题(Topic)3.1.1.上手代码1.pom.xml
Wesley13 Wesley13
4年前
activemq 使用介绍
1,activemq分为queue和topic两种2,下面先介绍queue,使用spring集成build.gradlecompile"org.springframework:springjms:3.2.1.RELEASE"compile"org.apache.activemq:activemqcore
Stella981 Stella981
4年前
AvctiveMQ和RabbitMQ的区别
ActiveMQ:传统的消息队列,使用Java语言编写。基于JMS(JavaMessageService),采用多线程并发,资源消耗比较大。支持P2P和发布订阅两种模式。RabbitMQ:是使用Erlang语言开发的开源消息队列系统。基于AMQP协议来实现的。AMQP的主要特征是面向消息、队列、路由(
Wesley13 Wesley13
4年前
activeMQ入门+spring boot整合activeMQ
最近想要学习MOM(消息中间件:MessageOrientedMiddleware),就从比较基础的activeMQ学起,rabbitMQ、zeroMQ、rocketMQ、Kafka等后续再去学习。上面说activeMQ是一种消息中间件,可是为什么要使用activeMQ?在没有使用JMS的时候,很多应用会出现同步通信(客户端发起请求后需要等待服务
Stella981 Stella981
4年前
20200202 ActiveMQ 3. Java编码实现ActiveMQ通讯
ActiveMQ3.Java编码实现ActiveMQ通讯3.1.队列(Queue)目的地(Destination)分为:点对点的队列(Queue)一对多的主题(Topic)3.1.1.上手代码1.pom.xml
Python进阶者 Python进阶者
2年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这