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.
MethodInvokingJobDetailFactoryBeanJobDetailFactoryBean

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.
pom.xml
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.
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.

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.
 quartz-config.xml
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
Step 7:
When you run above program, you will get following output.

Download source code:

click to begin
20KB .zip

We are done with Spring Quartz Scheduler Example using JobDetailFactoryBean.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *