Book Image

Learning NServiceBus - Second Edition

By : David Boike
Book Image

Learning NServiceBus - Second Edition

By: David Boike

Overview of this book

Table of Contents (18 chapters)
Learning NServiceBus Second Edition
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Scheduling


By now, you're probably thinking that Saga timeouts sound great, but what if you just need to run something on a schedule? Maybe you're thinking you'll create an IWantToRunWhenBusStartsAndStops class with a Timer. Well if you are, stop right there! NServiceBus can bring the power of timeouts to you without the full ceremony of a saga:

public class ScheduleTasks : IWantToRunWhenBusStartsAndStops
{
  public IBus Bus { get; set; }
  public Schedule Schedule { get; set; }

  public void Start()
  {

    Schedule.Every(TimeSpan.FromMinutes(5)).Action(() =>
    {
      Bus.Send<DoSomethingEvery5MinutesCmd>();
    });

    Schedule.Every(TimeSpan.FromMinutes(5)).Action("Task Name", () =>
    {
      Bus.Send<DoNamedTaskEvery5MinutesCmd>();
    });
  }

  public void Stop() { }
}

The scheduler is like a mini-saga that can send messages at particular times. The important thing to keep in mind while using schedules is to steer clear of any custom logic. The scheduler should...