Thursday, May 19, 2011

FuturePublish with the EasyNetQ RabbitMQ API

If you’re a regular reader, you’ll remember that I published an initial version EasyNetQ, my .NET friendly API for RabbitMQ, earlier this month. Today I want to show off a little addition that allows messages to be scheduled for publishing at a future date.

Many business scenarios require some kind of scheduling. For example, say I want to send a party invitation, but I know that it will be forgotten if I send it too early. Instead I want it to arrive two days before the party. I’d like to ‘future publish’ my invite at the time I’m planning my party, and let the messaging system worry about sending it two days before hand.

I’ve added a FuturePublish method to the EasyNetQ, you simply give it a messasge and the time that you want it sent.

var invitation = new PartyInvitation
{
Text = "Please come to my party",
Date = new DateTime(2011, 5, 24)
};

bus.FuturePublish(invitation.Date.AddDays(-2), invitation);

That’s cool, how does it work?

Internally the FuturePublish method wraps the given message in a special ‘Schedule Me’ message. This message is then published to Rabbit as normal. A windows service, EasyNetQ.Scheduler, subscribes to the Schedule Me messages which it writes to its database. At a pre-defined interval, it polls its database looking for messages who’s publish date is the current time, retrieves the wrapped message and publishes it.

Check out the source on GitHub here:

https://github.com/mikehadlow/EasyNetQ

4 comments:

Octoberclub said...

Great idea. Wouldn't it also be useful if you could create recurring schedules? e.g. 'Runs every day at 9.00am' or 'First Monday of every month' for instance. Or would that be up to the publisher to implement?

Mike Hadlow said...

Hi Michael,

Yes, that would be an excellent addition. There are plenty of business scenarios that have recurring events. Something like RecurringPublish ??

Anonymous said...

Good idea. You can use DDay.ICal for help with recurrence handling.

Octoberclub said...

DDay.ICal looks good and I think it already handles Timezones. There's also Quartz.Net, but it could be too heavyweight perhaps.