Quartz Scheduler Implementation

Quartz Scheduler Implementation with .NET

Created by: Shabbir Akolawala

Date: 7th Nov 2014

Table of Contents

Quartz Scheduler Implementation with .NET    1

Introduction    1

Architecture    1

Implementation with ASP.NET MVC Application    1

Add Nuget Package    1

Edit Web.Config    1

Add section to Global.ascx    1

Create Job Class    1

Resources    1

Introduction

  • Quartz.NET is a port of very popular open source Java job scheduling framework, Quartz
  • Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs

Architecture

The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering

Implementation with ASP.NET MVC Application

Add Nuget Package

It has dependency on the Common.Logging, which would be installed too. Quartz Scheduler engine use common.logging for logging needs

Edit Web.Config

Add Config Sections

<configSections>

<sectionGroup
name=common>

<section
name=logging
type=Common.Logging.ConfigurationSectionHandler, Common.Logging />

</sectionGroup>

<section
name=quartz
type=System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089 />

</configSections>

Add Common logging section

<common>

<logging>

<factoryAdapter
type=Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging>

<arg
key=showLogName
value=true />

<arg
key=showDataTime
value=true />

<arg
key=level
value=INFO />

<arg
key=dateTimeFormat
value=HH:mm:ss:fff />

</factoryAdapter>

</logging>

</common>

Add Quartz Section

<quartz>

<add
key=quartz.scheduler.instanceName
value=CommerceScheduler />

<!–Configure Thread Pool–>

<add
key=quartz.threadPool.type
value=Quartz.Simpl.SimpleThreadPool, Quartz />

<add
key=quartz.threadPool.threadCount
value=5 />

<add
key=quartz.threadPool.threadPriority
value=Normal />

<!–Configure Job Store–>

<add
key=quartz.jobStore.misfireThreshold
value=60000 />

<add
key=quartz.jobStore.type
value=Quartz.Simpl.RAMJobStore, Quartz />

</quartz>

Add section to Global.ascx

Add this section in the Global.ascx.cs

// Grab the Scheduler instance from the Factory


IScheduler
scheduler
=
StdSchedulerFactory.GetDefaultScheduler();


// and start it off


scheduler.Start();


// define the job and tie it to our FirstJob class


IJobDetail
job
=
JobBuilder.Create<FirstJob>()


.WithIdentity(“job1”,
“group1”)


.Build();


// Trigger the job to run now, and then repeat every 10 seconds


ITrigger
trigger
=
TriggerBuilder.Create()


.WithIdentity(“trigger1”,
“group1”)


.StartNow()


.WithSimpleSchedule(x
=>
x


.WithIntervalInSeconds(10)


.RepeatForever())


.Build();


// Tell quartz to schedule the job using our trigger


scheduler.ScheduleJob(job,
trigger);


// some sleep to show what’s happening


Thread.Sleep(TimeSpan.FromSeconds(60));


// and last shut down the scheduler when you are ready to close your program


scheduler.Shutdown();

Create Job Class

A schedule consists of two Parts

Job: It is what task which needs to done.
E.g. Clean Database Table, Clear Cache, Load Data from other system

Trigger: It is when it need to done
E.g. Daily at 1 AM, Weekly on Monday, First Monday of Month, etc.

‘FirstJob’ in the above code is the Job which is been schedule. It implements ‘IJob’ and has the method ‘Execute’ which executes when the trigger run the Job

public
class
FirstJob
:
IJob


{


public
void
Execute(IJobExecutionContext
context)


{


Console.WriteLine(“Greetings from Shabbir!”);


}


}

Resources

Official Quick start guide

http://www.quartz-scheduler.net/documentation/quartz-2.x/quick-start.html

Github Project Page

https://github.com/quartznet/

Managing Configuration for Quartz

http://stackoverflow.com/questions/13272770/managing-configuration-for-quartz-net

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s