RabbitMQ 中的延迟消息
是否可以通过 RabbitMQ 发送消息,但有一定的延迟? 例如,我想在 30 分钟后使客户端会话过期,并且我发送一条消息,该消息将在 30 分钟后处理。
Is it possible to send message via RabbitMQ with some delay?
For example I want to expire client session after 30 minutes, and I send a message which will be processed after 30 minutes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
感谢 Norman 的回答,我可以在 Node.js 中实现它。
从代码中一切都非常清楚。
Thanks to Norman's answer, I could implement it in Node.js.
Everything is pretty clear from the code.
由于我没有足够的声誉来添加评论,因此发布了新答案。这只是对 http: //www.javacodegeeks.com/2012/04/rabbitmq-scheduled-message-delivery.html
除了在消息上设置 ttl 之外,您还可以在队列级别设置它。此外,您还可以避免仅仅为了将消息重定向到不同的队列而创建新的交换。以下是示例 Java 代码:
生产者:
消费者:
As I don't have enough reputation to add comment, posting a new answer. This is just an addition to what has been already discussed at http://www.javacodegeeks.com/2012/04/rabbitmq-scheduled-message-delivery.html
Except instead of setting ttl on messages, you can set it at queue level. Also you can avoid creating a new exchange just for the sake of redirecting the messages to different Queue. Here is sample Java code:
Producer:
Consumer:
它看起来像 这篇博文描述了使用死信交换和消息 ttl 来执行类似的操作。
下面的代码使用 CoffeeScript 和 Node.js 访问 Rabbit 并实现类似的功能。
It looks like this blog post describes using the dead letter exchange and message ttl to do something similar.
The code below uses CoffeeScript and Node.js to access Rabbit and implement something similar.
目前这是不可能的。您必须将过期时间戳存储在数据库或类似的东西中,然后有一个帮助程序来读取这些时间戳并将消息排队。
延迟消息是经常需要的功能,因为它们在许多情况下都很有用。但是,如果您需要使客户端会话过期,我相信消息传递并不是您的理想解决方案,另一种方法可能会更好。
That's currently not possible. You have to store your expiration timestamps in a database or something similiar, and then have a helper program that reads those timestamps and queues a message.
Delayed messages are an often requested feature, as they're useful in many situations. However, if your need is to expire client sessions I believe that messaging is not the ideal solution for you, and that another approach might work better.
AMQP 协议不支持延迟消息传递,但可以使用 Time-To-Live 和 Expiration 和 < a href="https://www.rabbitmq.com/dlx.html" rel="nofollow noreferrer">死信交换扩展延迟消息传递是可能的。该解决方案在此描述 链接。我从该链接复制了以下步骤:
GitHub 上的 RabbitMQ 存储库。
请注意,有一个名为 Celery 的解决方案,它支持延迟任务排队RabbitMQ 代理通过提供名为
apply_async()
的调用 API 来实现。 Celery 支持 Python、node 和 PHP。AMQP protocol does not support delayed messaging, but by using Time-To-Live and Expiration and Dead Letter Exchanges extensions delayed messaging is possible. The solution is described in this link. I copied the following steps from that link:
There is also a plugin for delayed messaging in RabbitMQ repository on GitHub.
Note that there is a solution called Celery which supports delayed task queuing on RabbitMQ broker by presenting a calling API called
apply_async()
. Celery supports Python, node and PHP.您可以尝试两种方法:
旧方法:在每个消息/队列(策略)中设置 TTL(生存时间)标头,然后引入 DLQ 来处理它。一旦 ttl 过期,您的消息将从 DLQ 移动到主队列,以便您的侦听器可以处理它。
最新方法: 最近 RabbitMQ 推出了 RabbitMQ 延迟消息插件,使用它您可以实现相同的效果,并且自 RabbitMQ-3.5.8 起就提供了此插件支持。
您可以使用 x-delayed-message 类型声明交换,然后使用自定义标头 x-delay 发布消息,以毫秒为单位表示消息的延迟时间。消息将在 x-delay 毫秒后传递到相应的队列
更多信息:git
There are two approaches you can try:
Old Approach: Set the TTL(time to live) header in each message/queue(policy) and then introduce a DLQ to handle it. once the ttl expired your messages will move from DLQ to main queue so that your listener can process it.
Latest Approach: Recently RabbitMQ came up with RabbitMQ Delayed Message Plugin , using which you can achieve the same and this plugin support available since RabbitMQ-3.5.8.
You can declare an exchange with the type x-delayed-message and then publish messages with the custom header x-delay expressing in milliseconds a delay time for the message. The message will be delivered to the respective queues after x-delay milliseconds
More here: git
随着 RabbitMQ v2.8 的发布,计划交付现已可用,但作为间接功能:http://www.javacodegeeks.com/2012/04/rabbitmq-scheduled-message-delivery.html
With the release of RabbitMQ v2.8, scheduled delivery is now available but as an indirect feature: http://www.javacodegeeks.com/2012/04/rabbitmq-scheduled-message-delivery.html