If you need to run tasks on a schedule as part of your ASP.NET application with reliable accuracy and do not have access to various scheduling options directly on the server, or even want an alternative to messing around with the Windows task scheduler, there are many libraries, and Quartz.Net is one of the known ones out there.


WHAT IS Quartz.NET?

  • It is an open-source job scheduling system that can be used from smallest apps to large-scale enterprise systems.
  • Is a modulus and highly extensive approach for queuing or execution in the jobs and schedules.
  • A pure .Net library written in C# and is a port of the very popular open-source Java job scheduling framework, Quartz.

Quartz-1

WHY USE Quartz.NET?


    Quartz.NET provides a far more robust solution. You can ensure tasks only run   
    at specific times of the day (e.g., 4:25am), or only on specific days, or any 
    combination by using a Cron trigger. It also allows you to run multiple
    instances of your application in a clustered fashion, so that only a single 
    instance can run a given task at any one time.

   Quartz.NET consists of three primary components:

  • A job it refers to the background task that is required to run on particulars schedules (the task to be performed). 
  • A trigger it controls when a job runs, typically firing on some sort of schedule.
  • A scheduler it refers to the running jobs that are based upon the crackers under a time-based.


A diagram of how the schedulers, triggers and jobs components work together is below:

 

Quartz-2

Basic Example on Quartz.NET


Let us make a simple program to understand it through a practical example. Our example is a windows service that will print the message “HELLO FROM Quartz.NET” in the file, every 10 minutes.

  • The first step will be installing the latest stable Quartz NuGet package.

Quartz-3

  • Create the Job Class

That will print the message “HELLO FROM Quartz.NET” in the file. The class needs to implement the Job interface which lives in Quartz namespace.

Quartz-4

When the job’s trigger is fired the Execute method is invoked.

  • Setting up a Schedule

The program starts by getting an instance of Scheduler, this is done by creating a Std Scheduler Factory and then using it to create a scheduler.
A Job Scheduler is a service that runs indefinitely and executes "jobs" at certain times.

Quartz-5
 

Now, may we need to talk about quartz configurations? especially the store configuration.
on our program, we depended on the default quartz store that is RAM.
RAMJobStore is the simplest job store to use, it is also the most performant (in terms of CPU time) that keeps all data in RAM.
so, When using a RAM store, job information will not be persisted between Scheduler runs. If the Scheduler gets stopped and then restarted, all jobs will need to be scheduled again; also, no misfires will be detected.


There is another type of store, AdoJobStore it stores job data in a database via ADO.NET, When using a database store, Quartz will persist all job details to it as they are scheduled, run, finished, etc.; and retrieve said details from it upon restart, it is a bit more complicated to configure than RAMJobStore, and it also is not as fast. However, the performance draw-back is not terribly bad, especially if you build the database tables with indexes on the primary keys.

You can always see the table schema here in this official Quartz GitHub repository.

  • Create a Job

We create an instance of the job using the JobBuilder class (from Quartz). We use the Create method passing the job type - MessagesJob- that we just created.

Quartz-6

 

  • Create a Trigger

There are a number of options for how you can trigger a job:

  1. Running a job for a set number of times.
  2. Running a job that repeats until a specific date/time.
  3. Running a job with a delay interval.
  4. Running a job at a certain time of the day.
  5. Running a job on a particular day of the week/month/year.

Now we will create a trigger that will fire every 10 minutes.

Quartz-6


FREEFORMATTER.COM helps you generate a quartz CRON expression with an easy-to-use online interface. Convert a Cron expression into readable text that clearly explains when it will execute.

  • Schedule the job using the job and trigger

Quartz-7

NOW, we can see the result after running the service.

Quartz-8

There are more setup details that can be talked about, and I might write another post about those specifics.

إضافة تعليق جديد

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

منتجاتنا