Scheduling messages using RabbitMQ delayed exchange

1,384 views
Skip to first unread message

rvanoord

unread,
Feb 10, 2020, 3:58:28 AM2/10/20
to masstransit-discuss
Hi,

The MassTransit documentation indicates that it is possible to use RabbitMQ delayed exchanges for scheduling messages.

In the example, the only configuration required is: 
var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
    cfg
.UseDelayedExchangeMessageScheduler();

However, when I try this configuration, no scheduled (delayed) messages are received at the destination queue.

As a work-around, I created a message consumer which handles ScheduleMessage<T> (where T is the actual message type) which subsequently delays messages using context.Defer(...). This seems to work, however we are getting a lot of traffic in the message queue which I can't explain.
       
        public virtual Task Consume(ConsumeContext<ScheduleMessage<T>> context)
        {
            var delay = context.Message.ScheduledTime - DateTime.UtcNow;
            if (delay > TimeSpan.Zero)
            {
                return context.Defer(delay);
            }
            if (context.ExpirationTime.HasValue && context.ExpirationTime.Value < DateTime.UtcNow)
            {
                return Task.CompletedTask;
            }
            return context.Send(context.DestinationAddress, context.Message.Payload);
        }

My question is whether this is the correct / expected way of using the delayed exchange message scheduler? I.e. is it really necessary to create a consumer for ScheduleMessage<T>, or is the call to UseDelayedExchangeMessageScheduler() all that should be required.



Chris Patterson

unread,
Feb 10, 2020, 9:29:48 AM2/10/20
to masstrans...@googlegroups.com
How are you sending the initial scheduled message? If you're sending it within a consumer, the configuration you specified is all that is required.


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/25b2fbf5-10ca-4b6d-bee3-8b5b2cb4cd2c%40googlegroups.com.

Reng van Oord

unread,
Feb 11, 2020, 2:25:19 AM2/11/20
to masstrans...@googlegroups.com
Thanks for your reply!

I noticed yesterday that the rabbitMQ delayed message exchange plugin only supports a delay of 2^32-1 milliseconds (roughly 49 days). If this limit is exceeded, messages are sent with no delay. A work-around is to delay messages a maximum of 49 days and subsequently reschedule them to extend the delay. Is this limitation already catered for in the default DelayedExchangeScheduleMessageProvider?


Chris Patterson

unread,
Feb 11, 2020, 2:06:26 PM2/11/20
to masstrans...@googlegroups.com
No, honestly, I can't imagine scheduling a message that far in advance. If you exceed the limit, you're on your own.

An issue/PR to throw an exception if the duration is outside the limit would be accepted.



Eric Piraux

unread,
Feb 12, 2020, 3:52:37 AM2/12/20
to masstrans...@googlegroups.com
Hi,

I do agree with Chris, a messaging system is not the place to store data for a long period. As soon as arrived the message should be processed.
If you need to fire an event after a long period of time put it somewhere in a database (with a publishdatetime) and poll it regularly.

Eric

Reply all
Reply to author
Forward
0 new messages