Problems with DefaultMessageListenerContainer with shutdown procedure

I am new to Spring Framework and my questions are below:

I want to programmatically create the DefaultMessageListenerContainer program code, and the code that I use:

 DefaultMessageListenerContainer container = new DefaultMessageListenerContainer(); container.setConnectionFactory(cf); container.setDestination(Queue); container.setMessageListener(Consumer); container.setReceiveTimeout(-1); container.setMaxConcurrentConsumers(15); container.setConcurrentConsumers(10); container.start(); 

Why do I have to manually disable DefaultMessageListenerContainer when my project is not deployed? If I do not manually close the container, users remain open in my queues.

When I try to manually close the container (by calling container.shutdown() ), the procedure ends and the project does not continue. If I initialize the receiveTimeout without providing receiveTimeout , the shutdown procedure is executed correctly. Is there a problem with setReceiveTimeout(-1) ?

+4
source share
3 answers

receiveTimeout is a problem. To complete the work, the container must be able to stop listening to the queue. If your consumer stream has an infinite timeout, it will continue listening and will never check if the container needs to shut down. Your container will go to receiveTimeout to shutdown. If it is -1, it will never shut down.

+1
source

You only need to disconnect the listener manually, because you started it programmatically! If you use ApplicationContext to load Spring beans from xml, then closing the application context will disable all beans for you.

The easiest way I found managing Spring loaded beans is to create a servlet that implements the init () and destroy () methods from the HttpServlet. Init () loads my Spring configuration from my xml files (i.e. the main file named spring.xml) and caches the ApplicationContext object. Then destory () will call close () in the ApplicationContext. This will close / turn off all Spring beans (i.e. your JMS listeners will be stopped).

Any specific reason you programmatically create your listeners?

0
source

Here you just need to stop the container (do not close it or cancel it) and be able to run it back whenever you want, at runtime. Just use .start () and .stop (), which are methods inherited from AbstractJmsListeningContainer . I think. And don't mix them with .doStart () ,. shutDown () ... See spring documentation.

With your listener connected via Spring, you can get it anytime from the context and run .stop or .start on it. During autowiring spring, you can set the autoStartup property to false and the listenerContainer will be initialized, but will not be listened to at startup.

0
source

All Articles