Spring Quartz Scheduler Example

In this post, we will see how to schedule jobs using Spring Quartz scheduler or how to integrate spring with Quartz. We have already seen how to schedule jobs using Timer and TimerTask .

There are two ways by which you can specify quartz jobs.
    MethodInvokingJobDetailFactoryBean JobDetailFactoryBean

JobDetailFactoryBean is used for complex task. You need to create job class which extends QuartzJobBean.

Project structure:

Steps for creating Spring Quartz Scheduler Example using JobDetailFactoryBean are:
Step 1:
Create simple java maven project with Name "SpringQuartzSchedulerExampleUsingQuartzJobBean".

Step 2:
Update pom.xml to put spring core and spring quartz dependencies.
<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/xsd/maven-4.0.0.xsd">
 <description>It provide spring quartz scheduler example using MethodInvokingJobDetailFactoryBean</description>




  <!-- Quartz framework dependencies -->

Step 3:
Create  a class called "SendEmailTask.java" in src/main/java in package com.arpit.java2blog.spring.
It is class whose code you want to execute at specific time or time interval repeatedly.
package com.arpit.java2blog.spring;

import java.util.Calendar;

public class SendEmailTask {
  public void sendEmail() {
    // You may want to put some condition for sending email
          // Actual email send logic   
    System.out.println("Sending email at "+ Calendar.getInstance().getTime());

Step 4:
Create a class named "JobScheduler.java" in src/main/java in package com.arpit.java2blog.spring.. This class will extend QuartzJobBean and executeInternal method will get called at particular time specified in trigger.
package com.arpit.java2blog.spring;

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

public class JobScheduler extends QuartzJobBean {
 SendEmailTask sendEmailTask;

 public void setSendEmailTask(SendEmailTask sendEmailTask) {
  this.sendEmailTask = sendEmailTask;

 protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {

Step 5:
In this step, you need to create "quartz-config.xml" in src/main/resources.
quartz-config.xml will have all configuration required to schedule a task . You need to put four entries here
  • Need to specify task (SendEmailTask).
  • Need to specify JobDetailFactoryBean 
  • Need to put entry for triggers which specify time interval and start delay time.
  • Need to specify ScheduleFactoryBean to bind triggers and JobDetailFactoryBean together.
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

 <!--Specify Task bean -->
 <bean id="sendEmailTask" class="com.arpit.java2blog.spring.SendEmailTask" />

 <!-- Spring Quartz -->
 <bean name="emailJob"
  <property name="jobClass" value="com.arpit.java2blog.spring.JobScheduler" />

  <property name="jobDataAsMap">
    <entry key="sendEmailTask" value-ref="sendEmailTask" />
  <property name="durability" value="true" />

 <!-- simple trigger specify repeat interval and delay time -->
 <bean id="simpleTrigger"

  <property name="jobDetail" ref="emailJob" />
  <property name="repeatInterval" value="10000" />
  <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="emailJob" />

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

Step 6: 
Finally we will create main class to execute this. We will create object of ClassPathXmlApplicationContext to load quartz-config.xml create class name Application.java in src/main/java in package com.arpit.java2blog.spring
package com.arpit.java2blog.spring;

 * @author Arpit Mandliya
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {

public static void main(String[] args) throws Exception {
  new ClassPathXmlApplicationContext("quartz-config.xml");

Step 7:
When you run above program, you will get following output.
Jan 24, 2016 1:09:13 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1441e2aa: startup date [Sun Jan 24 01:09:13 IST 2016]; root of context hierarchy
Jan 24, 2016 1:09:13 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [quartz-config.xml]
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Jan 24, 2016 1:09:14 AM org.springframework.context.support.DefaultLifecycleProcessor start
INFO: Starting beans in phase 2147483647
Jan 24, 2016 1:09:14 AM org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler
INFO: Starting Quartz Scheduler now
Sending email at Sun Jan 24 01:09:15 IST 2016
Sending email at Sun Jan 24 01:09:25 IST 2016
Sending email at Sun Jan 24 01:09:35 IST 2016
Sending email at Sun Jan 24 01:09:45 IST 2016
Sending email at Sun Jan 24 01:09:55 IST 2016

Download source code:

click to begin
20KB .zip

We are done with Spring Quartz Scheduler Example using JobDetailFactoryBean.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012