Latest example of integrating Quartz Scheduler 2.2.2 with spring 4.2.2
This article demonstrates how to use Quartz Scheduler in Spring to schedule tasks. Spring provides support classes to simplify Quartz operations.
The related tools used in this example are as follows:
Spring 4.2.2 (released in October 2015)
Quartz Scheduler 2.2.2 (released on April 10, October 2015)
Maven 3
JDK 1.7
Eclipse Luna Service Release 1 (4.4.1)
Step 1: Create a Maven Project
If you do not know how to do this, refer to the blog "develop a JEE project using the Maven 2 plug-in of Eclipse".
Step 2: edit the third-party dependency package Maven pom. xml as follows:
4.0.0
settle
spring-quartz
1.0.0
jar
spring-quartz
http://maven.apache.org
UTF-8
4.2.2.RELEASE
2.2.2
org.springframework
spring-core
${springframework.version}
org.springframework
spring-context-support
${springframework.version}
org.springframework
spring-tx
${springframework.version}
org.quartz-scheduler
quartz
${quartz.version}
Step 3: Quartz Scheduler you can use Quartz in Spring to configure a job.
A: Use MethodInvokingJobDetailFactoryBean
This method is more convenient when you want to call a method of a specific bean than the other method.
The preceding job configuration simply calls the printMessage method of myBean. myBean is a simple POJO.
B: Use JobDetailFactoryBean
If you need more advanced settings, you need to pass data to the job. You can use this method if you want to be more flexible.
JobClass is associated with a class inherited from QuartzJobBean, which implements the Quartz job interface. When this job is called, its executeInternal will be executed.
JobDataMap allows us to transmit some data to related job beans. In this example, the 'anotherbean' to be used by ScheduledJob is passed to it.
The following describes how to reference jobclass (FirstScheduledJob.
Com.defonds.scheduler.jobs. FirstScheduledJob
/** * File Name:ScheduledJob.java * * Copyright Defonds Corporation 2015 * All Rights Reserved * */package com.defonds.scheduler.jobs;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.springframework.scheduling.quartz.QuartzJobBean;import com.defonds.scheduler.util.AnotherBean;/** * * Project Name:spring-quartz * Type Name:ScheduledJob * Type Description: * Author:Defonds * Create Date:2015-10-29 * @version * */public class FirstScheduledJob extends QuartzJobBean {private AnotherBean anotherBean;@Overrideprotected void executeInternal(JobExecutionContext arg0)throws JobExecutionException {System.out.println(I am FirstScheduledJob);this.anotherBean.printAnotherMessage();}public void setAnotherBean(AnotherBean anotherBean) {this.anotherBean = anotherBean;}}
Step 4: configure the trigger to be used during Quartz scheduling to define the time when the scheduler will execute your scheduling task. There are two possible trigger types:
A: simple trigger, using SimpleTriggerFactoryBean
You can define the job start time, the latency between triggers, and repeatInterval (frequency ).
B: Planning trigger, using CronTriggerFactoryBean
This type is more flexible, allowing you to select a plan for a specific instance and the frequency to be executed in the future.
Step 5: Configure and create the SchedulerFactoryBeanSchedulerFactoryBean for the Quartz scheduler to integrate jobDetails and triggers to configure the Quartz scheduler.
The following shows the complete Context file.
Src/main/resources/quartz-context.xml:
Step 6: create several simple POJO task beans used in this article
Com.defonds.scheduler.jobs. MyBean:
/** * File Name:MyBean.java * * Copyright Defonds Corporation 2015 * All Rights Reserved * */package com.defonds.scheduler.jobs;import org.springframework.stereotype.Component;/** * * Project Name:spring-quartz * Type Name:MyBean * Type Description: * Author:Defonds * Create Date:2015-10-29 * @version * */@Component(myBean)public class MyBean { public void printMessage() { System.out.println(I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean); } }
Com. defonds. scheduler. util. AnotherBean:
/** * File Name:AnotherBean.java * * Copyright Defonds Corporation 2015 * All Rights Reserved * */package com.defonds.scheduler.util;import org.springframework.stereotype.Component;/** * * Project Name:spring-quartz * Type Name:AnotherBean * Type Description: * Author:Defonds * Create Date:2015-10-29 * @version * */@Component(anotherBean)public class AnotherBean { public void printAnotherMessage(){ System.out.println(I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean); } }
Com.defonds.scheduler.jobs. SecondScheduledJob:
/** * File Name:SecondScheduledJob.java * * Copyright Defonds Corporation 2015 * All Rights Reserved * */package com.defonds.scheduler.jobs;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.springframework.scheduling.quartz.QuartzJobBean;/** * * Project Name:spring-quartz * Type Name:SecondScheduledJob * Type Description: * Author:Defonds * Create Date:2015-10-29 * @version * */public class SecondScheduledJob extends QuartzJobBean {@Overrideprotected void executeInternal(JobExecutionContext arg0)throws JobExecutionException {System.out.println(I am SecondScheduledJob);}}
To demonstrate the scheduling of multiple execution plans, we wrote two jobdetailfactorybeans, so we had SecondScheduledJob.
Step 7: Create the Main class of the execution program:
/** * File Name:AppMain.java * * Copyright Defonds Corporation 2015 * All Rights Reserved * */package com.defonds.scheduler;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * * Project Name:spring-quartz * Type Name:AppMain * Type Description: * Author:Defonds * Create Date:2015-10-29 * @version * */public class AppMain {public static void main(String args[]){ AbstractApplicationContext context = new ClassPathXmlApplicationContext(quartz-context.xml); }}
The directory structure of the entire project is as follows:
Execute this Main class and the output result is as follows:
I am FirstScheduledJob
I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am FirstScheduledJob
I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am FirstScheduledJob
I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am FirstScheduledJob
I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean
I am SecondScheduledJob
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am FirstScheduledJob
I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am FirstScheduledJob
I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
We can see that the job called by a simple trigger is executed every two seconds, while the scheduled trigger is executed every five seconds. The scheduled trigger is fixed and only executed once (eight o'clock, in red ).
Postscript
In most cases, JobDetailFactoryBean is used for task scheduling configuration. Each JobDetailFactoryBean has a trigger matching it. To configure multiple scheduling plans, you must configure multiple matching pairs. The trigger points to JobDetailFactoryBean;