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

Jay
  • 1,037
  • 5
  • 23
  • 41
  • can you show some code? like where you set up your channels / queues etc.. – Alex Sep 22 '15 at 14:38
  • Do you make messages themselves persistent, using delivery_mode message property? – Evk Sep 22 '15 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 – Jay Sep 22 '15 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. – Evk Sep 22 '15 at 14:47
  • @Evk I tried this earlier and this didnt help ````_bus = RabbitHutch.CreateBus("host=abc;virtualHost=def;username=a;password=b;persistentMessages=true");````, I have read that by default persistence is true but it doesnt seems to be the case in my case. I am now manually setting ````message.properties.DelievryMode = 2```` and test – Jay Sep 22 '15 at 14:49
  • @Jay I mean it should work by default, so you don't need to set that explicitly. In other words - should work as you describe. – Evk Sep 22 '15 at 14:49
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90336/discussion-between-evk-and-jay). – Evk Sep 22 '15 at 14:55

4 Answers4

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));
}
Vladimir Shiyanov
  • 1,236
  • 16
  • 18
  • 4
    Note `IBasicProperties` has a `Persistent` property. So you can do `props.Persistent = true;` – Hamish Sep 05 '19 at 14:55
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());
Pang
  • 9,564
  • 146
  • 81
  • 122
andani
  • 414
  • 1
  • 9
  • 28
1

Add these two lines after binding queue:

var properties = model.CreateBasicProperties();
properties.Persistent = true;
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

tsuryadi
  • 421
  • 4
  • 9