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?)