RabbitMQ分批消耗消息,并立即使用它们

发布于 2025-01-30 11:39:24 字数 279 浏览 2 评论 0 原文

我有一个兔子队列,其中我发布了数千条消息。我需要一个背景服务,该服务将:

  • 在100条消息的批处理中检索消息,
  • 将这些100条消息存储在数据库
  • ACK中所有100条消息
  • 中,请继续使用我使用RabbitMQ Client的下一批100个消息来

收听消息,但是我不知道如何“批量”。

是否有人有一个有效的例子,说明如何一次将我的Messagess 100-100获取并在将它们保存在DB中后一次ack?

提前很多

I have a RabbitMQ queue in which I post thousands of messages. I need a background Service that will:

  • Retrieve the messages in batches of 100 messages
  • Store those 100 messages in a Database
  • Ack all 100 messages at once
  • Proceed with the next batch of 100 messages

I'm using the RabbitMQ Client to listen for messages, but I don't know how to "batch" them.

Does anyone has a working example on how to get my messagess 100-100 at a time and ACK them all at once after they have been saved in a DB?

Thanx a lot in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Oo萌小芽oO 2025-02-06 11:39:24

您想使用“预摘要”和“多ACK”功能-Prefetch-

  • Multi-ack--

这是您在代码中您要做的:

  • 打开连接并将频道
  • 设置为100
  • 启动消费者。您为消费者注册的回调将开始使用消息打电话。 您将必须将这些消息保存在列表或其他数据结构中
  • 一旦收到100条消息,
  • ,保存第100条消息的交付标签并进行数据库工作。 ACK通过将“ Multi Ack”设置为 true ,并使用第100条消息的交付标签,从而获得100条消息。
  • RabbitMQ将以相同的方式发送接下来的100条消息

You want to use the "prefetch" and "multi ack" features -

This is what you'll do in your code:

  • Open a connection and channel
  • Set the channel prefetch to 100
  • Start a consumer. The callback you register for your consumer will start being called with messages. You will have to save these messages in a list or other data structure
  • Once you have received 100 messages, save the delivery tag of the 100th message and do your database work.
  • Ack the 100 messages by setting "multi ack" to true and use the delivery tag of the 100th message.
  • RabbitMQ will send the next 100 messages in the same manner
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文