RabbitMQ EventingBasicConsumer丢失消息

时间:2017-02-14 09:04:15

标签: c# .net rabbitmq amqp rabbitmq-exchange

我正在使用Rabbit MQ开发一个消息传递应用程序。我使用 EventingBasicConsumer Exchange QueueBind 。为了测试,我运行我的应用程序来接收消息,然后关闭我的以太网控制器。在我开始接收后,再次开始乱码,但大约有500条消息丢失。 我的代码:

private void DoWork()
    {
        try
        {
            var connection = ConnectionFactory.CreateConnection();
            IModel model = connection.CreateModel();

            // Configure the Quality of service for the model. Below is how what each setting means.
            // BasicQos(0="Dont send me a new message untill I’ve finshed",  1= "Send me one message at a time", false ="Apply to this Model only")
            model.BasicQos(0, 1, false);

            model.ExchangeDeclare(Options.RabbitConnectionOptions.Exchange, RabbitConstants.ExchangeType, RabbitConstants.ExchangeDurable, RabbitConstants.ExchangeAutoDelete);
            var queueDeclareOk = model.QueueDeclare("SomeSubsruberQueue3", RabbitConstants.QueueDurable, RabbitConstants.QueueExclusive, RabbitConstants.ExchangeAutoDelete);


            var queueName = queueDeclareOk.QueueName;

            foreach (var optionsBindingKey in Options.BindingKeys)
            {
                Logger.Debug($"QueueBind for {nameof(optionsBindingKey)}={optionsBindingKey}");
                model.QueueBind(queueName, Options.RabbitConnectionOptions.Exchange, optionsBindingKey);
            }


            var consumer = new EventingBasicConsumer(model);

            consumer.Received += (ch, ea) =>
            {
                try
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    var routingKey = ea.RoutingKey;
                    var messageToLog = $" [x] Received '{routingKey}':'{message}'";                        

                    Logger.Info(messageToLog);
                    Console.WriteLine($"receavedCount={++receavedCount}");

                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            };

            model.BasicConsume(queueName, RabbitConstants.QueueAutoAck, consumer);
        }
        catch (Exception e)
        {
            Logger.Error(e);
            Console.WriteLine(e);
            throw;
        }
    }

如何防止丢失邮件?

P.S。这里的日志显示丢失的消息。 “Id”是顺序的,所以你可以看到,id从926到1299的按摩丢失了

2017-02-14 10:52:01.4916 <Root><Subscribers><Subscriber Id="922" Name="1c" />
2017-02-14 10:52:01.4916 <Root><Subscribers><Subscriber Id="923" Name="1c" />
2017-02-14 10:52:01.4916 <Root><Subscribers><Subscriber Id="924" Name="1c" />
2017-02-14 10:52:01.5056 <Root><Subscribers><Subscriber Id="925" Name="1c" />
2017-02-14 10:52:22.5606 <Root><Subscribers><Subscriber Id="1300" Name="1c" />
2017-02-14 10:52:22.5606 <Root><Subscribers><Subscriber Id="1301" Name="1c" />
2017-02-14 10:52:22.5676 <Root><Subscribers><Subscriber Id="1302" Name="1c" />
2017-02-14 10:52:22.6046 <Root><Subscribers><Subscriber Id="1303" Name="1c" />

UPD:

如果我修改:

model.BasicConsume(queueName,true, consumer);

model.BasicConsume(queueName,false, consumer);

并使用显式ASK

((EventingBasicConsumer)ch).Model.BasicAck(ea.DeliveryTag, false);
我有非常奇怪的行为:

2017-02-14 13:06:35.9835| DeliveryTag=23, <Subscriber Id="778" Name="1c" /> 
2017-02-14 13:06:36.0295| DeliveryTag=24, <Subscriber Id="779" Name="1c" /> 
2017-02-14 13:06:57.3285| DeliveryTag=26, <Subscriber Id="782" Name="1c" /> 
2017-02-14 13:06:57.3755| DeliveryTag=27, <Subscriber Id="783" Name="1c" />

没有 DeliveryTag = 25!

2 个答案:

答案 0 :(得分:3)

不要使用自动确认。阅读一条消息,做任何你想做的事情,然后在完成后明确确认。

答案 1 :(得分:0)

Ок,谢谢EmilVikström,我必须使用明确的ASK:

model.BasicConsume(queueName,false, consumer);

并在处理完消息后询问:

((EventingBasicConsumer)ch).Model.BasicAck(ea.DeliveryTag, false);

关于丢失消息的其他问题:rabbitMq在将其返回队列时重新排序消息。所以它不是洛杉矶,它只会稍后退去