javascript - RabbitMQ(和 Node.js)中的异步确认

标签 javascript node.js rabbitmq amqp

我心中有一个 RabbitMQ 的具体用例,我想澄清某些事情并寻求建议。

考虑这种情况:

1-我发布两条消息,即要执行的任务:messageA,然后是messageB

2-我的消费者收到消息A,执行该消息中包含的任务,但在任务运行时,服务器崩溃

我的问题是:当服务器重新启动时,messageA是否会重新排队,是否会在messageB之前重新排队(与崩溃之前的顺序相同)?

据我了解,如果服务器崩溃,messageA 将会丢失,因为我的消费者在收到消息时默认已确认它。

所以我的想法是分别消费和确认消息:先消费,运行任务,然后在任务执行成功后确认消息。

您认为这种方法有问题吗?你能建议我做点别的吗?

最佳答案

RabbitMQ 不保留重新排队的消息顺序:

For the most part, yes. Say a publisher publishes messages M1, M2, M3 and M4 (in that order) on the same channel and with the same routing information. If these messages are routed to a queue then they will end up in the same order that they were published. Consuming on the queue will yield M1, M2, M3 and then M4.

However, the order is only guaranteed in the absence of requeuing. A message will be implicitly requeued if a consumer closes the channel before ack'ing a message. For example, if a consumer receives M1, fails to ack and closes the channel then the next consumer will receive messages in the order M2, M3, M4, M1. Messages can also be explicitly requeued if the consumer calls basic.recover{requeue=true}. The FAQ on at least once delivery talks about this in some more detail.

RabbitMQ FAQ

这可以完成,但 RabbitMQ 不会自动执行,您需要通过将子消息包装在父消息中来自己管理生命周期。然后,您将拥有管理父消息的代码,并在消息 1 成功但消息 2 失败时处理回滚。希望这有帮助..

最诚挚的问候

关于javascript - RabbitMQ(和 Node.js)中的异步确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7400039/

相关文章:

javascript - 用于保存下拉值的 session 存储

node.js - jsreport Hello World

javascript - 编写 express.js 应用程序。辅助方法去哪里了?

java - RabbitMQ 同一队列的多个消费者

javascript - 无法在 HTML 中运行 javascript onClick?

Javascript 深度复制对象

node.js - Express 和 i18n 默认区域设置不起作用

将 amqp_cstring_bytes 转换回 C 字符串

rabbitmq - 如何修复这个rabbitmq/celery错误?

javascript - 仅当数组存在时 lodash 映射数据