添加动态数目的侦听器(Spring JMS) [英] Adding Dynamic Number of Listeners(Spring JMS)

查看:395
本文介绍了添加动态数目的侦听器(Spring JMS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加多个侦听器,如application.properties文件中所述.像下面一样,

I have a requirement to add multiple listeners as mentioned in the application.properties file. Like Below,

InTopics=Sample.QUT4,Sample.T05,Sample.T01,Sample.JT7

注意:此数字可以更多或更少.

我正在考虑将它们排列成阵列,

I am thinking of getting them in an array,

@Value("${InTopics}")
private String[] inTopics;

但是我不知道如何从数组创建多个侦听器.

But i don't know how to create multiple listeners from the array.

目前,对于一个主题,我正在执行以下操作,

Currently, for one Topic i am doing as below,

@Configuration
@EnableJms
public class JmsConfiguration {

@Value("${BrokerURL}")
private String brokerURL;

@Value("${BrokerUserName}")
private String brokerUserName;

@Value("${BrokerPassword}")
private String brokerPassword;

@Bean
TopicConnectionFactory connectionFactory() throws JMSException {
    TopicConnectionFactory connectionFactory = new TopicConnectionFactory(brokerURL, brokerUserName, brokerPassword);
    return connectionFactory;
}

@Bean
JmsListenerContainerFactory<?> jmsContainerFactory(TopicConnectionFactory connectionFactory) throws JMSException {
    SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setPubSubDomain(Boolean.TRUE);
    return factory;
 }

}

我的听众,

@JmsListener(destination = "${SingleTopicName}", containerFactory = "jmsContainerFactory")
public void receiveMessage(Message msg) {
   //Do Some Stuff
}

我有什么办法可以做到这一点?

Is there any way i can achieve this?

推荐答案

您不能使用带注释的@JmsListener来做到这一点,但可以通过编程方式注册每个侦听器

You can't do it with annotated @JmsListeners but you can register each listener programmatically by extending JmsListenerConfigurer as described in the reference documentation.

编辑

由于您要将该属性作为数组注入...

Since you are injecting the property as an array...

@Value("${InTopics}")
private String[] inTopics;

Spring将拆分列表,并根据列表中的队列数创建一个数组.

Spring will split up the list an create an array based on the number of queues in the list.

然后您可以遍历JmsListenerConfigurer.configureJmsListeners()中的数组并为数组中的每个元素创建一个端点-您无需提前知道数组的大小.

You can then iterate over the array in JmsListenerConfigurer.configureJmsListeners() and create an endpoint for each element in the array - you don't need to know ahead of time how big the array is.

for (String inTopic : inTopics) {
    ...
}

这篇关于添加动态数目的侦听器(Spring JMS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆