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.
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.
First, on your main thread you need to lock the message listener after starting your JMS connection and invoke the message listener “wait” method. On your message listener, you need to lock again the message listener and then invoke the “notify all” method.
Create a Spring JMS Listener To create a JMS listener you need to implement the MessageListener interface. It has an onMessage() method that is triggered for each message that is received. The below StatusMessageListener tries to cast the received message to a TextMessage .
The status of the listeners is always available by running the list listeners command from the user interface or the Transaction Server console. If a listener becomes disconnected, the Transaction Server can attempt to re-connect to the queue multiple times before it fails with a connection error.
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.
Yes thats do the trick.
<jms:listener-container concurrency="1" connection-factory="exampleJmsFactory" destination-type="queue" message-converter="exampleMessageConverter">
<jms:listener id="exampleProductsMessageListener" destination="incoming.example.client.queue" ref="exampleProductsMessageConsumer" method="consume"/>
</jms:listener-container>
DefaultMessageListenerContainer exampleProductsMessageListener= Registry.getApplicationContext().getBean("exampleProductsMessageListener", DefaultMessageListenerContainer.class);
exampleProductsMessageListener.stop();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With