问题
Is there a way to gracefully stop a ListenerContainer
, and its associated Consumers
.
What I'm trying to achieve.
- Stop consuming messages.
- Gracefully stop
ListenerContainer
. - Await long running consumers, and ack when finished.
I'm able to stop the ListenerContainers
using consumer.stop()
, but active long running consumers won't complete successfully, and processed messages won't be acked and will therefore be processed again, once the ListenerContainer has been resumed.
Output
Waiting for workers to finish.
Workers not finished.
Closing channel for unresponsive consumer: Consumer@6d229b1c
The message was processed, but not acked.
I might be able to achieve a graceful shutdown using setForceCloseChannel(false)
, but is it possible to verify if the cancelled consumers has finished? SimpleMessageListenerContainer.doShutDown() has a local scoped List "canceledConsumers".
回答1:
Increase the shutdown timeout.
See Message Listener Container Configuration.
shutdownTimeout
When a container shuts down (for example, if its enclosing ApplicationContext is closed), it waits for in-flight messages to be processed up to this limit. Defaults to five seconds.
/**
* The time to wait for workers in milliseconds after the container is stopped. If any
* workers are active when the shutdown signal comes they will be allowed to finish
* processing as long as they can finish within this timeout. Defaults
* to 5 seconds.
* @param shutdownTimeout the shutdown timeout to set
*/
public void setShutdownTimeout(long shutdownTimeout) {
来源:https://stackoverflow.com/questions/55991826/how-to-gracefully-stop-consuming-messages-with-rabbitlistener