Spring Timer's are great if you need a job or task to run on a fixed interval. For example, if you need a bean to wake up and run itself every 30-minutes. But what if you need finer control over the scheduling of a timer? Say you need a job to run every morning at 3AM, regardless of when the JVM is started. Or, say you need a bean to run "every half hour between the hours of 8 AM and 10 AM on the 5th and 20th of every month." You won't get that type of granular control with a
straight-up Spring Timer.
Meet
Quartz, a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application. Similar to a native
UNIX Cron job, Quartz lets you define
powerful CronExpression's that give you very precise control over when a job or task is started and on what interval.
Setting up Quartz with your Spring powered web-application is easy.
1 - Download Quartz
The
latest Quartz libraries are available here. Download them, and add to your build-path and build-files accordingly.
2 - Add Beans to Your applicationContext.xml
In your applicationContext.xml, add the necessary beans:
<!-- this is the bean we're going to run on a schedule
with Quartz; we'll call it UpdaterOnScheduleExecutor.
The class is com.kolich.app.beans.UpdaterBean -->
<bean id="UpdaterOnScheduleExecutor"
class="com.kolich.app.beans.UpdaterBean"
init-method="init"
depends-on="AnotherBean">
<property name="someProperty" value="/WEB-INF/something" />
</bean>
<!-- quartz stuff below -->
<!-- UpdaterOnScheduleJobDetail defines a bean that points to
UpdaterOnScheduleExecutor and says to call its execute()
method when the trigger is fired -->
<bean id="UpdaterOnScheduleJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="UpdaterOnScheduleExecutor"/>
<!-- the method to call inside of
com.kolich.app.beans.UpdaterBean -->
<property name="targetMethod" value="execute"/>
</bean>
<!-- here's where we use the Cron like scheduling expression
to define when the bean is run. -->
<bean id="UpdaterOnScheduleJob"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="UpdaterOnScheduleJobDetail" />
<!-- run every morning at 3AM -->
<property name="cronExpression" value="0 0 3 * * ?" />
</bean>
<!-- the scheduler factory is what does the work to setup
the triggers -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="UpdaterOnScheduleJob" />
</list>
</property>
</bean>
Note that you can add as many jobs/triggers as desired to the
Quartz SchedulerFactoryBean.
3 - Sample Cron Expressions
Here are some other sample cronExpression's you might enjoy (I borrowed these
from the Quartz trigger tutorial) ...
Fires every five minutes:
"0 0/5 * * * ?"
Fires every 5 minutes, at 10 seconds after the minute:
"10 0/5 * * * ?"
Fires at 10:30, 11:30, 12:30, and 13:30, on every Wednesday and Friday:
"0 30 10-13 ? * WED,FRI"
Fires every half hour between the hours of 8 am and 10 am on the 5th and 20th of every month:
"0 0/30 8-9 5,20 * ?"
Enjoy!