This Article is for basic Understanding of Scheduling a Job using Quartz Framework.
Quartz is a job scheduling system that can be integrated with, or used alongside virtually any other software system. The term “job scheduler” seems to conjure different ideas for different people. As you read the tutorial attached, you should be able to get a firm idea of what they 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.
Introduction
This
Article is for basic Understanding
of Scheduling a Job using Quartz Framework
Quartz is a
job scheduling system that can be integrated with, or used alongside virtually
any other software system. The term “job
scheduler” seems to conjure different ideas for different
people. As you read the tutorial attached, you should be able to get a firm
idea of what they 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.
Objective
Quartz is
quite flexible, and contains multiple usage paradigms that can be used
separately or together, in order to achieve your desired behavior, and enable
you to write your code in the manner that seems most ‘natural’ to your project.
Quartz is very light-weight, and requires very
little setup/configuration – it can actually be used ‘out-of-the-box’ if your
needs are relatively basic.
Quartz is fault-tolerant, and can persist
(‘remember’) your scheduled jobs between system restarts.
Although Quartz is extremely useful for simply
running certain system processes on given schedules, the full potential of
Quartz can be realized when you learn how to use it to drive the flow of your
application’s business processes.
Using the code
Explain the topic of the article with code and description
Block of code should be set style as "Code" like below.
Here you can watch the
sample job created in Default.cs file
using Quartz;
using System.IO;
using System;
namespace Sample.Core.Scheduler
{
public class DefaultJob : IJob
{
void IJob.Execute(IJobExecutionContext context)
{
//Code that would be executed by the default job
TextWriter tw = new StreamWriter("D:\\sample.txt", true);
tw.WriteLine(DateTime.Now.ToString());
tw.Close();
}
}
}
Creation Of a Job Using
Quartz Framework
1)
First you have to
download Quartz Framwork that u can Grab it from Source Forge downloads . Download Latest Release of them…
2)
Open Visual Studio
2012 or Earlier Create a Sample Project
3)
Add all the .dll files of quartz framework in References.
4)
Add Folder named Jobs
and Listeners add all your class Files as shown in the below figure
Also add Job scheduler.cs
file which contains the run method and a trigger
Here you can watch the
run Method of a job
using Quartz;
using System;
using Quartz.Impl.Matchers;
namespace Sample.Core.Scheduler
{
///
/// JobScheduler class
///
public class JobScheduler : JobSchedulerBase
{
///
/// Run method
///
public void Run()
{
job = JobBuilder.Create()
.WithIdentity(new JobKey("Sample"))
.Build();
defaultTrigger = (ISimpleTrigger)TriggerBuilder.Create()
.WithIdentity("sampleTrigger", "sampleGroup")
.Build();
//DateTimeOffset dateTimeOffset = scheduler.ScheduleJob(job, defaultTrigger);
// WriteToLog(job, dateTimeOffset, trigger);
//scheduler.Start();
}
///
/// WriteToLog method
///
/// job as IJobDetail
/// dateTimeOffset as DateTimeOffset
/// trigger as ICronTrigger
private void WriteToLog(IJobDetail job, DateTimeOffset dateTimeOffset, ICronTrigger trigger)
{
//Code to write job execution details to the log
}
}
}
Here you can watch the scheduler service class file which is
claling the scheduler Library
using System.ServiceProcess;
using Sample.Core.Scheduler;
namespace Sample.Core.WindowsService
{
public partial class SchedulerService : ServiceBase
{
public SchedulerService()
{
this.ServiceName = "Sample.SchedulerService";
InitializeComponent();
}
protected override void OnStart(string[] args)
{
JobScheduler scheduler = new JobScheduler();
scheduler.Run();
}
protected override void OnStop()
{
}
}
}
Installing the service
Manually:
1.
Open Visual Studio Command promt in
Admin mode
2.
Install the .exe by using installutil
–i Sample.Core.WindowsService.exe as
shown in the below figure
3.
Now Open services.msc and check whether
the service is started or not,if started just restart it.
4.
Now you can watch a sample.txt file in
your D drive
Conclusion
Quartz Framework is very useful to schedule multiple jobs to run in back
ground…….
Reference
www.Quartz.net