• <tfoot id='JhVTn'></tfoot>

    <i id='JhVTn'><tr id='JhVTn'><dt id='JhVTn'><q id='JhVTn'><span id='JhVTn'><b id='JhVTn'><form id='JhVTn'><ins id='JhVTn'></ins><ul id='JhVTn'></ul><sub id='JhVTn'></sub></form><legend id='JhVTn'></legend><bdo id='JhVTn'><pre id='JhVTn'><center id='JhVTn'></center></pre></bdo></b><th id='JhVTn'></th></span></q></dt></tr></i><div id='JhVTn'><tfoot id='JhVTn'></tfoot><dl id='JhVTn'><fieldset id='JhVTn'></fieldset></dl></div>

        <bdo id='JhVTn'></bdo><ul id='JhVTn'></ul>

      <small id='JhVTn'></small><noframes id='JhVTn'>

    1. <legend id='JhVTn'><style id='JhVTn'><dir id='JhVTn'><q id='JhVTn'></q></dir></style></legend>
      1. Rabbitmq Ack 或 Nack,将消息留在队列中

        时间:2023-08-26
          • <bdo id='CkKWm'></bdo><ul id='CkKWm'></ul>

              <tbody id='CkKWm'></tbody>
            <tfoot id='CkKWm'></tfoot><legend id='CkKWm'><style id='CkKWm'><dir id='CkKWm'><q id='CkKWm'></q></dir></style></legend>

              <i id='CkKWm'><tr id='CkKWm'><dt id='CkKWm'><q id='CkKWm'><span id='CkKWm'><b id='CkKWm'><form id='CkKWm'><ins id='CkKWm'></ins><ul id='CkKWm'></ul><sub id='CkKWm'></sub></form><legend id='CkKWm'></legend><bdo id='CkKWm'><pre id='CkKWm'><center id='CkKWm'></center></pre></bdo></b><th id='CkKWm'></th></span></q></dt></tr></i><div id='CkKWm'><tfoot id='CkKWm'></tfoot><dl id='CkKWm'><fieldset id='CkKWm'></fieldset></dl></div>

                  <small id='CkKWm'></small><noframes id='CkKWm'>

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

                  问题描述

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

                  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?

                  我尝试过使用

                  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

                  我的完整代码是

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

                  推荐答案

                  我的公司是这样做的:如果一条消息失败(出于任何原因),我们将消息放入一个等待队列中等待 10 秒,它然后被放回队列中重试.我们最多循环 10 次,如果消息被 nack 10 次,那么我们认为这是一个我们无法恢复的失败,我们将它放入一个永久的死信队列进行调查.

                  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.

                  图表如下:

                  这篇关于Rabbitmq Ack 或 Nack,将消息留在队列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:等待一条带有超时的 RabbitMQ 消息 下一篇:如何为 RabbitMQ 管理 HTTP API 生成密码哈希

                  相关文章

                  最新文章

                    <bdo id='8hiZP'></bdo><ul id='8hiZP'></ul>

                    <legend id='8hiZP'><style id='8hiZP'><dir id='8hiZP'><q id='8hiZP'></q></dir></style></legend>

                    1. <tfoot id='8hiZP'></tfoot>

                    2. <small id='8hiZP'></small><noframes id='8hiZP'>

                      <i id='8hiZP'><tr id='8hiZP'><dt id='8hiZP'><q id='8hiZP'><span id='8hiZP'><b id='8hiZP'><form id='8hiZP'><ins id='8hiZP'></ins><ul id='8hiZP'></ul><sub id='8hiZP'></sub></form><legend id='8hiZP'></legend><bdo id='8hiZP'><pre id='8hiZP'><center id='8hiZP'></center></pre></bdo></b><th id='8hiZP'></th></span></q></dt></tr></i><div id='8hiZP'><tfoot id='8hiZP'></tfoot><dl id='8hiZP'><fieldset id='8hiZP'></fieldset></dl></div>