A while ago, on a project I am currently involved with which is based on NServiceBus, we needed to publish certain pieces of information at fixed intervals. I was not totally clear in my head on how this could be implemented in an NServiceBus service, so I asked for help on Twitter, which resulted in a nifty piece of advice from Andreas Öhlund: Set up a timer to do a bus.SendLocal at the specified interval.

That’s exactly what we did, and I think we ended up with a pretty nifty piece of code that I want to show off 🙂

PS: bus.SendLocal(message) effectively does a bus.Send(((UnicastBus)bus).Address, message) – i.e. it puts a message, MSMQ and all, in the service’s own input queue.

First, we have an API that looks like this (looking a little funny, I know – wait and see…):

– which is implemented like this (registered as a singleton in the container):

The System.Timers.Timer is a timer which uses the thread pool to schedule callbacks at the specified interval. It’s pretty easy to use, and it fits nicely with this scenario.

Now, in combination with this nifty class of extension goodness:

– we can schedule our tasks like so:

Now, why is this good? It’s good because the actual task will then be carried out by whoever implements IHandleMessages<PublishRealTimeDataMessage> in the service, processing the tasks with all the benefits of the usual NServiceBus message processing pipeline.

Nifty, huh?

Looking over the simplicity and elegance of this solution, I’m kind of embarassed to tell that my first take on this was to implement the timer almost exactly like above, except instead of bus.SendLocal in the Elapsed-callback, we had a huge event handler that simulated most of our message processing pipeline – including NHibernateMessageModule, transactions, and whatnot….

Please note that ScheduleRealTimeDataPublishing is not re-entrant – in this form its Every method should only be used from within the Run and Stop methods of implementors of IWantToRunAtStartup, as these are run sequentially.

Scheduling recurring tasks in NServiceBus

6 thoughts on “Scheduling recurring tasks in NServiceBus

  • 2010-10-07 at 18:29
    Permalink

    Where/how did you register ISchedule? I’m trying to do it during init(IWantCustomInitialization) and it is not resolving as I think IBus has not been created yet…

    Reply
  • 2010-10-07 at 19:05
    Permalink

    I register everything in the Init method of my EndpointConfig, but at this point there’s no dependency injection! This makes sense, because the Init method is typically where you would provide an IoC container of your own.

    Why not just make a class implement IWantToRunAtStartup?

    If you want it to run before everything else, make the class that implements IConfigureThisEndpoint inherit IWantToRunAtStartup – it will be run before any other IWantToRunAtStartup

    Reply
  • 2010-10-07 at 19:08
    Permalink

    If you’re in doubt as to how to supply a container and register stuff in it, check this out (the “Containers and Dependency Injection” section)

    Reply
  • 2010-11-10 at 18:51
    Permalink

    This is exactly what I was looking for. Super, simple, elegant API.

    Thank you!

    Reply
  • 2011-05-23 at 10:53
    Permalink

    Nice (and elegant) solution to your real-time problem.
    But how would you approach this when trying to guarantee the publishing of a message in a much longer interval?

    I need to run a task once a month that assigns holiday credits to employees, if this doesn’t run that’s a big problem.

    Especially when I want to avoid sending the message twice if the server goes down on the day when the task is supposed to run..

    Reply
  • 2011-05-23 at 11:02
    Permalink

    I’d probably schedule a check at a much shorter interval – say 1 hour or so – and then each check would see if it was “that time of the month” 🙂

    I know this doesn’t take advantage of the nifty declarative API in ISchedule, but it would be a pretty simple and pragmatic solution to that problem.

    Your comment does raise some questions, though – maybe NServiceBus would benefit from a formalized scheduling API, similar to how the Timeout Service works? I wonder if NSB 3 will have some news in this regard…

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: