Saturday, May 16, 2009

Scheduling simple Quartz job Using Spring

Currently, Spring supports the Timer, part of the JDK since 1.3, and the Quartz Scheduler (
Both schedulers are set up using a FactoryBean with optional references to Timers or Triggers, respectively

Quartx job can be written in several ways

1. Wiring up jobs using triggers and the SchedulerFactoryBean

<bean id="sampleJob" class="com.xyx.SampleJob" />

<bean id="sampleJobTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="sampleJob" />
<property name="startDelay" value="10000" />
<property name="repeatInterval" value="50000" />


<bean id="sampleJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="exampleJob"/>
<!-- run every morning at 6 AM -->
<property name="cronExpression" value="0 0 6 * * ?"/>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<ref bean="sampleJobTrigger" />

2. Using the JobDetailBean

<bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.xyx.SampleJob"/>
<property name="jobDataAsMap">
<entry key="timeout" value="5"/>

In this case com.xyx.SampleJob has to extend QuartzJobBean.

3. Using the MethodInvokingJobDetailFactoryBean

Often you just need to invoke a method on a specific object. Using the MethodInvokingJobDetailFactoryBean you can do as follows

<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="sampleJob"/>
<property name="targetMethod" value="sampleMethod"/>

The above example will result in the sampleMethod() being called on the sampleJob object.


No comments:

Free Domain Names @!