Search code examples
springmessage-queuelifecyclespring-jms

Not able to Stop MQueue listener


I have the following configuration for my MQueue:

<jms:listener-container container-type="default" connection-factory="cachedConnectionFactory" acknowledge="auto">
    <jms:listener id="myListenerId" destination="myDestination" ref="myListener" method="onMessage" />
</jms:listener-container>

When I try to stop the reception of JMS messages, I write the following code

jmsManagement = myProject.instance.getContext().getBean('myListenerId',Lifecycle.class);
jmsManagement.stop();

PS :

  • When I stop() my listener, the isRunning() return False, but I still get messages through the MQueue... the onMessage gets triggered.
  • jmsManagement is an instance of the class Lifecycle. Even when I changed it to DefaultMessageListenerContainer, same thing.
  • I'm receiving messages before calling start(), even when autoStartup is set to false.
  • jmsManagement.shutdown(); didn't stop the listener from being triggered.

Does anyone have an idea about how to stop this MQ listener ? Is there something I'm missing ?


Solution

  • I actually had to set autoStartup to true.

    Since I can't do that using jms:listener-container, I instanciated a DefaultMessageListenerContainer bean and set the autoStartup property to false.

    Here's the code that worked for me :

    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"  id="pitagorCPYListener">
        <property name="autoStartup" value="false" />
        <property name="connectionFactory" ref="cachedConnectionFactory" />
        <property name="destination" ref="defaultDestination" />
        <property name="messageListener" ref="listenerPitagorCPY" />
    </bean>
    
     <bean id="defaultDestination" class="com.ibm.mq.jms.MQQueue">
        <constructor-arg value="#{mqConnectionFactory.destination}"/>
      </bean>