3

The RabbitMQ documentation presents how to add a shutdown listener and when the listener is called but I fail to see on what purpose the handler is for. Seems that all the Java examples (including https://github.com/rabbitmq/rabbitmq-tutorials) out there ignore shutdown handlers. In which cases should I prefer shutdown listener over simply catching ShutdownSignalException (and IOException) exception ?

Another puzzling issue is that where the control flow goes when the handler has finished.

Potentially relevant related SO questions:

Community
  • 1
  • 1
user272735
  • 10,060
  • 8
  • 60
  • 89

2 Answers2

4

Catching IOException can tell you if a connection/channel shutdown occurred as the result of a method invocation, but connections/channels can also be shutdown involuntarily such as if the connection to the server is lost. In this case a ShutdownListener is the only way to learn of the connection/channel closure.

Shutdown listeners are often used to provide connection/channel recovery, though recovering reliably is a challenge of its own. For this, check out Lyra.

Jonathan
  • 5,528
  • 38
  • 47
  • first wanted to add a comment here but seemed long so created: http://stackoverflow.com/questions/33776794/what-is-the-purpose-of-rabbitmq-amqp-java-client-shutdown-handlers-v2 Could you share your thoughts on this? – Balazs Varhegyi Nov 18 '15 at 09:58
2

The main use of a ShutdownListener i've seen is to do automatic reconnection. You listen for a shutdown, and when it happens, try to reconnect.

For example, this is the basis of rabbitmq-ha-client, where it is done in HaConnectionFactory.HaShutdownListener.

Tom Anderson
  • 44,913
  • 16
  • 86
  • 129