通过同一ActiveMQConnectionFactory消耗多个ActiveMQ队列 [英] Consume multiple ActiveMQ queues via the same ActiveMQConnectionFactory
问题描述
我的应用程序处理放置在单个ActiveMQ队列(名为"incoming.queue")上的传入消息.我有一个MessageListener处理消息,并且一切正常.我的Java配置如下:
My application processes incoming messages that are placed on a single ActiveMQ queue (named "incoming.queue"). I have a MessageListener which processes the messages, and all is working well. My Java config is below:
@Configuration
@ComponentScan(basePackages="uk.co.domain")
public class JmsConfig {
@Bean
public ActiveMQConnectionFactory connectionFactory() {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL("tcp://localhost:61616");
return activeMQConnectionFactory;
}
@Bean
public DefaultMessageListenerContainer jmsListenerContainer() {
DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
dmlc.setConnectionFactory(connectionFactory());
dmlc.setDestination(new ActiveMQQueue("incoming.queue"));
dmlc.setMessageListener(new QueueProcessor());
dmlc.setConcurrentConsumers(50);
return dmlc;
}
}
public class QueueProcessor implements MessageListener {
public void onMessage(Message message) {
// process the message
}
}
另一个部门正在进行上游更改,以使消息将在名为"high_priority.queue","med_priority.queue"和"low_priority.queue"的三个不同队列之间传播.每个队列的并发使用者数分别需要分别为50、20和5.
Another department is making upstream changes such that the messages are going to be spread amongst three different queues, named "high_priority.queue", "med_priority.queue" and "low_priority.queue". The number of concurrent consumers for each queue needs to be 50, 20 and 5 respectively.
在我的代码中,相同的QueueProcessor
将负责处理消息,但是我不确定如何修改配置以创建三个消息侦听器而不是一个.任何建议表示赞赏.
Within my code the same QueueProcessor
will be responsible for processing the messages, but I am unsure how to modify my config to create the three message listeners instead of one. Any advice is appreciated.
推荐答案
我通过简单地创建多个bean来解决了这个问题-认为这不会那么简单:
I solved this problem by simply creating multiple beans - didn't think it would be this easy:
@Bean
public DefaultMessageListenerContainer highPriorityQueue() {
DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
dmlc.setConnectionFactory(connectionFactory());
dmlc.setDestination(new ActiveMQQueue("high_priority.queue"));
dmlc.setMessageListener(new QueueProcessor());
dmlc.setConcurrentConsumers(50);
return dmlc;
}
@Bean
public DefaultMessageListenerContainer medPriorityQueue() {
DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
dmlc.setConnectionFactory(connectionFactory());
dmlc.setDestination(new ActiveMQQueue("med_priority.queue"));
dmlc.setMessageListener(new QueueProcessor());
dmlc.setConcurrentConsumers(20);
return dmlc;
}
@Bean
public DefaultMessageListenerContainer lowPriorityQueue() {
DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
dmlc.setConnectionFactory(connectionFactory());
dmlc.setDestination(new ActiveMQQueue("low_priority.queue"));
dmlc.setMessageListener(new QueueProcessor());
dmlc.setConcurrentConsumers(5);
return dmlc;
}
这篇关于通过同一ActiveMQConnectionFactory消耗多个ActiveMQ队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!