Use Quartz to schedule tasks in Spring

Friday, May 7th, 2010

Quartz is 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.

In this simple example (notionally checking Twitter for updates) we integrate it with Spring.

Spring Config

<bean name="checkTwitterForUpdates"

       <property name="jobClass" value="test.twitterReader" />
<bean id="cronTwitterUpdateCheckTriggerBean"

       <property name="jobDetail" ref="checkTwitterForUpdates" />
       <!-- Run every day at noon -->
       <property name="cronExpression" value="0 0 12 * * ?" />
<bean id="scheduler"

       <property name="triggers">
               <ref bean="cronTwitterUpdateCheckTriggerBean" />

You may just want to call a method on an existing object (perhaps a business object). We can use MethodInvokingJobDetailFactoryBean to do just this:

Often you just need to invoke a method on a specific object. Using the MethodInvokingJobDetailFactoryBean you can do exactly this:

<bean id="jobDetail"
    <property name="targetObject" ref="someBusinessObject"/>
    <property name="targetMethod" value="someMethod"/>


package twitter;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class twitterReader extends QuartzJobBean {
    protected void executeInternal(JobExecutionContext ctx)
      throws JobExecutionException {
      // Like whatever.......


You can get get Quartz here

You may also need:
Simple Logging Facade for Java (SLF4J) and

You can leave a response, or trackback from your own site.

Tags: , ,
Posted in: Development, How to's

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>