Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Spring @JmsListener programmatically on startup

Tags:

jms

spring-jms

I have a Spring application that has methods annotated with Spring's @JmsListener. The application is deployed on multiple nodes. On some specific nodes I need to disable the JMS listener so that it is not pulling messages off the queue.

There appears to be a way to stop the listener after the application has started up. But this appears to leave open the brief window between startup and when the disable code runs where the application instance may consume messages. So instead is there a way to disable the listener during application startup.

like image 619
sudr Avatar asked Sep 21 '15 22:09

sudr


1 Answers

You need to customize the listener container definitions created by the annotation.

Add a listener container factory @Bean (see the documentation) and set the autoStartup property to false.

setAutoStartup(false);

You can then start each container as needed by getting a reference via the JmsListenerEndpointRegistry bean. The containers are not beans themselves - from its javadoc...

...
* <p>Contrary to {@link MessageListenerContainer}s created manually, listener
* containers managed by registry are not beans in the application context and
* are not candidates for autowiring. Use {@link #getListenerContainers()} if
* you need to access this registry's listener containers for management purposes.
* If you need to access to a specific message listener container, use
* {@link #getListenerContainer(String)} with the id of the endpoint.
...
like image 66
Gary Russell Avatar answered Oct 22 '22 02:10

Gary Russell