Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Scheduling With Quartz.Net

4.69/5 (9 votes)
5 Jan 2015CPOL1 min read 77.9K  
How to do scheduling with Quartz.Net

The other day, I had a requirement to schedule something in my app to run at certain times, and at fixed intervals thereafter. Typically, I would just solve this using either a simple Timer, or turn to my friend Reactive Extensions by way of Observable.Timer(..).

The thing is that I decided to have a quick look at something I have always known about but never really used, for scheduling, which is Quartz.net, which actually does have some pretty good documentation up already:

For me, I just wanted to get something very basic up and running, so I gave it a blast.

Step 1: Install Quartz.net

This is as easy as installing the following NuGet package “Quartz“.

Step 2: Create A Job Class

This again is fairly easy, thanks to Quartz nice API. Here is my job class

C#
using System;
using Quartz;
 
namespace FooBar
{
    public class LoggingJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
 
 
            Common.Logging.LogManager.Adapter.GetLogger("LoggingJob").Info(
                string.Format("Logging job : {0} {1}, and proceeding to log", 
                    DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
 
        }
    }
}

That is all you need for a job really. The context object gives you access to a lot of useful stuff.

Step 3: Setting Up A Schedule

Again, this was mega easy, all I had to do was something like this:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using Common.Logging;
 
using Quartz;
using Quartz.Impl;
 
namespace FooBar
{
    class Program
    {
 
        private static ILog Log = LogManager.GetCurrentClassLogger();
 
        static void Main(string[] args)
        {
            try
            {
                // construct a scheduler factory
                ISchedulerFactory schedFact = new StdSchedulerFactory();
 
                // get a scheduler
                IScheduler sched = schedFact.GetScheduler();
                sched.Start();
 
                IJobDetail job = JobBuilder.Create<LoggingJob>()
                    .WithIdentity("myJob", "group1")
                    .Build();
 
                ITrigger trigger = TriggerBuilder.Create()
                    .WithDailyTimeIntervalSchedule
                    (s =>
                        s.WithIntervalInSeconds(10)
                            .OnEveryDay()
                            .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(10, 15))
                    )
                    .Build();
 
                sched.ScheduleJob(job, trigger);
            }
            catch (ArgumentException e)
            {
                Log.Error(e);
            } 
        }
    }
}

And that was enough for my job to get scheduled, every 10 seconds starting at 10:15 AM.

I was fairly happy with Quartz, and I will certainly make more use of it, when I have bigger, bolder, badder scheduling needs.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)