Spring provides couple of classes that simplify the usage of Quartz within Spring-based applications.
There are 2 ways to configure a Job in Spring using Quartz:
A : Using MethodInvokingJobDetailFactoryBean
This is the simplest among two.
Above job configuration simply invokes myMethod method of bean myJobBean which is simple POJO
B : Using JobDetailFactoryBean
When you need more advanced setup, need to pass data to job, being more flexible.
Also there are two method to configure trigger:
Trigger defines the time when scheduler will run your scheduled job. There are two possible trigger type:
A: Simple Trigger , using SimpleTriggerFactoryBean
You can specify start time, delay between triggers and repeatInterval(frequency) to run the job.
B: Cron Trigger , using CronTriggerFactoryBean
It’s more flexible and allows you to choose scheduled job at specific instance (time, day, date,..) and frequency in future.
There are 2 ways to configure a Job in Spring using Quartz:
A : Using MethodInvokingJobDetailFactoryBean
This is the simplest among two.
<!-- For times when you just need to invoke a method on a specific object -->
<
bean
id
=
"simpleJobDetail"
class
=
"org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
>
<
property
name
=
"targetObject"
ref
=
"
myJobBean"
/>
<
property
name
=
"targetMethod"
value
=
"
myMethod"
/>
</
bean
>
Above job configuration simply invokes myMethod method of bean myJobBean which is simple POJO
B : Using JobDetailFactoryBean
When you need more advanced setup, need to pass data to job, being more flexible.
<!-- For times when you need more complex processing, passing data to the scheduled job -->
<
bean
name
=
"complexJobDetail"
class
=
"org.springframework.scheduling.quartz.JobDetailFactoryBean"
>
<
property
name
=
"jobClass"
value
=
"com.websystique.spring.quartz.ScheduledJob"
/>
<
property
name
=
"jobDataMap"
>
<
map
>
<
entry
key
=
"anotherBean"
value-ref
=
"anotherBean"
/>
</
map
>
</
property
>
<
property
name
=
"durability"
value
=
"true"
/>
</
bean
>
Also there are two method to configure trigger:
Trigger defines the time when scheduler will run your scheduled job. There are two possible trigger type:
A: Simple Trigger , using SimpleTriggerFactoryBean
You can specify start time, delay between triggers and repeatInterval(frequency) to run the job.
<!-- Run the job every 2 seconds with initial delay of 1 second --> < bean id = "simpleTrigger" class = "org.springframework.scheduling.quartz.SimpleTriggerFactoryBean" > < property name = "jobDetail" ref = "simpleJobDetail" /> < property name = "startDelay" value = "1000" /> < property name = "repeatInterval" value = "2000" /> </ bean > |
It’s more flexible and allows you to choose scheduled job at specific instance (time, day, date,..) and frequency in future.
<!-- Run the job every 5 seconds only on Weekends -->
<
bean
id
=
"cronTrigger"
class
=
"org.springframework.scheduling.quartz.CronTriggerFactoryBean"
>
<
property
name
=
"jobDetail"
ref
=
"complexJobDetail"
/>
<
property
name
=
"cronExpression"
value
=
"0/5 * * ? * SAT-SUN"
/>
</
bean
>
SchedulerFactoryBean glues together
jobDetails
and triggers
to Configure Quartz Scheduler
<!-- Scheduler factory bean to glue together jobDetails and triggers to Configure Quartz Scheduler -->
<
bean
class
=
"org.springframework.scheduling.quartz.SchedulerFactoryBean"
>
<
property
name
=
"jobDetails"
>
<
list
>
<
ref
bean
=
"simpleJobDetail"
/>
<
ref
bean
=
"complexJobDetail"
/>
</
list
>
</
property
>
<
property
name
=
"triggers"
>
<
list
>
<
ref
bean
=
"simpleTrigger"
/>
<
ref
bean
=
"cronTrigger"
/>
</
list
>
</
property
>
</
bean
>