[rabbitmq-discuss] How to shutdown cleanly a Java application using Consumers?

Bertrand Guay-Paquet bernie at step.polymtl.ca
Wed Apr 2 21:02:19 BST 2014


Hello,

I'm using the RabbitMQ Java client API and need some guidance on the 
proper application shutdown procedure.

Here's how I start the application:
1-Create a Connection
2-Create different types of consumers, each with its own channel, and 
call channel.basicConsume("queue", false, consumer)
3-Let it all run

This works great, but I can't figure out how to cleanly shutdown the 
application. If I simply close the Connection, each created channel 
immediately (or soon enough) becomes invalid and any Consumer currently 
doing some work fails when trying to ack their current message or 
perform any other action on the channel. I'd like to let the consumers 
finish whatever message they're processing and then close everything 
down. I guess I need to keep track of the created Consumers and somehow 
signal them to stop accepting new messages and after they're all done 
with their current job, close the connection? Is that possible or is 
there another way? I haven't found any management methods for the 
consumer classes to control or query their status.

The information I found so far is related to manually created threads 
that poll the queues to process messages. In that case, it's really easy 
because I can just set a flag on each runnable to exit after processing 
their current message and join on all the threads before closing the 
underlying connection. So this leads me to ask, as a side note: are 
Consumers the way to go to use RabbitMQ in real-world scenarios or 
should I poll on the queues? It seems to me that Consumers would be the 
better choice (polling is bad), but if they're less powerful, perhaps 
they're not a silver bullet in my case.

Thank you,
Bertrand


More information about the rabbitmq-discuss mailing list