Spring And Quartz
Quartz is a scheduler that, among many things, lets you set up cron-like jobs in Java.
Spring has nice integration with Quartz that makes it very easy to configure scheduled jobs. There are many ways to set up jobs in Spring and Quartz, but I find it easiest to use a POJO (that does not extend or implement any Quartz or Spring classes/interfaces).
Lets say you want to check your diskspace usage quota twice daily on a remote computer where your application is running. Simply write a plain Java class with a public void method in it to do the check for you, say:
package com.zabada.jobs;
public class DiskQuotaChecker
{
public void executeCheck()
{
//DO THE DISK CHECK
//MAYBE EMAIL SOMEONE IF A
//CERTAIN THRESHOLD OF FREE DISK
//SPACE IS REACHED
}
}
Now we just need to configure this job at a given interval in Spring. This can get a little verbose, but it is not very difficult.
First, make an instance of the class that contains your method to execute when the job is triggered.
<bean id="quotaChecker" class="com.zabada.jobs.DiskQuotaChecker" />Feel free to inject any needed dependencies for this class. For example, here, we might have a class to do the actual disk usage calculation and another injected class to handle the email notifications.
Now we wire up the actual Quartz job that refers to our class that will do the work using the MethodInvokingJobDetailFactoryBean:
<bean id="quotaJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="quotaChecker"/>
<property name="targetMethod" value="executeCheck"/>
</bean>
In the above snippet:
- targetObject is the Spring configured bean reference that does the work.
- targetMethod is the method to execute on the targetObject bean, in this case executeCheck()
There are additional properties that can be configured here, see the MethodInvokingJobDetailFactoryBean javadoc
Next, you need to define a trigger for your job. One type of trigger just repeats over a given interval. So for example, maybe you want to run the job every two hours. For this, you can just use the SimpleTriggerBean. For example:
<bean id="quotaTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="quotaJob"/>
<property name="startDelay" value="900000"/>
<property name="repeatInterval" value="7200000"/>
</bean>
In the above snippet:
- jobDetail references the Spring configured job to trigger, here quotaJob
- startDelay is the time in milliseconds after startup of the context to wait before triggering the first execution of your job
- repeatInterval is the time in milliseconds between the executions of the job.
Once again, there are many other properties that you can set, see the SimpleTriggerBean javadoc.
Finally, you've got to configure a scheduler to run the triggers in:
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cleanerTrigger"/>
</list>
</property>
<property name="waitForJobsToCompleteOnShutdown" value="false"/>
</bean>
In the above snippet:
- triggers is a list of reference to the triggers to schedule.
- waitForJobsToCompleteOnShutdown is whether or not to wait for running jobs to complete on shutdown.
Further properties can be set as shown in the javadoc: SchedulerFactoryBean
If you want to run the job at given times every day instead of in intervals as configured above, use something like the below alternate quotaTrigger (replace the above quotaTrigger with this):
<bean name="quotaTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail"><ref bean="quotaJob"/></property>
<property name="cronExpression">
<value>0 21 10 * * ?</value>
</property>
</bean>
In the above snippet:
- jobDetail is the Spring bean reference to the job
- cronExpression is a cron expression to describe when to trigger the job. The above example will fire every day at 10:21AM. For examples and a detailed description of cron expressions, see the Quartz CronTrigger javadoc.
That's it! Slap the four bean definitions into a Spring ApplicationContext and your jobs will be scheduled.
Further Reading





