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