编程语言
首页 > 编程语言> > C#-重新排队后保持交货顺序

C#-重新排队后保持交货顺序

作者:互联网

我正在开发一个使用Rabbit MQ进行消息传递的应用程序.我使用显式ACK:

model.BasicConsume(queueName,false, consumer);

并在处理消息后执行ACK:

consumer.Received += (ch, ea) =>
                {
                    try
                    {                        
                        var message = Encoding.UTF8.GetString(ea.Body);

                        Logger.Info($"DeliveryTag={ea.DeliveryTag}, message={message}");

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

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

问题是当处理消息时发生错误并且Rabbit不接收ACK时,它将以不同顺序将消息返回到队列.

例如,有消息M1,M2,M3,M4.

如果M2返回到队列,它将是M3,M4,M2.

有什么办法可以保持交货顺序吗?

附言我只有一个消费者和RabbitMQ 3.6.6,但是仍然存在重新排序的问题.

解决方法:

在消息排序保证下,所有问题的答案都为here.我只引用

Messages can be returned to the queue using AMQP methods that feature
a requeue parameter (basic.recover, basic.reject and basic.nack), or
due to a channel closing while holding unacknowledged messages. Any of
these scenarios caused messages to be requeued at the back of the
queue for RabbitMQ releases earlier than 2.7.0. From RabbitMQ release
2.7.0, messages are always held in the queue in publication order, even in the presence of requeueing or channel closure.

With release 2.7.0 and later it is still possible for individual
consumers
to observe messages out of order if the queue has multiple
subscribers. This is due to the actions of other subscribers who may
requeue messages. From the perspective of the queue the messages are
always held in the publication order.

标签:rabbitmq,message-queue,rabbitmq-exchange,c,net
来源: https://codeday.me/bug/20191111/2021821.html