RabbitMQ 中的延迟消息 [英] Delayed message in RabbitMQ

查看:100
本文介绍了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.

推荐答案

您可以尝试两种方法:

旧方法: 在每个消息/队列(策略)中设置 TTL(生存时间)标头,然后引入一个 DLQ 来处理它.一旦 ttl 过期,您的消息将从 DLQ 移动到主队列,以便您的侦听器可以处理它.

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.

最新方法:最近 RabbitMQ 提出了 RabbitMQ Delayed Message Plugin ,使用它您可以实现相同的功能,并且该插件支持自 RabbitMQ-3.5.8 起可用.

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.

您可以声明一个类型为 x-delayed-message 的交换,然后使用自定义标头 x-delay 发布消息,以毫秒为单位表示消息的延迟时间.消息将在 x-delay 毫秒

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

byte[] messageBodyBytes = "delayed payload".getBytes("UTF-8");
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("x-delay", 5000);
AMQP.BasicProperties.Builder props = new 
AMQP.BasicProperties.Builder().headers(headers);
channel.basicPublish("my-exchange", "", props.build(), messageBodyBytes);

更多信息:git

这篇关于RabbitMQ 中的延迟消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆