Please click on any Advertisement, if you like this blog.

Wednesday, May 25, 2011

Quartz Framework: Implementation with Spring Framework

Other Links: Job Scheduling with java.util.Timer
                    Job Scheduling with Quartz Framework and JSP - Servlet
                    Job Scheduling with Quartz Framework and Java Spring Framework

Quartz Framework with Spring:
Implementing Quartz framework with Spring is slightly different as compared to its implementation with plain JSP, Servlet application as discussed in my previous post. Followings are the steps for configuring Quartz with Spring:
1)      Add Quartz.jar in application library folder.
2)      Create a Quartz job
3)      Declare the job in Spring Configuration file.
4)      Schedule the Job
a)      Simple Scheduling
b)      Cron Scheduling
5)      Start the Job
6)      Invoke methods on schedule.
Create a Quartz Job:
Quartz job is nothing but a simple java class that extends org.springframework.scheduling.quartz.QuartzJobBean and simply defines what to be done on a schedule time. E.g.
package springQuartzEx;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class SpringQuartzJobExample extends QuartzJobBean
public SpringQuartzJobExample () {}
private SpringQuartzJobExampleService  springQuartzJobExampleService;
public void setSpringQuartzJobExample(springQuartzJobExampleService springQuartzJobExampleService)
                this. springQuartzJobExampleService = springQuartzJobExampleService;
protected void executeInternal(JobExecutionContext  jobContext)
throws JobExecutionException
Points to be noted here are:
·         QuartzJobBean is the implementation of org.quartz.Job interface.
·         executeInternal() is the method that defines “what the job class will do on its schedule”. It takes JobExecutionContext   as a parameter.

Please click on any advertisement if you like this blog.
Declare the job in Spring Configuration file: 
A Quartz job can be defined in following manner in spring configuration file:
<bean id=" springQuartzJobExample” class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.roadrantz.service.DailyRantEmailJob" />
<property name="jobDataAsMap">
<entry key=" springQuartzJobExampleService " value-ref=" springQuartzJobExampleService " />
Points to be noted here:
·         In the above declaration, we are defining JobDetailBean class with id as springQuartzJobExample.
·         springQuartzJobExampleService is not set directly. Instead JobDetail’s jobDataAsMap property takes java.util.Map that contains the property that are to be set on the object specified by job class.
Scheduling the Job:
                Scheduling a job means providing a time and date and also the intervals on which the code, written in executeInternal() method of quartz job class ( in our case) , should be executed. There are 2 ways of defining these timings:
a)      Simple Scheduling: This is very much similar to java.util.Timer and provide very less flexibility in comparison with Cron Scheduling.  Using it, we can specify how often a job should run and how long to wait before running the job for the first time. It can be done with SimpleTriggerBean class of spring framework E.g.

<bean id="simpleReportTrigger"
<property name="jobDetail" ref=" springQuartzJobExample "/>
<property name="startDelay" value="3600000" />
<property name="repeatInterval" value="86400000" />
b)      Cron Scheduling:  It is used to specify the exact time and date at which the job class should be executed. It is done with the help of CronTriggerBean of Spring framework. E.g.\
<bean id="cronEmailTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref=" springQuartzJobExample "/>
<property name="cronExpression" value="0 59 23 * * ?" />
This cron expression will force the cron job class to be executed at 11:59 PM every day.

NOTE: Above all declaration like that of spring quartz job, simple scheduling, cron scheduling are done in Spring Configuration file.

Please click on any advertisement if you like this blog.
Start the Job:
To start a quartz job, spring’s SchedularFactoryBean is used as follows:
<bean class="org.springframework.scheduling. quartz.SchedulerFactoryBean">
<property name="triggers">
<ref bean="cronEmailTrigger"/>
This mapping should be defined in Spring Configuration file.
Invoke methods on Schedule:
From above discussion, we can see that we are just scheduling the job class i.e. SpringQuartzJobExample whose main task is to execute the springQuartzJobExample.sendBirthdayEmails() method. If the question is about to just execute the springQuartzJobExample.sendBirthdayEmails() method on the schedule time, this can be done by only scheduling the methods directly, no need to create the job class and configure it in spring configuration file.

Please click on any advertisement if you like this blog.

Share on Facebook

1 comment:

  1. Thanks for detailed article. quartz with spring is one of the popular scheduler in enterprise applications.

    appreciate your efforts for detailed information


Please provide your precious comments and suggestion