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
In standard queues, messages are retained for at least 72 hours and will be deleted 72 hours later.
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.
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.
Messages are stored in subdirectories of the node's data directory.
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));
}
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With