Spring Quartz Scheduler Example using MethodInvokingJobDetailFactoryBean

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

MethodInvokingJobDetailFactoryBean is used for simpler task. It specifies target object and target method, so target object’s method will be executed at the time specified using trigger.

Project structure:

Steps for creating Spring Quartz Scheduler Example using MethodInvokingJobDetailFactoryBean are:
Step 1:
Create simple java maven project with Name “SpringQuartzSchedulerExample”.

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:
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 MethodInvokingJobDetailFactoryBean which specify class and method which will get executed.
  • Need to put entry for triggers which specify time interval and start delay time.
  • Need to specify ScheduleFactoryBean to bind triggers and MethodInvokingJobDetailFactoryBean together.
 quartz-config.xml
Step 5: 
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 6:
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 MethodInvokingJobDetailFactoryBean.

Was this post helpful?

Leave a Reply

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