Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Dynamic Number of Listeners(Spring JMS)

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

NOTE: This number can be lot more or less.

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;
 }

}

And My Listener,

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

Is there any way i can achieve this?

like image 266
Rajkishan Swami Avatar asked Dec 03 '15 10:12

Rajkishan Swami


People also ask

Can a JMS queue have multiple listeners?

a) Yes, you can define multiple MQ Listeners for the same queue manager, and having many queue managers in the same box, each with its own listener. The KEY restriction is that each Port Number MUST be different.

How JMS listener is implemented in spring?

Create a Spring JMS Listener To create a JMS listener you need to implement the MessageListener interface. It has an onMessage() method that is triggered for each message that is received. The below StatusMessageListener tries to cast the received message to a TextMessage .

What is JMS listener concurrency?

According to Spring JMS doc concurrency attribute specifies. The number of concurrent sessions/consumers to start for each listener.

Which features are provided by Spring JMS?

21.1 Introduction. Spring provides a JMS integration framework that simplifies the use of the JMS API much like Spring's integration does for the JDBC API. JMS can be roughly divided into two areas of functionality, namely the production and consumption of messages.


1 Answers

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

EDIT

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

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

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

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) {
    ...
}
like image 84
Gary Russell Avatar answered Oct 02 '22 05:10

Gary Russell