Time-To-Live Extensions

RabbitMQ allows you to set Time To Live for both messages and queues.
RabbitMQ 允许你针对 message 和 queue 设置 TTL 值。

Per-Queue Message TTL

The x-message-ttl argument to queue.declare controls for how long a message published to a queue can live before it is discarded. TTL can be set for a given queue by setting the x-message-ttl argument to queue.declare, or by setting the message-ttl policy.A message that has been in the queue for longer than the configured TTL is said to be dead. Note that a message routed to multiple queues can die at different times, or not at all, in each queue in which it resides. The death of a message in one queue has no impact on the life of the same message in other queues.
通过在 queue.declare 中设置 x-message-ttl 参数,可以控制被 publish 到 queue 中的 message 被丢弃前能够存活的时间。可以通过设置名为x-message-ttl 的 arugment 到 queue.declare ,或者通过设置名为 message-ttl 的 policy,来控制 queue 中 message 的存活时间。当某个 message 在 queue 留存的时间超过了配置的 TTL 值时,我们说该 message “已死”。值得注意的是,当一个 message 被路由到多个 queue 中时,其可以在不同的时间“死掉”,或者可能有的不会出现“死掉”情况。在某个 queue 中的某个 message 的“死亡”不会对相同 message 在其他 queue 中的生存状况产生影响。

The server guarantees that dead messages will not be included in any basic.get-ok or basic.deliver methods. Further, the server will try to reap messages at or shortly after their TTL-based expiry.
服务器会保证“死掉”的 message 将不会在任何 basic.get-ok 或 basic.deliver 方法中被包含。更进一步,服务器将努力在 TTL 到期或到期后的短时间内处理掉该 message 。

The value of the x-message-ttl argument must be a non-negative 32 bit integer (0 <= n <= 2^32-1) The value of the TTL argument or policy must be a non-negative integer (0 <= n), describing the TTL period in milliseconds. Thus a value of 1000 means that a message added to the queue will live in the queue for 1 second or until it is delivered to a consumer. The argument can be of AMQP type short-short-int, short-int, long-int, or long-long-int.
参数 x-message-ttl 的值必须是非负的 32 位整数 (0 <= n <= 2^32-1) 作为 argument 或 policy 的 TTL 的值必须为非负整数(0<=n),以毫秒为单位表示 TTL 的值。这样,值 1000 表示存在于 queue 中的当前 message 将最多只存活 1 秒钟,除非其被投递到 consumer 上。实参可以是以下 AMQP 类型:short-short-int 、short-int 、long-int 或者 long-long-int 。

This example in Java creates a queue in which messages may reside for at most 60 seconds:
下面的 Java 实例代码创建了一个 queue ,且 message 在该 queue 的存活时间最大为 60 秒:

?
1
2
3
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-message-ttl", 60000);
channel.queueDeclare("myqueue", false, false, false, args);

To specify a TTL using policy, add the key "message-ttl" to a policy definition. For example:
若想要通过 policy 设置 TTL ,则需要将 key 值 message-ttl 添加到 policy 定义中,例如:

rabbitmqctl rabbitmqctl set_policy TTL ".*" '{"message-ttl":60000}' --apply-to queues
rabbitmqctl (Windows) rabbitmqctl set_policy TTL ".*" "{""message-ttl"":60000}" --apply-to queues

This applies a TTL of 60 seconds to all queues. 
上述命令设置所有 queue 的 TTL 均为 60 秒。

The original expiry time of a message is preserved if it is requeued (for example due to the use of an AMQP method that features a requeue parameter, or due to a channel closure). 
当 message 被 requeue 的时候,其原始过期时间将被保留(例如由于设置了 requeue 参数的 AMQP 方法的使用,或者由于 channel 的关闭)。

Setting x-message-ttl The TTL to 0 causes messages to be expired upon reaching a queue unless they can be delivered to a consumer immediately. Thus this provides an alternative to basic.publish's immediate flag, which the RabbitMQ server does not support. Unlike that flag, no basic.returns are issued, and if a dead letter exchange is set then messages will be dead-lettered. 
设置 x-message-ttl TTL 为 0 则表示,在 message 到达 queue 之后,若未被立即投递到 consumer ,则立即将其判定为过期。这种方式相当于 RabbitMQ server 不支持 basic.publish 中 immediate 标识情况下的等价实现。与采用 immediate 属性的方式不同的是,将不会有 basic.returns 命令的调用,并且在设置了 dead letter exchange 的情况下,这些 message 将被处理为 dead-lettered 。

Per-Message TTL

A TTL can be specified on a per-message basis, by setting the expiration field in the basic AMQP class when sending a basic.publish.
TTL 设置可以具体到每一条 message 本身,只要在通过 basic.publish 命令发送 message 时设置 expiration 字段。

The value of the expiration field describes the TTL period in milliseconds. The same constraints as for x-message-ttl apply. Since the expiration field must be a string, the broker will (only) accept the string representation of the number.
expiration 字段以毫秒为单位表示 TTL 值。且与 x-message-ttl 具有相同的约束条件。因为 expiration 字段必须为字符串类型,broker 将只会接受以字符串形式表达的数字。

When both a per-queue and a per-message TTL are specified, the lower value between the two will be chosen.
当同时指定了 queue 和 message 的 TTL 值,则两者中较小的那个才会起作用。

This example in Java publishes a message which can reside in the queue for at most 60 seconds:
下面的 Java 示例 publish 了最多能在 queue 中存活 60 秒的 message :

?
1
2
3
4
byte[] messageBodyBytes = "Hello, world!".getBytes();
AMQP.BasicProperties properties = new AMQP.BasicProperties();
properties.setExpiration("60000");
channel.basicPublish("my-exchange", "routing-key", properties, messageBodyBytes);

Caveats

While consumers never see expired messages, only when expired messages reach the head of a queue will they actually be discarded (or dead-lettered). When setting a per-queue TTL this is not a problem, since expired messages are always at the head of the queue. When setting per-message TTL however, expired messages can queue up behind non-expired ones until the latter are consumed or expired. Hence resources used by such expired messages will not be freed, and they will be counted in queue statistics (e.g. the number of messages in the queue).
虽然 consumer 从来看不到过期的 message ,但过期 message 只会在到达 queue 的头部时才会被真正的丢弃(或者 dead-lettered )。当采用针对 queue 设置 TTL 的方式时,不会产生任何问题,因为过期的 message 总是会出现在 queue 的头部。但是当针对每条 message 设置了 TTL 时,已过期 message 可能会排在未过期 message 的后面,直到后者被 consume 掉或者过期。在这种情况下,这些过期的 message 使用的资源将不会被释放,且会在 queue 统计信息中被计算进去(例如,queue 中存在的 message 的数量)。

Queue TTL

The x-expires argument to queue.declare Expiry time can be set for a given queue by setting the x-expires argument to queue.declare, or by setting the expires policy. This controls for how long a queue can be unused before it is automatically deleted. Unused means the queue has no consumers, the queue has not been redeclared, and basic.get has not been invoked for a duration of at least the expiration period. This can be used, for example, for RPC-style reply queues, where many queues can be created which may never be drained.
queue.declare 命令中的 x-expires 参数 可以通过设置 x-expires 参数给 queue.declare 方法,或者设置名为 expires 的 policy 来设置过期时间,从而控制 queue 被自动删除前可以处于“未使用”状态的时间。“未使用”的意思是指 queue 上没有任何 consumer ,且在过期时间段内 queue 没有被重新声明,也未通过 basic.get 命令被访问过。该方式可用于,例如,RPC-style 的回复 queue ,其中许多 queue 会被创建出来,但是却从未被使用。

The server guarantees that the queue will be deleted, if unused for at least the expiration period. No guarantee is given as to how promptly the queue will be removed after the expiration period has elapsed. Leases of durable queues restart when the server restarts.
服务器会确保在过期时间到达后 queue 被删除,但是不保证删除的动作有多么的及时。在服务器重启后,持久化的 queue 的超时时间将重新计算。

The value of the x-expires argument or expires policy describes the expiration period in milliseconds and is subject to the same constraints as x-message-ttl and cannot be zero . It must be a positive integer (unlike message TTL it cannot be 0). Thus a value of 1000 means a queue which is unused for 1 second will be deleted.
x-expires 参数的值或者 policy 中的 expires 的值用于表示超期时间,以毫秒为单位。并且服从和 x-message-ttl 一样的约束条件,且不能设置为 0 。该值必须为正数(与消息 TTL 不同,该值不可以为 0),所以如果该参数设置为 1000 ,则表示该 queue 如果在 1 秒钟之内未被使用则会被删除。

This example in Java creates a queue which expires after it has been unused for 30 minutes.
下面的 Java 示例创建了一个 queue ,其会在 30 分钟不使用的情况下判定为超时。

?
1
2
3
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-expires", 1800000);
channel.queueDeclare("myqueue", false, false, false, args);

The following policy does the same thing for all queues:

下面的 policy 设置可以实现和上面相同的功能:

rabbitmqctl rabbitmqctl set_policy expiry ".*" '{"expires":1800000}' --apply-to queues
rabbitmqctl (Windows) rabbitmqctl set_policy expiry ".*" "{""expires"":1800000}" --apply-to queues

【原创】RabbitMQ 之 TTL 详解(翻译)相关推荐

  1. vb6反编译详解_[原创]VB6反编译详解(一)

    [原创]VB6反编译详解(一) 2006-7-9 16:59 23171 [原创]VB6反编译详解(一) 2006-7-9 16:59 23171 VB6反编译详解 by Kenmark-Fenix ...

  2. RabbitMQ基础知识详解

    RabbitMQ基础知识详解 2017年08月28日 20:42:57 dreamchasering 阅读数:41890 标签: RabbitMQ 什么是MQ? MQ全称为Message Queue, ...

  3. 程序的编译(详解翻译环境)

    程序的编译 1. 程序的翻译环境和执行环境 2. 详解翻译环境 2.1 详解编译过程 2.1.1 预编译(预处理) 2.1.2 编译 ①词法分析 ②语法分析 ③语义分析 2.1.3 汇编 ①将汇编代码 ...

  4. 消息队列RabbitMQ基础知识详解

    一: 什么是MQ? MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序或者模块对模块的通信方法.MQ是消费-生产者模型的一个典型的代表,一端往消息队列中不断写入消息,而另 ...

  5. RabbitMQ安装使用详解

    1.下载相应的版本安装: http://www.rabbitmq.com/download.html eg:http://www.rabbitmq.com/releases/rabbitmq-serv ...

  6. rabbitmq 连接过程详解

    连接过程详解 转载于:https://www.cnblogs.com/mrxiaohe/p/6605068.html

  7. RabbitMQ的6种工作模式的学习记录,普通MAVEN和springboot项目整合rabbitmq的API详解

    1.RabbitMQ后台管理页面 2.RabbitMQ 核心(自我理解) 3.RabbitMQ6种工作模式介绍 4. RabbitMQ的消息可靠性 5.RabbitMQ普通MAVEN项目使用 6.Sp ...

  8. java实现rabbitMQ延时队列详解以及spring-rabbit整合教程

    在实际的业务中我们会遇见生产者产生的消息,不立即消费,而是延时一段时间在消费.RabbitMQ本身没有直接支持延迟队列功能,但是我们可以根据其特性Per-Queue Message TTL和 Dead ...

  9. RabbitMQ的Queue详解;

    一.前言 Queue(队列)是RabbitMQ的内部对象,用于存储消息队列,并将它们转发给消费者: 二.Queue队列 队列跟交换机共享某些属性,但是队列也有一些另外的属性 Name:队列的名称 Du ...

最新文章

  1. wireshark安装
  2. JVM class加载机制的总结 收藏
  3. windows 技术篇-判断某个ip地址相对于自己的主机是内网ip还是外网ip实例演示
  4. 【Paper】论文中定义、定理、引理、证明分别的含义
  5. JVM之垃圾收集器回收种类
  6. 记一次EF Core连接MySql、Oracle
  7. Python 中@符号解释
  8. Educational Codeforces Round 54 (Rated for Div. 2) D Edge Deletion (SPFA + bfs)
  9. 构建可扩展的思科互联网络---单区域OSPF
  10. 小程序学习笔记(5)-目录结构介绍
  11. 简易sql词法分析器和语法分析器
  12. 玩转Luat 进阶篇③——远程升级实现过程详解
  13. android跑马灯效果横向,Android 通过自定义View实现纵向跑马灯效果
  14. 睿瞳车牌识别测试总结
  15. 发散思维能力(1-2)
  16. python笔记:猜大小,随机数
  17. OpenVINO-yolov5推理代码
  18. [渝粤教育] 西南科技大学 投资项目评估 在线考试复习资料
  19. 浦东新区科技发展基金产学研专项资金(电子信息)最高200万
  20. 【FPGA学习笔记】VHDL语言(二):VHDL的数字表示,数据对象,数据类型

热门文章

  1. anconda安装及opencv配置
  2. 【pytorch速成】Pytorch图像分类从模型自定义到测试
  3. NSIS 的 Modern UI 教程(二)
  4. 秘鲁农业功臣-国际农民丰收节贸易会:蔬菜用广州话发音
  5. Spring JPA使用CriteriaBuilder动态构造查询
  6. 爬虫_python3_requests_2
  7. 深入理解Spring AOP思想
  8. display:none和visible:hidden两者的区别
  9. 数据库存在即更新的高并发处理 - 转
  10. log4net报错集