Gracefully stop DefaultMessageListenerContainer ha

2019-08-04 01:23发布

问题:

Can somebody point me to a way to grecefully stop a DefaultMessageListenerContainer in Spring that is defined with a receiveTimeout = -1 (i.e. it is using the blocking version of javax.jms.MessageConsumer#receive) ?

Actually, I've tried both stop() and shutdown() methods of DMLC but consumer thread is stuck on the receive() call.

"heartBeatContainer-1" prio=6 tid=0x0304d800 nid=0x1d20 in Object.wait() [0x037ef000]
java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x22c48ab0> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:485)
    at org.apache.activemq.SimplePriorityMessageDispatchChannel.dequeue(SimplePriorityMessageDispatchChannel.java:87)
    - locked <0x22c48ab0> (a java.lang.Object)
    at org.apache.activemq.ActiveMQMessageConsumer.dequeue(ActiveMQMessageConsumer.java:452)
    at org.apache.activemq.ActiveMQMessageConsumer.receive(ActiveMQMessageConsumer.java:504)
    at org.springframework.jms.connection.CachedMessageConsumer.receive(CachedMessageConsumer.java:70)
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveMessage(AbstractPollingMessageListenerContainer.java:430)
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:310)
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:263)
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1069)
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1061)
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:958)
    at java.lang.Thread.run(Thread.java:662)

I cannot close the connection as there are other listeners that should continue to consume messages.

回答1:

That's the whole point of the receivetimeout - so the threads can check if the container has been shut down. Setting it to -1 means the threads will block in the JMS client until there's a message. There's not much overhead in timing out, checking the container state and doing another receive. If you don't like the default, set it to a higher number (as long as you can live with a delay when shutting down the container). But, setting it to -1 will cause the behavior you are seeing.