Introduction
NCron is a .Net scheduling framework, it is a .Net version of
Cron
- the time based job scheduler found on unix like operating systems or
Cron4j
- scheduling library for Java. Ncron is light weight and easy to use, with little
learning curve. It comes with some cool advantages, being that you can use it in
C#, Vb.net or any other .Net programming language. It takes your mind off the details
of scheduling and you can focus on how to implement the business logic of your application
or the job to be scheduled. Details such as threading and timers have been taken
care of.
Ncron Library
You can point your browser to
http://code.google.com/p/ncron/downloads/detail?name=ncron-2.1.zip to download
the ncron library. You need to add reference to the Ncron library in your project
so as to be able to access the classes and functionalities of the Ncron scheduling
framework.
Scheduling a Job
When creating a job to be scheduled using NCron, the job is wrapped up in a class
which must extend the class
NCron.CronJob and override a void method
Execute
public class MyJob : NCron.CronJob
{
public override void Execute()
{
System.IO.File.Copy(@"c:\\output.out", @"f:\\output.out");
}
}
The job to be scheduled will be placed in the Execute method. The next thing to
do is to give NCron control over the job execution, by calling the static method
Bootstrap.Init()at the entry point of your application, for example this
can be put in the Main method. You should have a static setup method, which
I called JobSetup method that will be passed into the Bootstrap.Init() method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCron.Fluent.Crontab;
using NCron.Fluent.Generics;
using NCron.Service;
namespace NcronExample
{
public class Program
{
private static void Main(string[] args)
{
Bootstrap.Init(args, JobSetup);
}
private static void JobSetup(SchedulingService schedulingService)
{
schedulingService.At("* * * * *").Run()<MyJob>;
}
}
}
The line of code inside the JobSetup method is to specify how the Job is going to
be run, and the parameter in the schedulingService.At() method is known as crontab
expression which I will discuss shortly. The SchedulingService class has a number
of methods of interest.
service.Daily().Run<MyJob>(); //runs the scheduled job once
every day
service.Hourly().Run<MyJob>(); //runs the scheduled job once
every hour
service.Weekly().Run<MyJob>(); //runs the scheduled job once
every week
Crontab Expression
A crontab expression is a string comprising of 5 characters, which are seperated
by space. This crontab expression when parsed produces occurrences of time based
on a given schedule expressed in the crontab format. NCron parses crontab expression
through the use of
NCrontab(Crontab for .Net) an open source library
for parsing crontab expressions.
A regular crontab expression is of the form
* * * * *
where the first * is for minute which can be from 0-59.
The second * is for hour which can also be from 0-23.
The third * is for day of the month from 1-31.
The fourth * is for month from 1-12.
The last * is for day of week from 0-6 where 0 represents Sunday.
The asterisk or wildcard character if left in the expression indicates all valid
or legal values for that column.
If yIf you want the scheduled job to run every minute, the expresion will be
in the form below.
* * * * *
The The expression below causes the scheduler to run the job at the fifth minute
of every ninth hour everyday.
5 9 * * *
To run a job every tenth minute of every hour from Monday to Friday only, the expression
will be in the form below.
10 * * * 1,2,3,4,5
You can read more on crontab expressions at
http://code.google.com/p/ncrontab/wiki/CrontabExamples
Deploying the Scheduled Job
After the application has been built and compiled, you can deploy the scheduled
job as a service by opening command prompt and change directory to where the
executable of the application is and then run the command.
ncronexample install
To install the scheduled job as a service, and that is it !!!