本文介绍了RabbitMQ的ACK或NACK,在队列中留下的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直RabbitMq.net和消息确认玩耍。
如果消费者能够处理你可以在



的形式发回ACK消息

  channel.BasicAck (ea.DeliveryTag,FALSE); 



这将脱下来的队列中。



不过要处理一下,如果该消息未能什么?也许暂时停运,你不想采取关闭消息只是把到后面排队,并与下一个消息进行?



我已经尝试使用

  channel.BasicNack(ea.DeliveryTag,假的,真正的); 



但下一次圆其仍然得到了同样的信息,而不是移动到下一个消息队列



我的完整的代码是

 类节目
{
私有静态IModel通道;
私有静态QueueingBasicConsumer消费;
私有静态IConnection连接;

静态无效的主要(字串[] args)
{
连接= GetRabbitMqConnection();
通道= Connection.CreateModel();
channel.BasicQos(0,1,假);
=消费新​​QueueingBasicConsumer(通道);
channel.BasicConsume(SMSQueue,假的,消费者); (!channel.IsOpen)
,而(真)
{
如果
{
抛出新的异常(通道关闭);
}
变种EA = consumer.Queue.Dequeue();
串jsonified = Encoding.UTF8.GetString(ea.Body);
VAR消息= JsonConvert.DeserializeObject< SmsRecords>(jsonified);
如果(ProcessMessage的())
channel.BasicAck(ea.DeliveryTag,FALSE);
,否则
channel.BasicNack(ea.DeliveryTag,假的,真正的);
}
}

私人静态布尔ProcessMessage的()
{
返回FALSE;
}

公共静态IConnection GetRabbitMqConnection()
{

{
变种的connectionFactory =新的ConnectionFactory
{
=用户名客人,
密码=客人,
主机名=localhost的
};
返回connectionFactory.CreateConnection();
}
赶上(异常前)
{
Console.WriteLine(ex.Message);
返回NULL;
}
}
}


解决方案

这是我的公司是如何做的:如果(出于任何原因)的消息失败,我们NACK信息进入它坐了10秒的保持队列,它就被放回到队列中重试。我们做这个循环高达10倍,如果消息被NACKED 10倍,那么我们认为这是我们不能从恢复失败,我们把它变成了一个调查永久死信队列。



下面是图:


I have been playing around with RabbitMq.net and the message acknowledgements.If the consumer is able to process the message you can send back an ack in the form of

channel.BasicAck(ea.DeliveryTag, false);

which will take it off the queue.

But what about if the message was unable to be processed ? maybe a temporary outage and you don't want the message taken off the queue just put to the back and carry on with the next message?

I have tried using

channel.BasicNack(ea.DeliveryTag, false, true);

but the next time round its still getting the same message and not moving to the next message in the queue

my complete code is

class Program
{
    private static IModel channel;
    private static QueueingBasicConsumer consumer;
    private static IConnection Connection;

    static void Main(string[] args)
    {
        Connection = GetRabbitMqConnection();
        channel = Connection.CreateModel();
        channel.BasicQos(0, 1, false);
        consumer = new QueueingBasicConsumer(channel);
        channel.BasicConsume("SMSQueue", false, consumer);
        while (true)
        {
            if (!channel.IsOpen)
            {
                throw new Exception("Channel is closed");
            }
            var ea = consumer.Queue.Dequeue();
            string jsonified = Encoding.UTF8.GetString(ea.Body);
            var message = JsonConvert.DeserializeObject<SmsRecords>(jsonified);
            if (ProcessMessage())
                channel.BasicAck(ea.DeliveryTag, false);
            else
                channel.BasicNack(ea.DeliveryTag, false, true);
        }
    }

    private static bool ProcessMessage()
    {
        return false;
    }

    public static IConnection GetRabbitMqConnection()
    {
        try
        {
            var connectionFactory = new ConnectionFactory
            {
                UserName = "guest",
                Password = "guest",
                HostName = "localhost"
            };
            return connectionFactory.CreateConnection();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return null;
        }
    }
}
解决方案

This is how my company does it: If a message fails (for any reason) we nack the message into a holding queue where it sits for 10 seconds, it then gets put back into the queue to be retried. We do this loop up to 10 times, if the message is nacked 10 times then we assume it is a failure we can't recover from and we put it into a permanent dead-letter queue for investigation.

Here is the diagram:

这篇关于RabbitMQ的ACK或NACK,在队列中留下的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 01:01