Spring, Quartz Scheduler Example - (Spring + Quartz Scheduler)

In this particular blog we will learn 'How to integrate a Spring application with Quartz Scheduler' and 'How to schedule jobs in Spring using Quartz'. Java supports a number of framework to work with job scheduling, some of the popular and commonly used schedulers are : jcrontab, Fulcrum Scheduler, Essiembre J2EE Scheduler, Gos4j, Oddjob, cron4j and Quartz. These all are good in different scenario, but Quartz is the most commonly and easy to use job scheduler in Java.

Spring provides a number of helping classes to work with quartz and implement it easily on the fly. In spring we can specify quartz jobs in two ways.

1. Using 'MethodInvokingJobDetailFactoryBean'
2. Using 'JobDetailFactoryBean'(click Here)

In this tutorial we will focus on scheduling jobs in spring using Quartz's MethodInvokingJobDetailFactoryBean method. This is the commonly used and easy to integrate method of scheduling jobs in Spring.

Scheduling jobs in Spring using Quartz

This method is straight forward and easy to use, we just need to specify the time span to run our job periodically and a little configuration. See the code below.

Spring Quartz Dependencies

We need to add a single dependency in our spring pom.xml and everything is up and ready to use.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  <!-- Spring 3 dependencies -->  
  <!-- Quartz framework dependencies -->  

Writing a task class

This is simple java class containing a method, our job core to be run periodically goes into this method. We have added a print statement  here that will print the current system time every time the job is called.


package com.beingjavaguys.java;  
 * @author Nagesh Chauhan 
import java.util.Calendar;  
public class MyTask {  
 public void printCurrentTime() {  
  // printing current system time  
    .println("Current Time : " + Calendar.getInstance().getTime());  

Spring Quartz configuration.

In spring-quartz configuration file we just need to specify three been entries, one for the specified task class. One for the trigger to specify repeat time interval and start delay time. And lastly for the 'SchedulerFactoryBean' to bind the executable code and time interval together.


<beans xmlns="http://www.springframework.org/schema/beans"  
 <bean id="myTask" class="com.beingjavaguys.java.MyTask" />  
 <!-- specifing class and method that is going to be called on a specified   
  time basis -->  
 <bean id="myJob"  
  <property name="targetObject" ref="myTask" />  
  <property name="targetMethod" value="printCurrentTime" />  
 <!-- simple trigger specify repeat interval and delay time -->  
 <bean id="simpleTrigger"  
  <property name="jobDetail" ref="myJob" />  
  <property name="repeatInterval" value="5000" />  
  <property name="startDelay" value="1000" />  
 <!-- scheduler factory bean to bind,the executing code and time intervals   
  together -->  
 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  <property name="jobDetails">  
    <ref bean="myJob" />  
  <property name="triggers">  
    <ref bean="simpleTrigger" />  

Implementation Class Code

This is a simple java class containing the main method to start execution, we have initialized a ClassPathXmlApplicationContext object here containing 'spring-quartz' configuration file to start execution.


package com.beingjavaguys.java;  
 * @author Nagesh Chauhan 
import org.springframework.context.support.ClassPathXmlApplicationContext;  
public class App {  
 public static void main(String[] args) throws Exception {  
  new ClassPathXmlApplicationContext("spring-quartz.xml");  

Thst's it ! We are done with specifying a scheduler job in spring, you will see the following outpup in your console in a specified time interval.

In this particular blog we came across 'Spring Quartz Scheduler Example Project in Maven - Using MethodInvokingJobDetailFactoryBean', in next tutorial we will see 'Spring Quartz Scheduler Example Project in Maven - Using JobDetailFactoryBean' and more about Spring MVC and other opensource technologies.