Quick Quartz + Spring 3 Tutorial

  • Post author:
  • Post category:Java

This is a quick Quartz + Spring tutorial. You will need Eclipse and the Eclipse m2e Plugin to follow the steps below.

This post was originally set out to use Quartz 2.0.2. It seems Quartz 2 has changed things around and its no longer compatible with the Spring Quartz classes. The error I get is java.lang.InstantiationError: org.quartz.JobDetail. This is because JobDetail used to be a class and is now an interface. So we will fall back to Quartz 1.8.5 for this tutorial.

1. Go to Eclipse and create a new Maven project. Click “Create a simple project” and hit Next. On the next page, enter Group Id as “com.teamextension”, Artifact Id as “quickquartzspring” and Name as “quickquartzspring”.

2. Right click on the project and go to Maven -> Add Dependency. Enter “quartz-scheduler” in the search box and pick “org.quartz-scheduler” version 1.8.5.

Add Dependency

Also add the Spring 3 dependencies. Enter “org.springframework” in the search box and add the dependencies “spring-context”, “spring-context-support” and “spring-tx”, version 3.0.6.RELEASE.

We will also add SLF4J as a dependency so you can see the Quartz logs. Enter “slf4j-simple” in the search box and pick “org.slf4j slf4j-simple” version 1.6.1.

3. Create the Job class. The job simply prints the current time to System.out.

package com.teamextension.quickquartzspring;

import java.util.Date;

public class QuickQuartzSpringJob {
	public void execute() {
		System.out.println(new Date());
	}
}

Notice that this class has no Quartz or Spring dependencies.

4. Create the following applicationContext.xml file under src/resources.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="quickQuartzSpringJob" class="com.teamextension.quickquartzspring.QuickQuartzSpringJob" />

	<!-- create the Job -->
	<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="quickQuartzSpringJob" />
		<property name="targetMethod" value="execute" />
	</bean>

	<!-- create the Trigger with a Schedule that runs every 5 seconds -->
	<bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="jobDetail" />
		<property name="cronExpression" value="0/5 * * * * ?" />
 	</bean>

 	<!-- schedule the Job -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers" ref="trigger" />
	</bean>
</beans>

The Spring ApplicationContext creates the job and trigger, and registers them with the Scheduler. The schedule in this example is set to every 5 seconds. This uses the simpler MethodInvokingJobDetailFactoryBean instead of org.springframework.scheduling.quartz.JobDetailBean.

5. Create our main application. It will simply load the Spring configuration file. If you are running a Spring MVC application, the same configuration can be used.

package com.teamextension.quickquartzspring;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class QuickQuartzSpring {
	public static void main(String[] args) throws InterruptedException {
		new ClassPathXmlApplicationContext("applicationContext.xml");
	}
}

Run the application and you should see the current date printed out every 5 seconds.

For more information about Spring and Quartz integration, read the Spring Task Execution and Scheduling page.

This Post Has One Comment

  1. Sam

    Thanks, very helpful tutorial..

Comments are closed.