一、背景
上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等。
二、开发环境及必须jar包依赖
1.开发环境
- Spring 4.2.6.RELEASE
- Maven 3.3.9
- JDK 1.7
- Idea 15.04
2.必不可少jar包依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
三、开发步骤
【1】.基于特定基类
1. 编写任务类,该类需要继承自QuartzJobBean
package com.hafiz.www.cron; import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean; /**
* Desc:第二个基于Spring Quartz的定时任务
* Created by hafiz.zhang on 2016/12/11.
*/
public class SecondCron extends QuartzJobBean {
private static final Logger logger = LoggerFactory.getLogger(SecondCron.class);
private Integer timeout; //调度工厂实例化后,经过timeout时间开始执行调度
public void setTimeout(Integer timeout) {
this.timeout = timeout;
} @Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
logger.info("定时任务2进行中.......");
// do something else
}
}
2.在spring配置文件中配置作业类JobDetailFactoryBean、作业调度的触发方式(触发器)、调度工厂
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <bean name="secondCron" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.hafiz.www.cron.SecondCron"/>
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="0"/>
</map>
</property>
</bean> <!--按照一定频率的触发器-->
<!--<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="secondCron"/>
<property name="startDelay" value="0"/>
<property name="repeatInterval" value="2000"/>
</bean>--> <!--Cron表达式触发器-->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="secondCron"/>
<property name="cronExpression" value="0/5 * * * * ?"/>
</bean> <!--配置调度工厂-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!--<ref bean="simpleTrigger"/>-->
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</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、作业调度的触发方式(触发器)、调度工厂
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <bean name="thirdCron" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="com.hafiz.www.cron.ThirdCron"/>
</property>
<property name="targetMethod" value="executeJob"/>
<!--作业不并发调度-->
<property name="concurrent" value="false"/>
</bean> <!--按照一定频率的触发器-->
<!--<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="thirdCron"/>
<property name="startDelay" value="0"/>
<property name="repeatInterval" value="2000"/>
</bean>--> <!--Cron表达式触发器-->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="thirdCron"/>
<property name="cronExpression" value="0/5 * * * * ?"/>
</bean> <!--配置调度工厂-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!--<ref bean="simpleTrigger"/>-->
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
注意:MethodInvokingJobDetailFactoryBean,有两个关键属性:
targetObject指定任务类.
targetMethod指定运行的方法.
3.运行结果
四、总结
Quartz属于重量级的定时任务框架,我们一般都会选择轻量级的如Spring Task定时任务进行开发,但是遇到比较棘手的,这也是一种解决问题的方式。