9

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

7
  • can you show some code? like where you set up your channels / queues etc.. Commented Sep 22, 2015 at 14:38
  • Do you make messages themselves persistent, using delivery_mode message property? Commented Sep 22, 2015 at 14:42
  • @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 it Commented Sep 22, 2015 at 14:45
  • @Jay I see that unless you explicitly set "persistentMessages=false" in your connection string, that should be true by default and it should use correct delivery_mode (=2). So we need some more info from you. Commented Sep 22, 2015 at 14:47
  • 1
    Let us continue this discussion in chat. Commented Sep 22, 2015 at 14:55

4 Answers 4

10

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));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note IBasicProperties has a Persistent property. So you can do props.Persistent = true;
4

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

1 Comment

I'm sure it works, but the question was regarding C#, not Java.
1

Add these two lines after binding queue:

var properties = model.CreateBasicProperties();
properties.Persistent = true;

Comments

0

Have you tried to enable the Lazy Queue? "Lazy Queues - queues that move their contents to disk as early as practically possible"

It can be enabled on the Policy level (my preference) or specific queue.

Full explanation is here https://www.rabbitmq.com/lazy-queues.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.