一、背景
上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等。
二、开发环境及必须jar包依赖
1.开发环境
- Spring 4.2.6.RELEASE
- Maven 3.3.9
- JDK 1.7
- Idea 15.04
2.必不可少jar包依赖
1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-context-support</artifactId> 4 <version>4.2.6.RELEASE</version> 5 </dependency> 6 <dependency> 7 <groupId>org.quartz-scheduler</groupId> 8 <artifactId>quartz</artifactId> 9 <version>2.2.1</version> 10 </dependency>
三、开发步骤
【1】.基于特定基类
1. 编写任务类,该类需要继承自QuartzJobBean
1 package com.hafiz.www.cron; 2 3 import org.quartz.JobExecutionContext; 4 import org.quartz.JobExecutionException; 5 import org.slf4j.Logger; 6 import org.slf4j.LoggerFactory; 7 import org.springframework.scheduling.quartz.QuartzJobBean; 8 9 /** 10 * Desc:第二个基于Spring Quartz的定时任务 11 * Created by hafiz.zhang on 2016/12/11. 12 */ 13 public class SecondCron extends QuartzJobBean { 14 private static final Logger logger = LoggerFactory.getLogger(SecondCron.class); 15 private Integer timeout; 16 17 //调度工厂实例化后,经过timeout时间开始执行调度 18 public void setTimeout(Integer timeout) { 19 this.timeout = timeout; 20 } 21 22 @Override 23 protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { 24 logger.info("定时任务2进行中......."); 25 // do something else 26 } 27 }
2.在spring配置文件中配置作业类JobDetailFactoryBean、作业调度的触发方式(触发器)、调度工厂
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:task="http://www.springframework.org/schema/task" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 6 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> 7 8 <bean name="secondCron" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> 9 <property name="jobClass" value="com.hafiz.www.cron.SecondCron"/> 10 <property name="jobDataAsMap"> 11 <map> 12 <entry key="timeout" value="0"/> 13 </map> 14 </property> 15 </bean> 16 17 <!--按照一定频率的触发器--> 18 <!--<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> 19 <property name="jobDetail" ref="secondCron"/> 20 <property name="startDelay" value="0"/> 21 <property name="repeatInterval" value="2000"/> 22 </bean>--> 23 24 <!--Cron表达式触发器--> 25 <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 26 <property name="jobDetail" ref="secondCron"/> 27 <property name="cronExpression" value="0/5 * * * * ?"/> 28 </bean> 29 30 <!--配置调度工厂--> 31 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 32 <property name="triggers"> 33 <list> 34 <!--<ref bean="simpleTrigger"/>--> 35 <ref bean="cronTrigger"/> 36 </list> 37 </property> 38 </bean> 39 </beans>
注意:
1).org.springframework.scheduling.quartz.JobDetailBean有两个属性.
jobClass属性即我们在java代码中定义的任务类.
jobDataAsMap属性即该任务类中需要注入的属性值.
2).Quartz的作业触发器有两种(两种任选其一就行)
org.springframework.scheduling.quartz.SimpleTriggerFactoryBean:只支持按照一定频度调用任务,如每隔30分钟运行一次.
org.springframework.scheduling.quartz.CronTriggerFactoryBean:支持到指定时间运行一次,如每天12:00运行一次等.
关于第二种触发器的Cron表达式,请参见另一篇博客:摆脱Spring 定时任务的@Scheduled cron表达式的困扰
3).org.springframework.scheduling.quartz.SchedulerFactoryBean的参数triggers指定的就是之前配置的触发器的名字.
3.运行结果
【2】.不基于特定的基类
1.编写任务调度类
package com.hafiz.www.cron; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Desc:基于Spring整合Quartz进行完成定时任务 * Created by hafiz.zhang on 2016/12/11. */ public class ThirdCron { private static final Logger logger = LoggerFactory.getLogger(ThirdCron.class); public void executeJob() { logger.info("定时任务3进行中......."); // do something else } }
2.在spring配置文件中配置作业类MethodInvokingJobDetailFactoryBean、作业调度的触发方式(触发器)、调度工厂
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://www.springframework.org/schema/beans 4 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> 5 6 <bean name="thirdCron" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 7 <property name="targetObject"> 8 <bean class="com.hafiz.www.cron.ThirdCron"/> 9 </property> 10 <property name="targetMethod" value="executeJob"/> 11 <!--作业不并发调度--> 12 <property name="concurrent" value="false"/> 13 </bean> 14 15 <!--按照一定频率的触发器--> 16 <!--<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> 17 <property name="jobDetail" ref="thirdCron"/> 18 <property name="startDelay" value="0"/> 19 <property name="repeatInterval" value="2000"/> 20 </bean>--> 21 22 <!--Cron表达式触发器--> 23 <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 24 <property name="jobDetail" ref="thirdCron"/> 25 <property name="cronExpression" value="0/5 * * * * ?"/> 26 </bean> 27 28 <!--配置调度工厂--> 29 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 30 <property name="triggers"> 31 <list> 32 <!--<ref bean="simpleTrigger"/>--> 33 <ref bean="cronTrigger"/> 34 </list> 35 </property> 36 </bean> 37 </beans>
注意:MethodInvokingJobDetailFactoryBean,有两个关键属性:
targetObject指定任务类.
targetMethod指定运行的方法.
3.运行结果
四、总结
Quartz属于重量级的定时任务框架,我们一般都会选择轻量级的如Spring Task定时任务进行开发,但是遇到比较棘手的,这也是一种解决问题的方式。