<small id='0nIKQ'></small><noframes id='0nIKQ'>

      <tfoot id='0nIKQ'></tfoot>
      • <bdo id='0nIKQ'></bdo><ul id='0nIKQ'></ul>
    1. <legend id='0nIKQ'><style id='0nIKQ'><dir id='0nIKQ'><q id='0nIKQ'></q></dir></style></legend>

      1. <i id='0nIKQ'><tr id='0nIKQ'><dt id='0nIKQ'><q id='0nIKQ'><span id='0nIKQ'><b id='0nIKQ'><form id='0nIKQ'><ins id='0nIKQ'></ins><ul id='0nIKQ'></ul><sub id='0nIKQ'></sub></form><legend id='0nIKQ'></legend><bdo id='0nIKQ'><pre id='0nIKQ'><center id='0nIKQ'></center></pre></bdo></b><th id='0nIKQ'></th></span></q></dt></tr></i><div id='0nIKQ'><tfoot id='0nIKQ'></tfoot><dl id='0nIKQ'><fieldset id='0nIKQ'></fieldset></dl></div>
      2. RabbitMQ 和 C#

        时间:2023-08-24

          <small id='6n1Os'></small><noframes id='6n1Os'>

          <legend id='6n1Os'><style id='6n1Os'><dir id='6n1Os'><q id='6n1Os'></q></dir></style></legend>

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

                <bdo id='6n1Os'></bdo><ul id='6n1Os'></ul>

                <tfoot id='6n1Os'></tfoot>
                    <tbody id='6n1Os'></tbody>
                • 本文介绍了RabbitMQ 和 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  RabbitMQ 有没有一种类似于 MSSMQ 的使用方法,可以从队列中弹出 1000 条消息,然后插入数据库并从那里继续.

                  With RabbitMQ is there a way to use it similar to MSSMQ where one can pop 1000 messages from the queue, then do your inserts to the database and continue from there.

                  我似乎无法通过订阅频道然后在订阅中的 BasicDeliveryEventArgs 上执行 foreach 来执行此操作,并使用我想要在给定时间处理的最大消息数执行 If 语句.

                  I cannot seem to do that with a Subscription to a channel and then doing a foreach over the BasicDeliveryEventArgs in the Subscription, with that doing a If statement with the max message count I want to process at the given time.

                  提前致谢然而,这仍然会从队列中获取所有 22k 消息

                  Thanks in advance This however still takes all 22k messages from the queue

                  using (IConnection connection = factory.CreateConnection())
                  {
                      using (IModel channel = connection.CreateModel())
                      {
                          channel.QueueDeclare("****", true, false, false, null);
                  
                          var subscription = new Subscription(channel, "****", false);
                          int maxMessages = 5;
                          int i = 0;
                          foreach (BasicDeliverEventArgs eventArgs in subscription)
                          {
                              if (++i == maxMessages)
                              {
                                  Console.WriteLine("Took 5 messages");
                                  subscription.Ack(eventArgs);
                                  break;
                              }
                          }
                      }
                  }
                  

                  推荐答案

                  我假设你想优化将消息加载到数据库中,通过将它们分组到更大的事务中而不是消耗每个事务的成本信息.强制性警告表明,这样做意味着大量消息可能会一起失败,即使其中只有一个会导致问题,这就是您的处理方式...

                  I'm assuming that you want to optimise loading of messages into the database by batching up groups of them into larger transactions rather than wearing the cost of a transaction per message. With the obligatory warning that doing so means large groups of messages can fail together, even if only one of them causes a problem, here's how you'd go about it...

                  在频道上设置QOS:

                  channel.BasicQos(0, 1000, false);
                  

                  这将预先获取 1000 条消息并阻止进一步的流量,直到您确认某些内容.请注意,它不会以 1000 个块为单位进行获取.相反,它确保在任何时候最多预获取 1000 条 UNACK 消息.模拟块传输就像首先处理 1000 条消息,然后一次性 ACK 一样简单.

                  This will pre-fetch 1000 messages and block further traffic until you ACK something. Note that it doesn't fetch in blocks of 1000. Rather, it ensures that a maximum of 1000 UNACK'ed messages are pre-fetched at any one time. Simulating block transfers is as simple as processing the 1000 messages first, and then ACK'ing them all in one go.

                  请参阅此处和这里比我的解释更权威.

                  See here and here for a more authoritative explanation than mine.

                  还有一点:您可能希望在消息可用时立即刷新队列,即使您尚未达到 1000 条消息的配额.您应该能够通过在 foreach 循环内调用 queue.BasicGet() 直到它运行干,然后交付您拥有的任何内容(包括您拉出的消息)来执行此操作subscription) 到数据库.警告:我自己没有尝试过,所以我可能在说垃圾,但我认为它会起作用.这种方法的美妙之处在于它立即将消息推送到数据库中,而无需等待整批 1000 条消息.如果数据库因处理太多小事务而落后,预取积压将在每个周期之间简单地填满.

                  One more point: You may want to flush the queue as soon as messages are available, even if you haven't made your quota of 1000 messages. You should be able to do this by calling queue.BasicGet() inside the foreach loop until it runs dry, and then delivering whatever you have (including the message you pulled out of subscription) to the database. Caveat: I haven't tried this myself, so I could be talking rubbish, but I think it'll work. The beauty of this method is that it pushes messages into the database immediately, without having to wait for a full batch of 1000 messages. If the database falls behind from handling too many small transactions, the prefetch backlog will simply fill up more between each cycle.

                  这篇关于RabbitMQ 和 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 RabbitMQ 中混合发布/订阅与工作队列 下一篇:RabbitMQ + Windows + LDAP 不发送密码

                  相关文章

                  最新文章

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

                    1. <legend id='P7aOd'><style id='P7aOd'><dir id='P7aOd'><q id='P7aOd'></q></dir></style></legend>
                    2. <small id='P7aOd'></small><noframes id='P7aOd'>

                    3. <tfoot id='P7aOd'></tfoot>