ActiveMq--Pub/Sub的使用
来源:互联网 发布:linux 守护进程程序 编辑:程序博客网 时间:2024/05/21 17:16
上一篇记录了ActiveMq的P2P模式的使用,这一篇主要记录ActiveMq的Pub/sub模式的使用。
针对ActiveMq的发布订阅模式,消息的发布必须针对特定的主题进行消息发布,同时为了消费消息,消息的订阅者必须一直处于运行状态。
如果想要发布的消息被多个、一个或者零个消费者使用,这个时候就因该考虑使用发布订阅模式。
下面开始代码演示整个发布订阅模式的实现过程,注意:本代码是使用SpringBoot和ActiveMq集成来实现消息的发布订阅的。要保证消息发布订阅的正常进行,需要在程序的主类上添加@EnableJms注解以使程序支持java的消息服务。
(1)消息队列配置类
package com.liutao.config;import com.liutao.enums.QueueName;import com.liutao.enums.TopicName;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 javax.jms.Queue;import javax.jms.Topic;/** * 消息队列配置类 * * @author LIUTAO * @version 2017/5/18 * @see * @since */@Configurationpublic class QueueConfig { @Bean public Queue queue(){ return new ActiveMQQueue(QueueName.FIRST_QUEUE.getValue()); } @Bean public Topic topic(){ return new ActiveMQTopic(TopicName.FIRST_TOPIC.getValue()); }}(2)消息的生产者
package com.liutao.producer;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.stereotype.Component;import javax.jms.Queue;import javax.jms.Topic;/** * 消息生产者 * * @author LIUTAO * @version 2017/5/18 * @see */@Componentpublic class FirstProducer{ private static final Logger logger = LoggerFactory.getLogger(FirstProducer.class); @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; @Autowired private Topic topic; public void send(String message){ this.jmsMessagingTemplate.convertAndSend(queue,message); this.jmsMessagingTemplate.convertAndSend(topic,message); }}(3)消息的消费者,为了掩饰一个消息发送到主题,这个主题的消息能够被多个消费者消费,我们创建两个消费者来消费同一个消息。
1、第一个消费者
package com.liutao.consumer;import com.liutao.enums.QueueName;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;/** * 消息消费者 * * @author LIUTAO * @version 2017/5/18 * @see * @since */@Componentpublic class FirstConsumer { private static final Logger logger = LoggerFactory.getLogger(FirstConsumer.class); @JmsListener(destination = "first_topic") public void receiveQueue(String message){ logger.debug("this is first consumer"); logger.debug("the message received from "+ QueueName.FIRST_QUEUE.getValue()+" is: "+message); }}2、第二个消费者
package com.liutao.consumer;import com.liutao.enums.QueueName;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;/** * 消息消费者 * * @author LIUTAO * @version 2017/5/18 * @see * @since */@Componentpublic class SecondConsumer { private static final Logger logger = LoggerFactory.getLogger(SecondConsumer.class); @JmsListener(destination = "first_topic") public void receiveQueue(String message){ logger.debug("this is second consumer"); logger.debug("the message received from "+ QueueName.FIRST_QUEUE.getValue()+" is: "+message); }}(4)测试Controller
package com.liutao.controller;import com.liutao.producer.FirstProducer;import com.wordnik.swagger.annotations.Api;import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;/** * ActiveMq测试 * * @author LIUTAO * @version 2017/5/18 * @see */@Controller@Api(value = "test activeMQ")@RequestMapping("/liutao/v1")public class ActiveMq { private Logger logger = LoggerFactory.getLogger(ActiveMq.class); @Autowired private FirstProducer firstProducer; @GetMapping("/activeMq") public @ResponseBody String activeMq(@RequestParam("message") String message) { logger.debug("enter test activeMq"); message = StringUtils.isEmpty(message) ? "this is a empty message" : message; firstProducer.send(message); return "OK"; }}运行程序后,进行相应的测试就可以看见,一个消费同时被两个消费者使用。
详细代码参考gitHub地址:ActiveMq--Pub/Sub
阅读全文
0 0
- ActiveMq--Pub/Sub的使用
- ActiveMQ入门教程(四) - ActiveMQ Pub/Sub版的HelloWorld
- ActiveMQ demos-Pub/Sub domain
- [ActiveMQ实战]基于JMS的pub/sub传播机制
- 基于ActiveMQ 的发布/订阅(Pub/Sub) Chat 示例,上传了源码
- Smack 实现的Pub/Sub
- Redis的Pub/Sub模式
- ActiveMQ两种模式PTP和PUB/SUB
- JMS实战——ActiveMQ实现Pub-Sub
- 一起来学activeMQ 5.pub/sub ,non-persistent/persistent,queue
- springmvc+spring+maven+ActiveMq之:PuB/Sub模式
- 0MQ PUB/SUB子系统的设计
- pub/sub模式的jquery插件
- Redis的发布/订阅(pub/sub)
- Zmq中pub和sub的用法
- nanomsg的pub/sub模式用法
- redis的pub/sub性能测试
- redis的pub与sub笔记
- 监控系统Nagios系列(四) 状态类型(soft & hard)
- Java-Socket通信
- 【备忘】科卡在线Linux高级应用视频教程(全套17集)视频下载
- openCV svm分类 基础
- 关联账户
- ActiveMq--Pub/Sub的使用
- hive原理与源码分析-物理执行计划与执行引擎(六)
- 负权边的最短路径--Bellman-Ford算法及其优化
- 好书分享(一)
- 基础入门:File相对路径
- 深入理解mybatis原理(四) MyBatis的二级缓存的设计原理
- POJ 3255 Roadblocks 最短路Dijkstra+堆优化
- poj 3723 最大生成树
- poj 2139 Six Degrees of Cowvin Bacon 最短路bellman 多源最短路径 (一次AC)