Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple JMS MessageListners in a single MessageListenerContainer for Spring Java Config

I had the following xml code in my spring-config.xml

<jms:listener-container acknowledge="auto"
        connection-factory="cachedConnectionFactory" container-type="default"
        error-handler="consumerErrorHandler" concurrency="20-25">
        <jms:listener destination="#{TaskFinished.destination}"
            method="onMessage" ref="taskFinished" />
</jms:listener-container>

Now, I was converting my spring xml configuration file to Java configuration.

I translated it like

@Bean(name = "consumerJmsListenerContainer")
public DefaultMessageListenerContainer consumerJmsListenerContainer() {
    DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
    messageListenerContainer
            .setConnectionFactory(cachingConnectionFactory());
    messageListenerContainer.setConcurrency("20-25");
    messageListenerContainer.setErrorHandler(new ConsumerErrorHandler());
    messageListenerContainer
            .setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
    messageListenerContainer.setMessageListener(new TaskFinished());
    return messageListenerContainer;
}

What I need to know is, if there were more than one MessageListner in the Message Container like

<jms:listener-container acknowledge="auto"
        connection-factory="cachedConnectionFactory" container-type="default"
        error-handler="consumerErrorHandler" concurrency="20-25">
        <jms:listener destination="#{questionGeneration.destination}"
            method="onMessage" ref="questionGeneration" />
        <jms:listener destination="#{friendShipLogic.destination}"
            method="onMessage" ref="friendShipLogic" />
        <jms:listener destination="#{postAvailabilityChecker.destination}"
            method="onMessage" ref="postAvailabilityChecker" />
        <jms:listener destination="#{playOn.destination}" method="onMessage"
            ref="playOn" />
</jms:listener-container>

How could I supposed to convert this xml code into Java config?

like image 461
Sandhu Santhakumar Avatar asked Feb 19 '14 13:02

Sandhu Santhakumar


People also ask

How could you implement JMS listener using Spring JMS support?

The @JmsListener is the only annotation required to convert a method of a normal bean into a JMS listener endpoint. Spring JMS provides many more annotations to ease the JMS implementation. In order to add multiple listeners to a single method we just need to add multiple @JmsListener annotations.

Is JMS listener multithreaded?

If you use Spring to implement JMS client, instance of the JMSTemplate class is thread-safe. The multi-threading capability provided by Spring from Message Listener Containers, by creating a fixed number of JMS sessions and consumers at startup.

Which dependencies are required to use JMS spring boot?

Maven Configuration xml file with Spring Boot and ActiveMQ dependencies. Additionally, we will need Jackson for object to JSON conversion.

How do I start and stop JMS listener in Spring?

You can access the JMS listener containers from the registry (all or by name) and call stop() on the one(s) you want; the container will stop after any in-process messages complete their processing.


1 Answers

The namespace is just a convenience - each <jms:listener/> element gets its own DMLC; the outer (container) element is just a vehicle to supply common attributes.

like image 134
Gary Russell Avatar answered Sep 22 '22 01:09

Gary Russell