A great concern in many web applications is how scheduled jobs can be executed. There are many job scheduling frameworks available and Quartz.NET is one of the most popular. Quartz.NET is an open source job scheduling framework written in C# for .NET. It is the .NET port of the popular Java job scheduling framework. The Quartz.NET official site states: "Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems." In this article, we will discuss Quartz.NET and how it can be implemented in .NET applications.
To schedule a job using Quartz.NET you need the following:
- A scheduler service
- A trigger
- One or more jobs
You can use the NuGet Package Manager to install Quartz.NET in your application and add a reference to the Quartz.Net.dll file.
The first thing you need to do is create an instance of the Quartz.NET scheduler. You would then need to define the job that has to be executed. To create a job class in Quartz.NET, you need to implement the IJob interface. This interface contains one method, Execute. You will need to implement this method in your class and provide the code that your job is to execute.
Once you have a job defined, the next step is to associate a trigger with it. The trigger will contain the metadata of the job. This metadata should define the interval and frequency with which the job is to be executed. Once your job and trigger are defined, you need to assign the job to the scheduler instance you created earlier.
Creating a Job
A typical job class would look like this:
public class TestJob : IScheduledJob
public void Execute(IJobExecutionContext context)
//Code to write a message to a log file
The Job Scheduler
The following code snippet illustrates how the JobScheduler class would look:
public class TestJobScheduler
public static void Start()
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
IJobDetail job = JobBuilder.Create<TestJob>().Build();
ITrigger trigger = TriggerBuilder.Create()
.WithSimpleSchedule(s => s
Note how the scheduler instance has been created and the job instance assigned to it. The Create() method of the JobBuilder class is used to create a job instance. To be more precise, to create a job, the JobBuilder.Create<T> method is used. The generic parameter to this method is the job type. In our example, TestJob is the job type.
The Create() method of the TriggerBuilder class is used to specify the job scheduler. The trigger defines how and when the job is to be executed. In this example, the job would be executed indefinitely every minute. It would start immediately and log a message to the log file (as defined in the Execute method of the TestJob class) every minute.
The job is assigned to the scheduler instance and scheduled by making a call to the SchedulerJob() method of the IScheduler interface. This method accepts the job instance and the trigger instance as parameters and schedules the job. In essence, this method registers the job and the trigger with the scheduler.
Calling the JobScheduler
If you would want the job you have created to be fired in your ASP.NET web application, you can make a call to the start method of the job scheduler you have implemented. The following code snippet illustrates this:
public class Global : HttpApplication
void Application_Start(object sender, EventArgs e)
The Application_Start event handler in the Global.asax.cs file is the best place for you to invoke the start method of the scheduler, as it would be called the first time the application is started. Note that if the IIS is restarted, then this event will be called afresh if the application is executed.
The Quartz.NET framework also provides you with the ability to persist job states. To persistent storage, you would need to use the IJobStore interface and take advantage of AdoJobStore to persist the jobs and triggers in a relational database. If you use the RAMJobStore you can persist the jobs and the associated triggers in the RAM. Other striking features of this framework include support for clustering, plugins and listeners.
In this article we explored the popular open source job scheduling framework, Quartz.NET. Happy reading!