How to disable Spring @JmsListener programmatically at startup

I have a Spring application that has methods annotated with Spring @JmsListener. The application is deployed to multiple nodes. On some specific nodes, I need to disable the JMS listener so that it does not pull messages from the queue.

There seems to be a way to stop the listener after starting the application. But this seems to leave a brief window between starting and disabling code, where the application instance can consume messages. So instead, there is a way to disable the listener during application startup.

+4
source share
1 answer

You need to configure listener container definitions created by annotation.

- factory @Bean (. ) autoStartup false.

setAutoStartup(false);

, JmsListenerEndpointRegistry bean. beans - 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 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.
...
+4

Source: https://habr.com/ru/post/1608385/


All Articles