Skip to content

Support for Delayed Messages Plugin

Atallah0 edited this page Jul 22, 2024 · 5 revisions

The RabbitMQ Delayed Message Plugin is still in the experimental phase.
You use it at your own risk.

The RabbitMQ Delayed Message Plugin adds a new exchange type to RabbitMQ which allows to delay message delivery.

EasyNetQ provides support for using that exchange by defining a new scheduler type: DelayedExchangeScheduler.

This allows you to use the same Future Publish interface as before. The below example shows how to publish a message which will get delivered within three months from now:

For EasyNetQ v8.x:

var serviceCollection = new ServiceCollection();
serviceCollection.AddEasyNetQ("host=localhost").UseDelayedExchangeScheduler();

using var provider = serviceCollection.BuildServiceProvider();
var bus = provider.GetRequiredService<IBus>();

var followUpCallMessage = new FollowUpCallMessage( .. );

bus.Scheduler.FuturePublish(DateTime.UtcNow.AddMonths(3), followUpCallMessage);

The UseDelayedExchangeScheduler method instructs EasyNetQ to use a new scheduler that supports Delayed Message Exchanges. Then, retrieve the bus instance from the DI service provider. Next, the message is created and published with a delivery time set to three months. Note that FuturePublish uses UTC time.

For EasyNetQ v7.x:

bus = RabbitHutch.CreateBus("host=localhost", x => x.EnableDelayedExchangeScheduler());

var followUpCallMessage = new FollowUpCallMessage( .. );

bus.Scheduler.FuturePublish(DateTime.UtcNow.AddMonths(3), followUpCallMessage);

The first line instructs EasyNetQ to use a new scheduler that supports Delayed Message Exchanges. Next, the message is created and published with delivery time set to three months. Note that FuturePublish uses UTC time.

DelayedExchangeScheduler requires Delayed Message Plugin to be installed.

Plugin installation

Delayed Message Plugin can be found on Community Plugins page. Download the corresponding .ez file for your RabbitMQ installation, copy it into RabbitMQ's plugin folder and then enable it by running the following command:

rabbitmq-plugins enable rabbitmq_delayed_message_exchange

The plugin requires RabbitMQ version 3.4 or newer.

How it works

When you call bus.Scheduler.FuturePublish(...), EasyNetQ automatically creates new x-delayed-message exchange along with normal exchange and binds them together. The message is published on the delayed exchange which will store the message until the time comes to deliver it. At that point, the message is routed to normal exchange and from there to bound queue(s).

When you call Publish(...) method, the messages are published to normal exchange which prevents any performance degradation related to using new x-delayed-message exchange.

The delayed exchange persists messages using Mnesia. That prevents messages loss in case of server downtime. Once the server restores, all eligible messages will be delivered on schedule.

Clone this wiki locally