以下内容基于Spring4.X、quartz1.6.6。大致想法是。通过Spring注入工厂对象,获取Scheduler。通过JobFactory解决执行类无法使用注入注解的问题。
QuartzManager封装了定时器常用操作方法
package framework.basic.quartz;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.ParseException;
/**
* @Description: 定时任务管理类
* 该类可管理XML配置的定时器(quartzManagerSchedulerFactoryBean)
* 默认任务组名为(Scheduler.DEFAULT_GROUP)
* 任务名为JobDetail的beanId
* 默认触发器组名为(Scheduler.DEFAULT_GROUP)
* 触发器名为CronTriggerBean的BeanId
* 可在代码中定义创建定时器
* 该类方法时 执行类需要继承org.quartz.Job接口 复写execute方法
* @ClassName: QuartzManager
*/
public class QuartzManager {
private static final Logger logger = LoggerFactory.getLogger(QuartzManager.class);
public QuartzManager() {
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
try {
setScheduler(schedulerFactory.getScheduler());
} catch (SchedulerException e) {
e.printStackTrace();
}
}
/**
* 通过构造器注入Spring所管理的工厂对象
* @param scheduler
*/
public QuartzManager(Scheduler scheduler) {
setScheduler(scheduler);
}
private Scheduler scheduler;
public Scheduler getScheduler() {
return scheduler;
}
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
/**
* @param jobName 任务名
* @param jobGroupName 任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param jobClass 任务
* @param time 时间设置,参考quartz说明文档
* @Description: 添加一个定时任务
*/
@SuppressWarnings("unchecked")
public void addJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName, Class jobClass,
String time) {
try {
JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任务名,任务组,任务执行类
// 触发器
CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时间设定
scheduler.scheduleJob(jobDetail, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
/**
* 暂停一个job
*
* @param jobName
* @param jobGroupName
*/
public void pauseJob(String jobName, String jobGroupName) {
try {
scheduler.pauseJob(jobName, jobGroupName);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
/**
* 恢复一个job 恢复定时器时立刻先执行一次定时器的任务 然后再按计划执行
*
* @param jobName
* @param jobGroupName
*/
public void resumeJob(String jobName, String jobGroupName) {
try {
scheduler.resumeJob(jobName, jobGroupName);
} catch (SchedulerException e) {
logger.error("恢复定时器异常:jobName[{}],jobGroupName[{}]",jobName,jobGroupName);
e.printStackTrace();
}
}
/**
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
* @Description: 移除一个任务
*/
public void removeJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName) {
try {
scheduler.pauseTrigger(triggerName, triggerGroupName);// 停止触发器
scheduler.unscheduleJob(triggerName, triggerGroupName);// 移除触发器
scheduler.deleteJob(jobName, jobGroupName);// 删除任务
} catch (SchedulerException e) {
e.printStackTrace();
}
}
/**
* @param triggerName
* @param triggerGroupName
* @param time
* @Description: 修改一个任务的触发时间
*/
public void modifyJobTime(String triggerName,
String triggerGroupName, String time) {
try {
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerName, triggerGroupName);
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
CronTrigger ct = (CronTrigger) trigger;
// 修改时间
ct.setCronExpression(time);
// 重启触发器
scheduler.resumeTrigger(triggerName, triggerGroupName);
}
} catch (SchedulerException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
/**
* @param triggerName
* @param triggerGroup
* @Description: 获取触发器状态
*
* @return
* STATE_BLOCKED 4 阻塞
* STATE_COMPLETE 2 完成
* STATE_ERROR 3 错误
* STATE_NONE -1 不存在
* STATE_NORMAL 0 正常
* STATE_PAUSED 1 暂停
*/
public int getTriggerState(String triggerName, String triggerGroup ){
try {
return scheduler.getTriggerState(triggerName,triggerGroup);
} catch (SchedulerException e) {
e.printStackTrace();
}
return 3;
}
/**
* @Description:启动所有定时任务
*/
public void startJobs() {
try {
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
/**
* @Description:关闭所有定时任务
*/
public void shutdownJobs() {
try {
if (!scheduler.isShutdown()) {
scheduler.shutdown();
}
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
JobFactory对象注入类
package framework.basic.job;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
public class JobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
//调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
//进行注入
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
WorkScheduledJod 执行类,该类需要继承org.quartz.Job,定时器通过程序控制的情况下,默认执行execute方法
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import javax.annotation.Resource;
public class WorkScheduledJod implements Job {
Logger logger = LoggerFactory.getLogger(WorkScheduledJod.class);
@Resource
private ApplicationContext context;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
logger.info("context[{}]",context);
}
}
QuartzService用于对外提供服务
package framework.basic.quartz.impl;
import cn.bthhotels.omplan.service.impl.WorkScheduledJod;
import framework.basic.quartz.QuartzManager;
import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
public class QuartzService {
@Resource
private QuartzManager quartzManager;
private static final Logger logger = LoggerFactory.getLogger(QuartzService.class);
@Override
public void startAllJod() {
logger.info("开启定时器");
quartzManager.addJob(WorkScheduledJod.JOD_ID, Scheduler.DEFAULT_GROUP, WorkScheduledJod.TRIGGER_ID, Scheduler.DEFAULT_GROUP, WorkScheduledJod.class, WorkScheduledJod.CRON_EXPRESSION);
logger.info("挂起定时器");
quartzManager.pauseJob(WorkScheduledJod.JOD_ID, Scheduler.DEFAULT_GROUP);
logger.info("恢复定时器");
quartzManager.resumeJob(WorkScheduledJod.JOD_ID, Scheduler.DEFAULT_GROUP);
logger.info("删除定时器");
quartzManager.removeJob(WorkScheduledJod.JOD_ID, Scheduler.DEFAULT_GROUP, WorkScheduledJod.TRIGGER_ID, Scheduler.DEFAULT_GROUP);
}
public QuartzManager getQuartzManager() {
return quartzManager;
}
}
Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 用于控制定时器的自动注入 -->
<bean id="jobFactory" class="framework.basic.job.JobFactory"></bean>
<!-- QuartzManager管理的总定时器 -->
<bean id="quartzManagerSchedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no">
<property name="jobFactory" ref="jobFactory"></property>
</bean>
<!-- 定时器管理工具类 -->
<bean id="quartzManager" class="framework.basic.quartz.QuartzManager" lazy-init="false" >
<constructor-arg name="scheduler" ref="quartzManagerSchedulerFactoryBean"/>
</bean>
<!-- 定时器服务 这里仅作启动演示 -->
<bean id="quartzService" class="framework.basic.quartz.impl.QuartzService" init-method="startAllJod" >
</bean>
</beans>
tips:
- 可以将QuartzManager 改成工具类的方式,将对象里的scheduler,以及方法调整为静态;
- 因为Quartz版本不同,后续添加方法使用JobKey或TriggerKey;
- JobDetail有个getJobDataMap()方法,通过该MAP添加的对象能在执行类execute方法JobExecutionContext对象的getMergedJobDataMap获取。
- 通过XML配置的定时器默认组名为(Scheduler.DEFAULT_GROUP)