RabbitMQ TTL过期
设置具体的某一条消息或者队列中所有的消息过期时间
SpringBoot Queue设置TTL过期
@Bean
public Queue ttlQueue() {
Map<String, Object> props = new HashMap<>();
// TTL队列消息过期时间
props.put("x-message-ttl", 10000);
Queue queue = new Queue(RabbitMQConstants.TTL_QUEUE, false, false, false, props);
return queue;
}
@Bean
public Exchange ttlExchange(){
return new DirectExchange(RabbitMQConstants.TTL_EXCHANGE, false, false);
}
@Bean
public Binding ttlBinding(){
return BindingBuilder.bind(ttlQueue()).to(ttlExchange()).with(RabbitMQConstants.TTL_ROUTING_KEY).noargs();
}
SpringBoot 消息设置过期时间
MessageProperties properties = new MessageProperties();
// 消息过期时间
properties.setExpiration("3000");
Message msg = new Message(message.getBytes("utf-8"), properties);
amqpTemplate.convertAndSend(RabbitMQConstants.DIRECT_EXCHANGE, RabbitMQConstants.QUEUE_ROUTING_KEY, msg);