Quartz Scheduler is used
to scheduling of all kinds of jobs. For this it uses Trigger, Job and JobDetail objects
JobDetail objects contain all information needed to run a job. The
Spring Framework provides a JobDetailBean that makes the JobDetail more of an
actual JavaBean with sensible defaults
For Ex:
<bean
name="exampleJob"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property
name="jobClass" value="example.ExampleJob" />
<property
name="jobDataAsMap">
<map>
<entry
key="timeout" value="5" />
</map>
</property>
</bean>
The timeout is specified
in the job data map. The job data map is available through the
JobExecutionContext (passed to you at execution time), but the JobDetailBean
also maps the properties from the job data map to properties of the actual job.
So in this case, if the ExampleJob contains a property named timeout,the
JobDetailBean will automatically apply it.
Ex:
package example;
public class ExampleJob
extends QuartzJobBean {
private int timeout;
/**
* Setter called after
the ExampleJob is instantiated
* with the value from
the JobDetailBean (5)
*/
public void
setTimeout(int timeout) {
this.timeout = timeout;
}
protected void
executeInternal(JobExecutionContext ctx) throws JobExecutionException {
// do the actual work
}
}
MethodInvokingJobDetailFactoryBean:
Oftenly we need to
invoke a method on a specific Class. The
"MethodInvokingJobDetailFactoryBean"
can do this exactly.
Ex Configuration:
<bean
id="jobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property
name="targetObject" ref="exampleBusinessObject" />
<property
name="targetMethod" value="doIt" />
</bean>
The above example will
call the doIt method on the ExampleBusinessObject class.
The class:
public class
ExampleBusinessObject {
// properties and
collaborators
public void doIt() {
// do the actual work
}
}
Using the
MethodInvokingJobDetailFactoryBean, we don't need to create one-line jobs that
just invoke a method, and we only need to create the actual business object and
wire up the detail object.
By default Quartz jobs
are stateless. Since they are stateless there is a possibility of interfering
each other.
If we specify 2 triggers
for the same JobDetails there is a possibility of starting the second one
before first one is Completed.
To avoid this and to
make jobs of 'MethodInvokingJobDetailFactoryBean'
Non-concurrent set
'concurrent' flag to 'false'.
<bean
id="jobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property
name="targetObject" ref="exampleBusinessObject" />
<property
name="targetMethod" value="doIt" />
<property
name="concurrent" value="false" />
</bean>
Wiring up jobs using
triggers & the 'SchedulerFactoryBean':
We have created
JobDetails and Jobs.
we still needs to
schedule the jobs themselves this can be done using 'triggers' and a 'SchedulerFactoryBean'.
There are several
triggers are available with in Quartz and Spring Provides 2 Quartz FactoryBean
implementations with convenient defaults
CronTriggerFactoryBean
SimpleTriggerFactoryBean.
Triggers need to be
Scheduled.For this Spring offers a 'SchedulerFactoryBean' that exposes triggers
to be set as properties.
SchedulerFactoryBean
schedules the actual jobs with those triggers.
Ex. Configurations:
<bean
id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<!-- see the example
of method invoking job above -->
<property
name="jobDetail" ref="jobDetail" />
<!-- 10 seconds
-->
<property
name="startDelay" value="10000" />
<!-- repeat every 50
seconds -->
<property
name="repeatInterval" value="50000" />
</bean>
<bean
id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property
name="jobDetail" ref="exampleJob" />
<!-- run every
morning at 6 AM -->
<property
name="cronExpression" value="0 0 6 * * ?" />
</bean>
Now we've set up two
triggers,
First one running every
50 seconds with a starting delay of 10 seconds and
Second one runs every
morning at 6 AM. And Finally we need to set up the
SchedulerFactoryBean:
<bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property
name="triggers">
<list>
<ref
bean="cronTrigger"/>
<ref
nean="simpleTrigger"/>
</list>
</property>
</bean>
there are some more
properties to use.
0 comments:
Post a Comment