0

What is the purpose of RabbitMQ AMQP Java client shutdown handlers?

Is the accepted answer still valid? If server is lost, then ShutdownSignalException is thrown and therefore we "learn of the connection/channel closure".

Also since version 3.3.0 there's automatic recovery. Does it mean adding my own ShutdownListener is not required anymore? (and therefore Lyra library is not useful anymore?)

My usecase is that I send messages to rabbitmq cluster and when I stop the node that my client is connected to, basicPublish throws a ShutdownSignalException. I catch it and wait until client connects to the other rabbitmq node. (client connects by itself if I wait), like this:

public void sendMessageToQueue(BasicProperties properties, Model model, String routingKey) {

    try {
            channel.basicPublish("exchange", routingKey, properties, message.getBytes());
    } catch (ShutdownSignalException e) {
        resend(properties, model, routingKey);
    }
}


private void resend(BasicProperties properties, Model model, String routingKey){
    int triedToConnect = 0;
    while (triedToConnect < 3) {
        try {
            Thread.sleep(1000);
            if(channel.isOpen()){
                sendMessageToQueue(properties, model, routingKey);
                break;
            } else {
                triedToConnect++;
            }
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
    if(triedToConnect == 3){
        System.exit(1);
    }
}

But I call basicPublish, basicConsume in many places. I would wrap these method calls so all SSE is caught and wait until reconnection happened and retry sending.

Is it the right approach or should I use ShutdownListeners for this? (How?)

Community
  • 1
  • 1
Balazs Varhegyi
  • 921
  • 1
  • 16
  • 36

1 Answers1

0

I'd guess that shutdown handlers were initially meant to help you recover from failures, but it turns out that properly recovering from a failure manually is fairly complicated. Using the automatic recovery feature or Lyra are the only good ways to deal with this. Shutdown listeners are useful for miscellaneous stuff you might want to do, like shutdown related resources, log messages, etc. In Lyra, events, which are somewhat similar to shutdown listeners, can be used to recover and re-establish certain types of resources that are not automatically recoverable like RpcClients:

https://github.com/jhalterman/lyra/wiki/Lyra-Cookbook#recoverable-rpcclient

As for Lyra versus amqp-client, Lyra provides a few additional features such as eventing and retries (which can help with re-send), but also it is primarily focused on recovery, and recovering from every type of failure that could conceivably occur including connection, channel and consumer failures.

https://github.com/jhalterman/lyra/wiki/Failure-Scenarios

amqp-client's built-in automatic recovery only focuses on connection failures, which is still great, but may not be enough for certain use cases.

Jonathan
  • 5,528
  • 38
  • 47