spring-projects/spring-amqp — pull request #122 by garyrussell (2013-10-28 19:12:56+00:00)
Title: AMQP-338 Fix Listener Container Stop

Previously, when stopping the listener container, the consumer threads
kept consuming messages and were eventually killed after the `shutdownTimeout`
by forcing the channel closed.

The problem was due to the `receiveAndExecute` method always returned true while
messages exist (called within a loop from AsyncMessageProcessingConsumer.run().

https://jira.springsource.org/browse/AMQP-338

Add code to suspend the client delivery thread during shutdown to prevent delivery
of new messages, while allowing the consumer thread to drain the messages already
in the prefetch queue. When the queue is drained, the method returns false, causing
the loop to exit. Subsequently, consumer.stop() is called, which releases the
delivery thread and closes the channel.

Add a test case to reproduce the problem (more than prefetch messages delivered after
stop()) and verify it is corrected with this change.

It is important to understand that if AcknowledgeMode.NONE is employed, one message
may be lost for each consumer when the container is stopped.