Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message persistence in RabbitMQ

I am writing a small app where i am using RabbitMQ to send/Receive a message. Everything is working ok, but i am struggling with message persistence.

I want message to remain in a Queue even in server restarts. I understand the concept of durability at exchange and Queue level and have set them to true (rather they are default as true). So when i restart my RabbitMQ server, exchange and Queue remains intact but the messages in a queue are deleted.

I am using EasyNetQ.IBus interface to send messages.

Thanks

like image 266
Jay Avatar asked Sep 22 '15 14:09

Jay


People also ask

How long do messages stay in RabbitMQ?

In standard queues, messages are retained for at least 72 hours and will be deleted 72 hours later.

How do you make RabbitMQ persist messages?

Set message delivery mode to persistent Messages can be published either having a delivery mode set to persistent or transient. You need to set delivery mode to persistent when publishing your message, if you would like it to remain in your durable queue during restart.

Is message queue persistent?

Queues make your data persistent, and reduce the errors that happen when different parts of your system go offline. By separating different components with message queues, you create more fault tolerance.

Where are RabbitMQ messages stored?

Messages are stored in subdirectories of the node's data directory.


2 Answers

Using RabbitMQ.Client, you can set the delivery mode using IBasicProperties, which can be obtained using the method IModel.CreateBasicProperties().

using (IConnection conn = factory.CreateConnection())
using (IModel channel = conn.CreateModel())
{
    channel.ExchangeDeclare(exchange, ExchangeType.Direct, durable: true);
    channel.QueueDeclare(queue, durable: true, exclusive: false, autoDelete: false, arguments: null);
    channel.QueueBind(queue, exchange, routingKey, null);

    var props = channel.CreateBasicProperties();
    props.Persistent = true; // or props.DeliveryMode = 2;

    channel.BasicPublish(exchange, routingKey, props, Encoding.Default.GetBytes(message));
}
like image 165
Vladimir Shiyanov Avatar answered Oct 02 '22 13:10

Vladimir Shiyanov


To make your message persistent in RabbitMQ, you need to add MessageProperties.PERSISTENT_TEXT_PLAIN in your code.

import com.rabbitmq.client.MessageProperties;

channel.basicPublish("", "task_queue",
        MessageProperties.PERSISTENT_TEXT_PLAIN,
        message.getBytes());
like image 25
andani Avatar answered Oct 02 '22 15:10

andani