Start and Stop JMS Listener using Spring

JOKe picture JOKe · Jul 10, 2012 · Viewed 21.7k times · Source

So question is how to temporary stop and start a jms listener created using spring using the fallowing way :

<amq:connectionFactory id="exampleJmsFactory" brokerURL="tcp://${jms.broker.url}" />

<jms:listener-container concurrency="1" connection-factory="exampleJmsFactory"  destination-type="queue" message-converter="exampleMessageConverter">
        <jms:listener destination="incoming.example.client.queue" ref="exampleProductsMessageConsumer" method="consume"/>
</jms:listener-container>


<bean id="exampleProductsMessageConsumer" class="com.unic.example.jms.receive.JmsExampleProductsMessageConsumer" scope="tenant"/>

So basically what is the problem. We do have an init/update mechanism that the client can run in any time and durring this init/update I want to stop consuming of ANY messages because the system is unusable in this time and if a message came it will be lost.

So how I can stop the listener or the listener container or the whole connection using the API. I found that a class AbstractJmsListeningContainer have stop/start but how I can get it ? I mean none of this jms: listener and listener-containers have a name or anything like that.

Answer

gkamal picture gkamal · Jul 10, 2012

You can assign an id to the listener-container. Then get a reference to it, either by calling getBean or getting it injected. This will give you a AbstractJmsListeningContainer on which you can call start / stop.