RabbitMQ - surviving a consumer disconnection using DefaultConsumer

StackOverflow https://stackoverflow.com/questions/22941644

  •  29-06-2023
  •  | 
  •  

Вопрос

using java client QueueingConsumer I wrote the following code to assist consumer recovery

while (true) {
   try{
      .. create connection
      .. create channel & queue
      while (true) {
             final QueueingConsumer.Delivery delivery = consumer.nextDelivery();
             ....
      } catch {
          handleException()
      }
}

And every time I restarted rabbit it survived cause it created the connection from start.
Now, I understand I should be working with DefaultConsumer.
So I have this method

public void handleDelivery(...) throws IOException {
}

and my problems are :

  1. I cant catch the exception inside and recreate the connection
  2. This only recovers when the ack fails. What if it fails in another stage of the process? How should I recover these fails?
    Thanks.
Это было полезно?

Решение

You should update to version 3.3.0 of the Rabbit Java Client. Finally, they have added support for reconnection and re-declaration of your topology.

You just need to set this when setting up the ConnectionFactory:

...
connectionFactory.setAutomaticRecoveryEnabled(true);
connectionFactory.setTopologyRecoveryEnabled(true);
...
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top