您的位置:首页 > 编程语言 > Java开发

95. Spring Boot之ActiveMQ的topic和queue【从零开始学Spring Boot】

2017-02-14 17:08 435 查看
经网友意见,发布了此篇文章,在之前的一篇文章【72. Spring Boot集成ActiveMQ】我们介绍过spring boot怎么集成activeMQ,之前我们的例子中只是消息模型中的一种。

       JMS规范两种常用的消息模型:点对点(point  to point ,queue)和发布/订阅(publish/subscribe,topic)。

       点对点:消息生产者生产消息发布到queue中,然后消息消费者从queue中取出,并且消费消息。这里需要注意:消息被消费者消费以后,queue中不再有存储,所以消息消费者不可消费到已经被消费的消息。Queue支持存在多个消息消费者,但是对一个消息而言,只会有一个消费者可以消费。

       发布/订阅:消息生产者(发布)将消息发布到topic中,同时有多个消息消费者(订阅)消费该消息。和点对点方式不同,发布到topic的消息会被所有订阅者消费。

 

       在之前的【72. Spring Boot集成ActiveMQ】中我们的例子中很实用了queue的方式,也就是点对点的方式,那么怎么切换为发布/订阅的消息模型呢。好了,这就是我们本章好解决的重点,另外就是如何在一个工程中即使用 queue的方式也使用 topic的方式。

抛出了本章的问题,那么看看本章的大纲吧:

本章大纲 写道
(1)spring boot ActiveMQ之发布/订阅消息模型使用;

(2)spring boot ActiveMQ 之queue and topic ;
 

 

 

接下来我们看看具体怎么操作?

(1)spring boot ActiveMQ之发布/订阅消息模型使用;

       这里不就不重新搭建工程了,我们在【72. Spring Boot集成ActiveMQ】的博客中的基础上继续研究。

       首先我们对我们的消息生成进行一定的改造。

第一步在App.java中声明一个ActiveMQTopic,具体代码如下:

package com.kfit;
 
import javax.jms.Queue;
import javax.jms.Topic;
 
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
 
/**
 *
 * @author Angel --守护天使
 * @version v.0.1
 * @date 2016年8月23日
 */
@SpringBootApplication
public class App {
    @Bean
    public Queue queue() {
       return new ActiveMQQueue("sample.queue");
    }
   
    @Bean
    public Topic topic() {
       return new ActiveMQTopic("sample.topic");
    }
   
//  @Bean
//  public DefaultMessageListenerContainer jmsListenerContainerFactory() {
//     DefaultMessageListenerContainer  dmlc = new DefaultMessageListenerContainer();
//      dmlc.setPubSubDomain(true);
//      // Other configuration here
//      return dmlc;
//  }
   
    public static void main(String[] args)
{
       SpringApplication.run(App.class, args);
    }
}
       这里比之前多了一个topic()方法。

 

第二步就是改造消息生成者Producer,定义Topic类,在定时发送的时候发送到topic上,具体代码如下:

 

package com.kfit.demo;
 
import javax.jms.Queue;
import javax.jms.Topic;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
/**
 * 消息生产者.
 * @author Angel --守护天使
 * @version v.0.1
 * @date 2016年8月23日
 */
@Component
@EnableScheduling
public class Producer {
   
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
   
    @Autowired
    private Queue queue;
   
    @Autowired
    private Topic topic;
   
    @Scheduled(fixedDelay=3000)//每3s执行1次
    public void send() {
       //send queue.
       this.jmsMessagingTemplate.convertAndSend(this.queue, "hi,activeMQ");
       //send topic.
       this.jmsMessagingTemplate.convertAndSend(this.topic, "hi,activeMQ(topic)");
    }
   
}
 

第三步:定义消息消费者Consumer2,Consumer3,具体代码如下:

Consumer2:

package com.kfit.demo;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
 
/**
 * 消息消费者.
 * @author Angel --守护天使
 * @version v.0.1
 * @date 2016年8月23日
 */
@Component
public class Consumer2 {
    @JmsListener(destination = "sample.topic")
    public void receiveQueue(String text) {
       System.out.println("Consumer2="+text);
    }
}
Consumer3:

package com.kfit.demo;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
 
/**
 * 消息消费者.
 * @author Angel --守护天使
 * @version v.0.1
 * @date 2016年8月23日
 */
@Component
public class Consumer3 {
    @JmsListener(destination = "sample.topic")
    public void receiveQueue(String text) {
       System.out.println("Consumer3="+text);
    }
}
到这里进行测试不会看到我们想要的效果的,这个主要是因为默认情况下,spring boot的jms配置是queue的方式,所以我们需要进行指定为topic的方式。

 

第四步:配置消息模型为pub/sub方式,在application.properties添加如下配置信息:

spring.jms.pub-sub-domain=true

这里简单对这个配置说明下:如果为True,则是Topic;如果是false或者默认,则是queue。

       这时候重新启动App.java,可以看到如下的打印信息:

Consumer2=hi,activeMQ(topic)
Consumer2=hi,activeMQ(topic)
Consumer3=hi,activeMQ(topic)
Consumer2=hi,activeMQ(topic)
Consumer3=hi,activeMQ(topic)

       好了到这里我们就实现了发布/订阅的消息模式,但是我们会发现另外问题了,queue的好不像不能使用了,我们会想,如果只是单纯的使用消息模型的话,那么没有问题,通过配置文件配置就好了,但是如果想使用多种消息模型的话,那么怎么办呢?

 

(2)spring boot ActiveMQ 之queue and topic ;

 

       对于同时支持queue和topic目前还没找到完美的解决方案,现在的思路就是:定义过个JmsListenerContainerFactory去实现,后续博主会关注这个部分,然后有新的方案会在博客中进行更新发布。有更好的方案的博友们也可以给我留言,告知,感激不尽。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: