确认 RabbitMQ 消息是否有超时? [英] Is there a timeout for acking RabbitMQ messages?

查看:46
本文介绍了确认 RabbitMQ 消息是否有超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个超时时间,在此之后出队的消息将自动 NACK.

I would like to set a timeout after which a dequeued message is automatically NACKed.

当我将消息出列时,我会等到它通过套接字传输并且对方确认接收.

When I dequeue a message I wait until it is transfered over a socket and the other party confirms its reception.

我需要保留一个计时器列表还是 RMQ 可以自动处理这个列表?

Do I need to keep a list of Timers or can RMQ handle this automatically?

private void Run()
{
    _rmqConnection = _queueConnectionFactory.CreateFactory().CreateConnection();

    _rmqReadchannel = _rmqConnection.CreateModel();

    _rmqReadchannel.QueueDeclare(QueueIdOutgoing(), true, false, false, null);

    _rmqReadchannel.BasicQos(0, 1, false);
    var consumer = new QueueingBasicConsumer(_rmqReadchannel);
    _rmqReadchannel.BasicConsume(QueueIdOutgoing(), false, consumer);
    while (true)
    {
        if (!_rmqReadchannel.IsOpen)
        {
            throw new Exception("Channel is closed");
        }
        var ea = consumer.Queue.Dequeue();
        string jsonData = Encoding.UTF8.GetString(ea.Body);
        if (OnOutgoingMessageReady != null)
        {
            OnOutgoingMessageReady(this, new QueueDataEventArgs(jsonData, ea.DeliveryTag));
        }
        //waiting for ACK from a different thread
    }
}

推荐答案

RabbitMQ 不提供任何类型的用于确认消息的超时机制.官方 Python 教程中对此进行了讨论:

RabbitMQ does not provide any sort of timeout mechanism for acknowledging messages. This is discussed in the official Python tutorial:

没有任何消息超时;RabbitMQ 只会在工作连接断开时重新传递消息.即使处理一条消息需要很长时间也没关系.

There aren't any message timeouts; RabbitMQ will redeliver the message only when the worker connection dies. It's fine even if processing a message takes a very, very long time.

AMQP 0-9-1 规范的第 3.1.8 节 描述了确认,并且非常清楚它们可以是自动(客户端无需执行任何操作,消息一传送就被确认)或显式(客户端必须对其处理的每条消息或消息组进行确认).

Section 3.1.8 of the AMQP 0-9-1 specification describes Acknowledgements, and is very clear that they can either be Automatic (the client doesn't have to do anything, messages are acknowledged as soon as they are delivered) or Explicit (the client must an Ack for each message or group of messages that it has processed).

这里有一些 过去的讨论 早在 2009 年就证实了这种情况.

Here's some past discussion from back in 2009 confirming that this is the case.

所以:是的,如果您需要超时以在一段时间后自动发送 NACK,您必须自己这样做.

So: yes, if you need a timeout to automatically send NACKs after some interval, you will have to do so yourself.

这篇关于确认 RabbitMQ 消息是否有超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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