RabbitMQ allows you to set TTL (time to live) for both messages and queues. This can be done using optional queue arguments or policies (the latter option is recommended).
从队列维度设置消息超时时间:
Message TTL can be set for a given queue by setting the message-ttl argument with a policyor by specifying the same argument at the time of queue declaration.
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.
The server guarantees that dead messages will not be delivered using basic.deliver (to a consumer) or included into a basic.get-ok response (for one-off fetch operations). Further, the server will try to remove messages at or shortly after their TTL-based expiry.
单位毫秒,类型整形
To specify a TTL using policy, add the key "message-ttl" to a policy definition:
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.
Define Message TTL for Queues Using x-arguments During Declaration
This example in Java creates a queue in which messages may reside for at most 60 seconds:
Map<String, Object> args = new HashMap<String, Object>(); args.put("x-message-ttl", 60000); channel.queueDeclare("myqueue", false, false, false, args);
The same example in C#:
var args = new Dictionary<string, object>(); args.Add("x-message-ttl", 60000); model.QueueDeclare("myqueue", false, false, false, args);
如果队列里已经有消息了,然后又设置了这个值,会有一些警告
如果之前队列的消息 消费失败有重新入队还会保留之前的过期时间。
从消息维度设置消息过期时间: 使用expiration 函数
byte[] messageBodyBytes = "Hello, world!".getBytes(); AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder() .expiration("60000") .build(); channel.basicPublish("my-exchange", "routing-key", properties, messageBodyBytes);
如果两个维度都设置了 过期时间,以小值为准。
两个维度的区别
队列维度 : the server will try to remove messages at or shortly after their TTL-based expiry.
消息维度:Queues that had a per-message TTL applied to them retroactively (when they already had messages) will discard the messages when specific events occur. Only when expired messages reach the head of a queue will they actually be discarded (or dead-lettered)
当设置每个消息的TTL过期消息时,已过期消息可能会在未过期的消息后面排队,直到已过期消息被处理(使用或丢弃)。因此,这些过期消息使用的资源将不会被释放,它们将在队列统计数据中计数(例如队列中的消息数量)。
当追溯应用每条消息的TTL策略时,建议让消费者在线,以确保更快地丢弃消息。
考虑到现有队列上的每个消息TTL设置的这种行为,当需要删除消息以释放资源时,应该使用队列TTL(或队列清除或队列删除)。
设置队列过期时间
当队列超过指定时间未使用的时候,自动删除
This can be used, for example, for RPC-style reply queues, where many queues can be created which may never be drained.
The following policy makes all queues expire after 30 minutes since last use:
rabbitmqctl | rabbitmqctl set_policy expiry ".*" '{"expires":1800000}' --apply-to queues |
---|---|
rabbitmqctl (Windows) | rabbitmqctl.bat set_policy expiry ".*" "{""expires"":1800000}" --apply-to queues |
Define Queue TTL for Queues Using x-arguments During Declaration
This example in Java creates a queue which expires after it has been unused for 30 minutes.
Map<String, Object> args = new HashMap<String, Object>(); args.put("x-expires", 1800000); channel.queueDeclare("myqueue", false, false, false, args);