Home > dotnet 4, Dotnet 4.5 > Quartz.Net


In this post I am going to talk about yet another open source project Quartz.net which is simple but powerful way of scheduling jobs in .NET. In any enterprise wide development I am sure you have come across situation where you need to run jobs at specific interval,frequency, at a specific day of the week etc.

Quartz.net API is pretty simple and all it cares about is any class which implements the IJob interface and when you wire that up to the Quartz scheduler class and it will take care of the scheduling.

Lets start cutting some and I am going to create a simple ToDoJob class which implements the IJob interface.


    using System;
    using Quartz;
    public class ToDoJob : IJob
        public void Execute(IJobExecutionContext context)
            Console.WriteLine("Job is executing - {0}.", DateTime.Now);

Now I am going to create my own interface which will encapsulate the Quartz scheduler and decouple the Quartz component.


using System;
using Quartz;

public interface IJobScheduler

    void ScheduleJob<T>(string groupName, string jobDetails, 
                        string triggerName, TimeSpan repeatInterval)
                        where T : IJob;

The implementation of this interface is pretty simple as you can see I am using the Standard Scheduler factory provided by Quartz and from there we can get the scheduler object. After that you just need to create a simple trigger object and wire up a few setting like when it needs to be started, how long the job needs to run for etc.


using System;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Triggers;

public class JobScheduler : IJobScheduler
    private readonly IScheduler scheduler;
    private readonly ISchedulerFactory schedFact;

    public JobScheduler()
        this.schedFact = new StdSchedulerFactory();

        // get a scheduler
        this.scheduler = this.schedFact.GetScheduler();

    public void ScheduleJob<T>(string groupName, string job, 
                               string triggerName, TimeSpan repeatInterval) 
                                where T : IJob
        var trigger = new SimpleTriggerImpl

        var jobDetail = new JobDetailImpl(job, groupName, typeof(T));
        this.scheduler.ScheduleJob(jobDetail, trigger);

As you can see from the above code I am configuring a trigger which start immediately and runs indefinitely at an interval specified in the parameter.

So our Job scheduler class is ready and we have define the schedule job method in such a way that it takes any IJob interface along with necessary scheduling parameters to schedule a job. Now the only thing left is use the class and schedule a job.

Below is the main program where I am asking the ToDoJob class to execute every 10 seconds and this is just for illustration in real life enterprise application this could be inside a config or whatever requirements you have.


using System;

public class Program
    public static void Main(string[] args)
        IJobScheduler scheduler = new JobScheduler();

And here is the output of the ToDoJob.

Quartz Scheduler Running

As you can see this is pretty neat and if you guys remember a long time ago I blog about TopShelf if you haven’t read it or not heard of it then I highly recommend to read about it here. If you wireup the Quartz Scheduler with the TopShelf component then you get a pretty robust scheduling platform for your enterprise.

I just love doing these small project where I integrate various component and see these lego blocks coming up together and have fun with it and I hope you guys will enjoy playing with these lego blocks as much as I do.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: