Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Task Scheduling in .NET Applications Using Quartz.Net

Explore Quartz.NET, the popular open source job scheduling framework.


advertisement

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.

Getting Started

To schedule a job using Quartz.NET you need the following:

  1. A scheduler service
  2. A trigger
  3. 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:

using System;
using Quartz;
namespace DevXJobSchduler
{
    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:

using System;
using Quartz;
using Quartz.Impl;
namespace DevXJobSchduler
{
    public class TestJobScheduler
    {
        public static void Start()
        {
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();
             IJobDetail job = JobBuilder.Create<TestJob>().Build();
             ITrigger trigger = TriggerBuilder.Create()
   	 .WithIdentity("trigger1", "group1")
    	.StartNow()
    	.WithSimpleSchedule(s => s
        	.WithIntervalInSeconds(60)
        	.RepeatForever())
    	.Build();
             scheduler.ScheduleJob(job, trigger);
        }
    }
}

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:

using DevXJobSchduler;
using System;
using System.Web;
using System.Web.Optimization;
using System.Web.Routing;
namespace DevXWeb
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            //Some code
            TestJobScheduler.Start();
        }
    }
}

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.

Summary

In this article we explored the popular open source job scheduling framework, Quartz.NET. Happy reading!

Suggested Reading



   
Joydip Kanjilal has over 10 years of industry experience with C, C++, Java, C#, VB, VC++, ASP.Net, XML, Design Patterns, UML, etc. He currently works as a senior project leader in a reputable multinational company in Hyderabad, India, and has contributed articles on .NET and related technologies to www.aspalliance.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date