Message persistence in RabbitMQ
Asked Answered
A

4

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

Akihito answered 22/9, 2015 at 14:28 Comment(7)
can you show some code? like where you set up your channels / queues etc..Sheugh
Do you make messages themselves persistent, using delivery_mode message property?Eades
@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 itAkihito
@Akihito 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.Eades
@Eades 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 testAkihito
@Akihito I mean it should work by default, so you don't need to set that explicitly. In other words - should work as you describe.Eades
Let us continue this discussion in chat.Eades
H
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));
}
Hester answered 22/11, 2016 at 17:58 Comment(1)
Note IBasicProperties has a Persistent property. So you can do props.Persistent = true;Girlfriend
A
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());
Afford answered 4/5, 2018 at 5:50 Comment(1)
I'm sure it works, but the question was regarding C#, not Java.Whithersoever
S
1

Add these two lines after binding queue:

var properties = model.CreateBasicProperties();
properties.Persistent = true;
Snakeroot answered 7/8, 2022 at 10:34 Comment(0)
I
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

Ingratiating answered 15/10, 2021 at 4:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.