spring boot Activitymq topic与 queue 并存实现方案

Activitymq topic与 queue 并存实现方案

问题:

根据官方的事例文档,配置,我们发现按照该方案,发现要么实现topic 要么 实现queue,不能同时出现,因为他只是一个配置参数,结果值为true或是false。但是我们的实际使用场景,很可能会出现一个服务即需要队列,又需要订阅。本文档则着重解决这个问题。

 

 

一、引入activity_sample_activitymq

二、修改SampleActiveMQApplication

删除原有的queue方法,


替换为queue和topic的工厂

       @Bean

       publicJmsListenerContainerFactory topicListenerFactory(ConnectionFactory connectionFactory,

                     DefaultJmsListenerContainerFactoryConfigurerconfigurer) {

              DefaultJmsListenerContainerFactoryfactory = new DefaultJmsListenerContainerFactory();

              //为true 则是 topic 不写则为queue

              factory.setPubSubDomain(true);

              configurer.configure(factory, connectionFactory);

              return factory;

       }

 

       @Bean

       publicJmsListenerContainerFactory queueListenerFactory(ConnectionFactory connectionFactory,

                     DefaultJmsListenerContainerFactoryConfigurerconfigurer) {

              DefaultJmsListenerContainerFactoryfactory = new DefaultJmsListenerContainerFactory();

              configurer.configure(factory, connectionFactory);

              return factory;

       }

  

三、修改Consumer

删除原有的消费者方法,替换为:

 

// destination 监听的队列名, containerFactory                                                                                           使用的工厂

@JmsListener(destination ="mytopic", containerFactory = "topicListenerFactory")               

public void receiveTopicMessage(Stringcontext) {                                              

     System.out.println("Receivedtopic<" + context + ">");                                     

}                                                                                               

                                                                                               

                                                                                                  

@JmsListener(destination ="myqueue", containerFactory = "queueListenerFactory")               

public void receiveQueueMessage(Stringcontext) {                                              

     System.out.println("Receivedqueue<" + context + ">");                                      

}                                                                                              

                                                                                                  

四、修改消息生产者send方法

public void send(String msg) {                                                            

     //key值 为 监听器监听的 队列名  value 可以是任意类型,需要在consumer中定义对应的类型才可以接受                          

     this.jmsMessagingTemplate.convertAndSend("mytopic","topic");                         

     this.jmsMessagingTemplate.convertAndSend("myqueue","queue");                         

}                                                                                         

                                                                                          

 

 

五、结果

 

 

你可能感兴趣的:(activitymq)