I'm using spring-boot with rabbitMQ and I'm wondering if I can use per message TTL using RabbitTemplate. So far I have :
@Autowired
private RabbitTemplate rabbit;
public void produce() {
rabbit.convertAndSend("My.Queue", routingKey, message);
}
You can create MessagePostProcessor and use it in you rabbitTemplate:
final String message = "message";
final MessagePostProcessor messagePostProcessor = new MyMessagePostProcessor(10000);
rabbitTemplate.convertAndSend("my.queue", "routingKey", message, messagePostProcessor);
This will catch you message, apply changes and send further.
public class MyMessagePostProcessor implements MessagePostProcessor {
private final Integer ttl;
public MyMessagePostProcessor(final Integer ttl) {
this.ttl = ttl;
}
@Override
public Message postProcessMessage(final Message message) throws AmqpException {
message.getMessageProperties().getHeaders().put("expiration", ttl.toString());
return message;
}
}
rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE,
RabbitMqConfig.RK,
String.valueOf(body),
message -> {
message.getMessageProperties().setExpiration(String.valueOf(1000));
return message;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With