Start and Stop JMS Listener using Spring
Asked Answered
C

3

8

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.

Chilson answered 10/7, 2012 at 6:32 Comment(0)
A
11

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.

Analyzer answered 10/7, 2012 at 7:3 Comment(2)
well first of all I cannot assign id really to the listener-container. The second issue is that in fact I have 3 listeners not just one so it is 1 listener-container with 3 listeners so if I try to get by class DefaultMessageListenerContainer or AbstractJmsListeningContainer it tells me that it finds 3. I guess for each listener it creates a listener container so I cannot really lookup by class/typeChilson
ahh the listener object itself have an id which is passed to the container let me try thatChilson
C
6

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();
Chilson answered 10/7, 2012 at 10:40 Comment(2)
How ill do it when i have 10 listeners, calling the same consume method. First i have to identify which listener is calling my consume method. this will make this code cumbersome. Do you any other suggestion for the same objective.Columbous
@JOKe When I stop() my listener, the isRunning() return False, but I still get messages through the MQueue... Any idea ?Blacken
L
1

You can also get a hold of the messageListenerContainer, and invoke stop() on it:

@javax.annotation.Resource //autowire by name
 private AbstractJmsListeningContainer myMessageListenerContainer;

 myMessageListenerContainer.stop();

 I'm using the more verbose setup of this container:
 <bean id="myMessageListenerContainer" class="org.springframework.jms.listener.DefaultMes sageListenerContainer">
 <property name="connectionFactory" ref="jmsConnectionFactory"/>
 <property name="destination" ref="myQueue"/>
 <property name="messageListener" ref="myListener"/>
 <property name="autoStartup" value="true" />
 </bean>

Here you see that you can set autoStartup to false if you don't want the listenerContainer to automatically start.

Lyophilic answered 7/2, 2017 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.