6
votes

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

2
can you show some code? like where you set up your channels / queues etc..Alex
Sorry, Its not allowing me to edit my post now.Jay
Do you make messages themselves persistent, using delivery_mode message property?Evk
@Evk: that what i am trying to figure out, where do i set delivery_mode. i have read about it but cant get my head around where/how to do itJay

2 Answers

6
votes

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));
}
3
votes

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());