Monday, April 2, 2012

Quartz.NET

Quartz.NET is a very popular and widely used open source shedular framework. You can easily shedule thousands of complex job with Quartz.NET. Generally, we used timer feature in .net when need to develop sheduling based application. The limitation of times is that, we can not shedule a job by a specified month, day or year etc based and Timer does not have persistence mechanishm as well as it does not utilized the tread-pool. We can specify each job in a specified thread in the Quartz.NET. Here the example explain how to use the Quartz.NET in .net. Here I am using the Visual Studio 2010.

Steps:
1. Download the Quartz.NET dll of Quartz.NET 2.0 beta 1 from the following link  http://sourceforge.net/projects/quartznet/files/quartznet/
2. Created a Console application.
3. Add the refernce of Common.Logging.dll and Quartz.dll to the application from the downloaded folder Quartz.NET-2.0-beta1\bin\4.0\release\Quartz.
4. Add the app.config file and add the following configuration.

<configuration>
   <configSections>
     <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
   </configSections>
   <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
   </startup>
 </configuration>

5. Add the following code in the Main() function.

        static void Main(string[] args)
        {
            ISchedulerFactory obSchedulerFactory = new StdSchedulerFactory();
            IScheduler obIScheduler = obSchedulerFactory.GetScheduler();
            DateTimeOffset runTime = DateBuilder.EvenSecondDateAfterNow();
            IJobDetail job = JobBuilder.Create<MYJob>()
                .WithIdentity("job1""group1")
                .Build();
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1""group1")
                .StartAt(runTime)
                .EndAt(runTime.AddMinutes(1))
                .WithCronSchedule("0/5 * * * * ?")
                .Build();

            obIScheduler.ScheduleJob(job, trigger);
            obIScheduler.Start();
        }
      
6. Creat a MyJob class.

    public class MYJob : IJob
    {
        public MYJob()
        {
        }
        public virtual void Execute(IJobExecutionContext context)
        {
            Console.WriteLine(string.Format("My Job! - {0}", System.DateTime.Now.ToString("r")));
        }
    }
   
7. Open the property of the application and select the Target Framework to .NET Framework 4 from .NET Framework 4 Client Profile.

8. Run the application. The output will be as follow

My Job! - Mon, 02 Apr 2012 12:16:10 GMT
My Job! - Mon, 02 Apr 2012 12:16:15 GMT
My Job! - Mon, 02 Apr 2012 12:16:20 GMT
My Job! - Mon, 02 Apr 2012 12:16:25 GMT

The Execute() function of the MyJob class will run in each 5 second, since we had set the shedular for 5 second.
There are 3 main component of the Quartz.NET : Trigger, Scheduler and Jobs.

Jobs:   Job is an executable task, which is sheduled and trigger in a regular interval. To specify the executable task, you have to inherit the IJob Interface and implement the Execute() function. This Execute() method will be called in each interval. In this example, I have used MYJob class and inherited the IJob interface. In the Main() function I have created the instance of IJobDetail instance with the identity name "job1" and group name "group1" and specified the MyJob class to be called.

Trigger: Trigger refers the start, end and sheduler interval for the job. Here I have created the instance of ITrigger instance with the identity name "trigger1" and group name "group1".  The group name for both the job and trigger should be same, so that it can identify that which job is used for which shedule when there are more than one job and trigger. Here I have set the StartAt() to the current datetime and EndAt() after the one minute of current time. The expression "0/5 * * * * ?" specifies the Crone experession, which is used to tell the sheduling time. Here I 0/5 specify that this job will be executing in each 5 second.

Cron Expressions Allowed Fields and Values
Name
Required
Allowed Values
Allowed Special Characters
Seconds
Y
0-59
, - * /
Minutes
Y
0-59
, - * /
Hours
Y
0-23
, - * /
Day of month
Y
1-31
, - * ? / L W C
Month
Y
0-11 or JAN-DEC
, - * /
Day of week
Y
1-7 or SUN-SAT
, - * ? / L C #
Year
N
empty or 1970-2099
, - * /

Cron Expressions
Cron expressions can be as simple as * * * * ? * or as complex as 0 0/5 14,18,3-39,52 ? JAN,MAR,SEP MON-FRI 2002-2010.
Here are some more examples:
Expression
Means
0 0 12 * * ?
Fire at 12:00 PM (noon) every day
0 15 10 ? * *
Fire at 10:15 AM every day
0 15 10 * * ?
Fire at 10:15 AM every day
0 15 10 * * ? *
Fire at 10:15 AM every day
0 15 10 * * ? 2005
Fire at 10:15 AM every day during the year 2005
0 * 14 * * ?
Fire every minute starting at 2:00 PM and ending at 2:59 PM, every day
0 0/5 14 * * ?
Fire every 5 minutes starting at 2:00 PM and ending at 2:55 PM, every day
0 0/5 14,18 * * ?
Fire every 5 minutes starting at 2:00 PM and ending at 2:55 PM, AND fire every 5 minutes starting at 6:00 PM and ending at 6:55 PM, every day
0 0-5 14 * * ?
Fire every minute starting at 2:00 PM and ending at 2:05 PM, every day
0 10,44 14 ? 3 WED
Fire at 2:10 PM and at 2:44 PM every Wednesday in the month of March
0 15 10 ? * MON-FRI
Fire at 10:15 AM every Monday, Tuesday, Wednesday, Thursday and Friday
0 15 10 15 * ?
Fire at 10:15 AM on the 15th day of every month
0 15 10 L * ?
Fire at 10:15 AM on the last day of every month
0 15 10 ? * 6L
Fire at 10:15 AM on the last Friday of every month
0 15 10 ? * 6L
Fire at 10:15 AM on the last Friday of every month
0 15 10 ? * 6L 2002-2005
Fire at 10:15 AM on every last friday of every month during the years 2002, 2003, 2004, and 2005
0 15 10 ? * 6#3
Fire at 10:15 AM on the third Friday of every month
0 0 12 1/5 * ?
Fire at 12 PM (noon) every 5 days every month, starting on the first day of the month
0 11 11 11 11 ?
Fire every November 11 at 11:11 AM

Sheduler: Sheduler is used to grouped the job and trigger both in a group and start and end the sheduling. Here I have created the instance of ISheduler and grouped both the job and trigger and then started the sheduler.
Download: