Spring – Quartz scheduler setup

November 25, 2008 at 5:55 pm Leave a comment

The Spring Application Framework ships with helper classes to make configuring OpenSymphony’s quartz scheduler a breeze. The scheduler allows (amongst other things) cron style triggers. At the time of writing, the latest version of Quartz is 1.6.3, but I’ll be using 1.6.0 as this is the latest version in the Maven central repos.

First, download the version you wish to use and add as a dependency to your project. In Maven‘s case, add this to the pom.xml:


Now, create your task which extends org.springframework.scheduling.quartz.QuartzJobBean and implement public void executeInternal(JobExecutionContext context) throws JobExecutionException. You can inject any necessary beans too (myService in this example).

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

public class myTask extends QuartzJobBean {
  private MyService myService;

  public void setMyService(MyService myService) {
    this.myService = myService;

  public void executeInternal(JobExecutionContext context) throws JobExecutionException {
    Actual Business Logic

Create a schedulingContext-timer.xml to keep the scheudling configuration seperate and place in the WEB-INF folder of your Spring webapp. These settings could also go in the applicationContext.xml if you prefer but I like to keep the configuration seperate.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  <!-- Job details -->
  <bean id="myTask" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="com.mycompany.project.scheduling.MyTask"/>
    <property name="jobDataAsMap">
        <entry key="myService" value-ref="myServiceBean"/>

   <!-- Cron -->
  <bean id="myTaskCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="myTask"/>
    <property name="cronExpression" value="0 0/5 * * * ?"/>

   <!-- Kicker -->
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <ref bean="myTaskCronTrigger"/>

A couple of notes:

  • “Job details” is where you inject any required beans
  • This example uses a CronTriggerBean as this provides functionality otherwise not present in a simple Timer. In the example, the trigger is set to run every 5 minutes starting on the hour. For a full list of possible cron settings, click here.
  • Available triggers are CronTrigger, SimpleTrigger and UICronTrigger. See here.
  • The “Kicker” is required to actually make use of the triggers you’ve set up
  • If you’ve used a separate context, remember to add its name to the web.xml under contextConfigLocation

And that’s all… your webapp now has full Cron capabilities! Enjoy 🙂


Entry filed under: Development. Tags: , , .

Maven and log4j Poodle vulnerability

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

%d bloggers like this: