概述
RabbitMQ支持隊列的過期時間設置,從消息入隊列開始,只要超過了隊列的超時時間配置,消息就會被自動清除。同時RabbitMQ也支持設置消息的生存時間,在發送消息時通過headers
參數可以設置。
設置隊列的生存時間(TTL)
在消費端
聲明隊列時,指定x-message-ttl
參數,設置生存時間
Map<String, Object> args = new HashMap<String, Object>();
//設置生存時間爲60分鐘
args.put("x-message-ttl", 60000);
channel.queueDeclare("myqueue", false, false, false, args);
設置消息的生存時間(TTL)
在生產端
,發送消息時指定消息的生存時間
byte[] messageBodyBytes = "Hello, world!".getBytes();
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
.expiration("60000")//設置生存時間爲60分鐘
.build();
channel.basicPublish("my-exchange", "routing-key", properties, messageBodyBytes);
參考:https://www.rabbitmq.com/ttl.html