本文将演示如何通过 Spring 使用 Quartz Scheduler 进行任务调度。Spring 为简化 Quartz 的操作提供了相关支持类。
本文示例使用的相关工具如下:
- Spring 4.2.2 (发布于 2015 年 10 月)
- Quartz Scheduler 2.2.2 (发布于 2015 年 10 月)
- Maven 3
- JDK 1.7
- Eclipse Luna Service Release 1 (4.4.1)
步骤 1:新建 Maven 项目
这一步如果不知道怎么做可以参考博客《使用 Eclipse 的 Maven 2 插件开发一个 JEE 项目》。
步骤 2:第三方依赖包的引入
Maven pom.xml 编辑如下:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>settle</groupId> 6 <artifactId>spring-quartz</artifactId> 7 <version>1.0.0</version> 8 <packaging>jar</packaging> 9 <name>spring-quartz</name> 10 <url>http://maven.apache.org</url> 11 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 <springframework.version>4.2.2.RELEASE</springframework.version> 15 <quartz.version>2.2.2</quartz.version> 16 </properties> 17 18 <dependencies> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-core</artifactId> 22 <version>${springframework.version}</version> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-context-support</artifactId> 27 <version>${springframework.version}</version> 28 </dependency> 29 <!-- Transaction dependency is required with Quartz integration --> 30 <dependency> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring-tx</artifactId> 33 <version>${springframework.version}</version> 34 </dependency> 35 36 <!-- Quartz framework --> 37 <dependency> 38 <groupId>org.quartz-scheduler</groupId> 39 <artifactId>quartz</artifactId> 40 <version>${quartz.version}</version> 41 </dependency> 42 </dependencies> 43 </project>
步骤 3:Quartz Scheduler 配置作业
有两种方式在 Spring 中使用 Quartz 来配置一个作业。
A:使用 MethodInvokingJobDetailFactoryBean
这种方式在你想要调用特定 bean 的一个方法的时候很是方便,比另一种方法要简单的多。
1 <!-- For times when you just need to invoke a method on a specific object --> 2 <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 3 <property name="targetObject" ref="myBean" /> 4 <property name="targetMethod" value="printMessage" /> 5 </bean>
以上作业配置简单调用了 myBean 的 printMessage 方法,myBean 是一个简单的 POJO。
B:使用 JobDetailFactoryBean
如果你需要更高级的设置,需要给作业传递数据,想更加灵活的话就使用这种方式。
1 <!-- For times when you need more complex processing, passing data to the scheduled job --> 2 <bean name="firstComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> 3 <property name="jobClass" value="com.defonds.scheduler.jobs.FirstScheduledJob" /> 4 <property name="jobDataMap"> 5 <map> 6 <entry key="anotherBean" value-ref="anotherBean" /> 7 </map> 8 </property> 9 <property name="durability" value="true" /> 10 </bean>
jobClass 关联到一个继承自 QuartzJobBean 的类,它实现了 Quartz 作业接口。调用到这个作业的时候,它的 executeInternal 将被执行。
jobDataMap 允许我们给相关作业 bean 传递一些数据。在这个例子里,我们将 ScheduledJob 将要使用到的 'anotherBean' 传递给它。
以下是引用 jobclass(FirstScheduledJob)的具体实现。
com.defonds.scheduler.jobs.FirstScheduledJob
1 /** 2 * File Name:ScheduledJob.java 3 * 4 * Copyright Defonds Corporation 2015 5 * All Rights Reserved 6 * 7 */ 8 package com.defonds.scheduler.jobs; 9 10 import org.quartz.JobExecutionContext; 11 import org.quartz.JobExecutionException; 12 import org.springframework.scheduling.quartz.QuartzJobBean; 13 14 import com.defonds.scheduler.util.AnotherBean; 15 16 /** 17 * 18 * Project Name:spring-quartz 19 * Type Name:ScheduledJob 20 * Type Description: 21 * Author:Defonds 22 * Create Date:2015-10-29 23 * @version 24 * 25 */ 26 public class FirstScheduledJob extends QuartzJobBean { 27 28 private AnotherBean anotherBean; 29 30 @Override 31 protected void executeInternal(JobExecutionContext arg0) 32 throws JobExecutionException { 33 System.out.println("I am FirstScheduledJob"); 34 this.anotherBean.printAnotherMessage(); 35 36 } 37 38 public void setAnotherBean(AnotherBean anotherBean) { 39 this.anotherBean = anotherBean; 40 } 41 }
步骤 4:配置 Quartz 调度时要使用到的触发器
触发器用来定义调度器何时将会执行你的调度任务的那个时间。有两种可能的触发器类型:
A:简单触发器,使用 SimpleTriggerFactoryBean
你可以定义作业的启动时间、触发器之间的延迟时间以及 repeatInterval(频率)。
1 <!-- Run the job every 2 seconds with initial delay of 1 second --> 2 <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> 3 <property name="jobDetail" ref="simpleJobDetail" /> 4 <property name="startDelay" value="1000" /> 5 <property name="repeatInterval" value="2000" /> 6 </bean>
B:计划触发器,使用 CronTriggerFactoryBean
这种类型更加灵活,允许你针对特定实例选择计划方案以及将来要执行的频率。
1 <!-- Run the job every 5 seconds --> 2 <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 3 <property name="jobDetail" ref="firstComplexJobDetail" /> 4 <!--<property name="cronExpression" value="0/5 * * ? * SAT-SUN" />--> 5 <property name="cronExpression" value="0/5 * * ? * *" /> 6 7 </bean>
步骤 5:配置创建定配置 Quartz 调度器的 SchedulerFactoryBean
SchedulerFactoryBean 将 jobDetails 和 triggers 整合在一起以配置 Quartz 调度器。
1 <!-- Scheduler factory bean to glue together jobDetails and triggers to Configure Quartz Scheduler --> 2 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 3 <property name="jobDetails"> 4 <list> 5 <ref bean="simpleJobDetail" /> 6 <ref bean="firstComplexJobDetail" /> 7 <ref bean="secondComplexJobDetail" /> 8 </list> 9 </property> 10 11 <property name="triggers"> 12 <list> 13 <ref bean="simpleTrigger" /> 14 <ref bean="cronTrigger" /> 15 <ref bean="secondCronTrigger" /> 16 </list> 17 </property> 18 </bean>
下面贴出我们示例的完整的上下文文件。
src/main/resources/quartz-context.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 5 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 6 7 <context:component-scan base-package="com.defonds.scheduler" /> 8 9 10 <!-- For times when you just need to invoke a method on a specific object --> 11 <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 12 <property name="targetObject" ref="myBean" /> 13 <property name="targetMethod" value="printMessage" /> 14 </bean> 15 16 17 <!-- For times when you need more complex processing, passing data to the scheduled job --> 18 <bean name="firstComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> 19 <property name="jobClass" value="com.defonds.scheduler.jobs.FirstScheduledJob" /> 20 <property name="jobDataMap"> 21 <map> 22 <entry key="anotherBean" value-ref="anotherBean" /> 23 </map> 24 </property> 25 <property name="durability" value="true" /> 26 </bean> 27 28 <!-- Always run at 20:00 everyday --> 29 <bean name="secondComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> 30 <property name="jobClass" value="com.defonds.scheduler.jobs.SecondScheduledJob" /> 31 <property name="durability" value="true" /> 32 </bean> 33 34 35 <!-- Run the job every 2 seconds with initial delay of 1 second --> 36 <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> 37 <property name="jobDetail" ref="simpleJobDetail" /> 38 <property name="startDelay" value="1000" /> 39 <property name="repeatInterval" value="2000" /> 40 </bean> 41 42 43 <!-- Run the job every 5 seconds --> 44 <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 45 <property name="jobDetail" ref="firstComplexJobDetail" /> 46 <!--<property name="cronExpression" value="0/5 * * ? * SAT-SUN" />--> 47 <property name="cronExpression" value="0/5 * * ? * *" /> 48 49 </bean> 50 51 <!-- Always run at 20:00 everyday --> 52 <bean id="secondCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 53 <property name="jobDetail" ref="secondComplexJobDetail" /> 54 <property name="cronExpression" value="0 0 20 ? * *" /> 55 </bean> 56 57 58 <!-- Scheduler factory bean to glue together jobDetails and triggers to Configure Quartz Scheduler --> 59 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 60 <property name="jobDetails"> 61 <list> 62 <ref bean="simpleJobDetail" /> 63 <ref bean="firstComplexJobDetail" /> 64 <ref bean="secondComplexJobDetail" /> 65 </list> 66 </property> 67 68 <property name="triggers"> 69 <list> 70 <ref bean="simpleTrigger" /> 71 <ref bean="cronTrigger" /> 72 <ref bean="secondCronTrigger" /> 73 </list> 74 </property> 75 </bean> 76 77 </beans>
步骤 6:创建本文用到的几个简单 POJO 任务 Bean
com.defonds.scheduler.jobs.MyBean
1 /** 2 * File Name:MyBean.java 3 * 4 * Copyright Defonds Corporation 2015 5 * All Rights Reserved 6 * 7 */ 8 package com.defonds.scheduler.jobs; 9 10 import org.springframework.stereotype.Component; 11 12 /** 13 * 14 * Project Name:spring-quartz 15 * Type Name:MyBean 16 * Type Description: 17 * Author:Defonds 18 * Create Date:2015-10-29 19 * @version 20 * 21 */ 22 @Component("myBean") 23 public class MyBean { 24 25 public void printMessage() { 26 System.out.println("I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean"); 27 } 28 29 }
com.defonds.scheduler.util.AnotherBean
1 /** 2 * File Name:AnotherBean.java 3 * 4 * Copyright Defonds Corporation 2015 5 * All Rights Reserved 6 * 7 */ 8 package com.defonds.scheduler.util; 9 10 import org.springframework.stereotype.Component; 11 12 /** 13 * 14 * Project Name:spring-quartz 15 * Type Name:AnotherBean 16 * Type Description: 17 * Author:Defonds 18 * Create Date:2015-10-29 19 * @version 20 * 21 */ 22 @Component("anotherBean") 23 public class AnotherBean { 24 25 public void printAnotherMessage(){ 26 System.out.println("I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean"); 27 } 28 29 }
com.defonds.scheduler.jobs.SecondScheduledJob
1 /** 2 * File Name:SecondScheduledJob.java 3 * 4 * Copyright Defonds Corporation 2015 5 * All Rights Reserved 6 * 7 */ 8 package com.defonds.scheduler.jobs; 9 10 import org.quartz.JobExecutionContext; 11 import org.quartz.JobExecutionException; 12 import org.springframework.scheduling.quartz.QuartzJobBean; 13 14 /** 15 * 16 * Project Name:spring-quartz 17 * Type Name:SecondScheduledJob 18 * Type Description: 19 * Author:Defonds 20 * Create Date:2015-10-29 21 * @version 22 * 23 */ 24 public class SecondScheduledJob extends QuartzJobBean { 25 26 @Override 27 protected void executeInternal(JobExecutionContext arg0) 28 throws JobExecutionException { 29 System.out.println("I am SecondScheduledJob"); 30 31 } 32 33 }
为了演示多个执行计划的一起调度,我们写了两个 JobDetailFactoryBean,于是就有了 SecondScheduledJob。
步骤 7:创建执行程序的 Main 类
1 /** 2 * File Name:AppMain.java 3 * 4 * Copyright Defonds Corporation 2015 5 * All Rights Reserved 6 * 7 */ 8 package com.defonds.scheduler; 9 10 import org.springframework.context.support.AbstractApplicationContext; 11 import org.springframework.context.support.ClassPathXmlApplicationContext; 12 13 /** 14 * 15 * Project Name:spring-quartz 16 * Type Name:AppMain 17 * Type Description: 18 * Author:Defonds 19 * Create Date:2015-10-29 20 * @version 21 * 22 */ 23 public class AppMain { 24 25 public static void main(String args[]){ 26 AbstractApplicationContext context = new ClassPathXmlApplicationContext("quartz-context.xml"); 27 } 28 29 }
这时整个项目目录结构如下图所示:
执行这个 Main 类,输出结果如下:
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
可以看到,简单触发器调用的作业每隔两秒执行一次,而计划触发器一的则是每隔五秒钟执行一次,计划触发器二则是固定只执行了一次(晚上八点整,红色字体部分)。
后记
- 大多数情况下都会使用 JobDetailFactoryBean 进行任务调度配置;
- 每个 JobDetailFactoryBean 都有一个触发器与之匹配。配置多个调度计划,需要配置多个这种匹配对;
- 由触发器指向 JobDetailFactoryBean;
本文完整 Eclipse 下的开发项目示例已上传 CSDN 资源,有兴趣的朋友可以去下载下来参考:http://download.csdn.net/detail/defonds/9224893。