Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to mark a message as persistent using spring-rabbitmq?

This is how I'm creating an exchange and binding a queue to it

<rabbit:topic-exchange id="dataExchange" name="MQ-EXCHANGE" durable="true">
        <rabbit:bindings>
            <rabbit:binding queue="COMM_QUEUE" pattern="queue.*" />
        </rabbit:bindings>
</rabbit:topic-exchange>

I have read a lot of posts on the Internet where it is written that a message is also needed to be marked persistent if it is to be secured in case rabbitmq or the queue crashes. But I couldn't figure out how to mark my messages persistent.

This is how I'm publishing the messages to the queue

    @Autowired
    private RabbitTemplate template;

    @Override
    public void produceMessage(Object message, String routingKey) {
        template.convertAndSend(routingKey, message);  
    }

I looked for different API methods to know this and also tried to look for any specific property that I could configure in the XML but couldn't find a way. Any guidance ?

like image 471
Lalit Mehra Avatar asked Jul 05 '16 14:07

Lalit Mehra


People also ask

How do I persist RabbitMQ messages?

Persistent messages will be written to disk as soon as they reach the queue, while transient messages will be written to disk only so that they can be evicted from memory while under memory pressure. Persistent messages are also kept in memory when possible and only evicted from memory under memory pressure.

What is durability in RabbitMQ?

RabbitMQ Durable queues are those that can withstand a RabbitMQ restart. If a queue is not durable, all messages will be lost if RabbitMQ is shut down for any reason. For messages to survive restarts, both of these configurations must be true.

What is @rabbitlistener?

Annotation that marks a method to be the target of a Rabbit message listener on the specified queues() (or bindings() ). The containerFactory() identifies the RabbitListenerContainerFactory to use to build the rabbit listener container.


1 Answers

The default delivery mode (in MessageProperties) is PERSISTENT. See here.

To make it non-persistent you need to use a convertAndSend(...) method with a MessagePostProcessor to set the deliveryMode property.

like image 127
Gary Russell Avatar answered Oct 05 '22 21:10

Gary Russell