Quartz.net Schedulling Jobs

Introduction:

Sometimes when you develop an application, you are going to face some tasks needs to¬†perform a certain action at a given time without any user interaction, example for that a User wants to set an alert in specific time to remind him with a date Or something else like updating records in database periodically. All this tasks needs non-human interaction, so how could you tell the machine to do this job ? that’s the question. Cron Job is the solution for this tasks. Quartz.net has presented an excellent open source job scheduler that can be used for these purposes.

What is Quartz ?

Quartz is a job scheduling system that can be integrated with, or used along side virtually any other software system. The term “job scheduler” seems to conjure different ideas for different people. As you read this tutorial, you should be able to get a firm idea of what we mean when we use this term, but in short, a job scheduler is a system that is responsible for executing (or notifying) other software components when a pre-determined (scheduled) time arrives.

Job , Schedule & Trigger:

At the beginning we have to recognize how the Quartz works. Jobs are Scheduled to run when a given Trigger occurs. Triggers can be created with nearly any combination of the following directives:

  • at a certain time of day (to the millisecond)
  • on certain days of the week
  • on certain days of the month
  • on certain days of the year
  • not on certain days listed within a registered Calendar (such as business holidays)
  • repeated a specific number of times
  • repeated until a specific time/date
  • repeated indefinitely
  • repeated with a delay interval

Quartz in Action:

Let’s go inside the code and see how Quartz works. First we will create a new Console application and get Quartz.net from Nuget Packages.

Now create a new class let’s name it MyJob, this class will be the job that we want it to be done in specific time, as we mention above it has to inherit from IJob interface.

Back to the Program.cs class in the Main method we are going to Schedule our Job with a Trigger as we said above.

Let’s explain our code. in the first two lines of code we are instantiating the Schedule which will going to register our job in it.

After that we will specify what time we want this job to run. The DateBuilder class is responsible to create our DateTimeOffset which will meet our need.

This line means that we want our Job to run at the beginning of the next minute. And now we have to build our Job by using JobBuilder class.

Here we are building a job and specify a name for this job – “job1- ” and group name – “group1”.

JJobs and Triggers are given identifying names as they are registered with the Quartz.NET scheduler. Jobs and triggers can also be placed into ‘groups’ which can be useful for organizing your jobs and triggers into categories for later maintenance. The name of a job or trigger must be unique within its group – or in other words, the true identifier of a job or trigger is its name + group. If you leave the group of the Job or Trigger ‘null’, it is equivalent to having specified SchedulerConstants.DefaultGroup.

After that we have to create Trigger for this job by using TriggerBuilder class.

Here we are saying to the trigger to start this job at the time which we were specifying before.

And then just we will schedule our job and start it. Notice that there is a line of code

this line is just to ensure that a minute will pass, because in our example we want the job to run at the beginning of the next minute. So this line is related to our example not always required.

At last we shut down the schedule. This was a simple example for Quartz.net, I’ll let you discover the whole features in this great library.

References

Quartz.net

FAQs

Download Sample