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 (http://www.quartzscheduler.org).
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>

OR

<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>


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



2. Using the JobDetailBean

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


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"/>
</bean>

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

Ref:
http://www.opensymphony.com/quartz.
http://static.springframework.org/spring/docs/1.2.9/reference/scheduling.html

No comments:

 
Free Domain Names @ .co.nr!