RabbitMQ - 使用 DefaultConsumer 在消费者断开连接后幸存下来 [英] RabbitMQ - surviving a consumer disconnection using DefaultConsumer

查看:68
本文介绍了RabbitMQ - 使用 DefaultConsumer 在消费者断开连接后幸存下来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用java客户端QueueingConsumer我写了下面的代码来辅助消费者恢复

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()
      }
}

每次我重新启动兔子时,它都会存活下来,因为它从一开始就创建了连接.
现在,我明白我应该与 DefaultConsumer 合作.
所以我有这个方法

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 {
}

我的问题是:

  1. 我无法在内部捕获异常并重新创建连接
  2. 这仅在 ack 失败时恢复.如果它在流程的另一个阶段失败了怎么办?我应该如何恢复这些失败?
    谢谢.

推荐答案

您应该更新到 Rabbit Java Client 3.3.0 版.最后,他们增加了对重新连接和重新声明拓扑的支持.

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.

你只需要在设置ConnectionFactory时设置这个:

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

...
connectionFactory.setAutomaticRecoveryEnabled(true);
connectionFactory.setTopologyRecoveryEnabled(true);
...

这篇关于RabbitMQ - 使用 DefaultConsumer 在消费者断开连接后幸存下来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆