几个概念:
1、Scheduler:
调度器,进行任务调度,可以理解成一名调度员,根据提供给他的注册信息,由他完成任务的调度工作。
2、Job:
业务组件,定时任务通过实现此接口,完成任务逻辑。调度器会调用此接口的execute方法完成设计好的功能。
3、Trigger:
触发器,用来定义一个指定的Job何时被执行。
4、JobBuilder:
Job构建器,用来定义或创建JobDetail的实例;JobDetail限定了只能是Job的实例。
5、TriggerBuilder:
触发器构建器,用来定义或创建触发器的实例。
包引入:
Spring boot 2.0版本以上提供了Quartz包,只需要在pom中引用即可。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
任务管理工具类
package com.ucontrol.ms.databaseservice.quartz;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author nanhao
*/
@Component
public class QuartzManager {
private final Scheduler sched;
/**
* 任务组
*/
private static String JOB_GROUP_NAME = "ATAO_JOBGROUP";
/**
* 触发器组
*/
private static String TRIGGER_GROUP_NAME = "ATAO_TRIGGERGROUP";
@Autowired
public QuartzManager(Scheduler sched) {
this.sched = sched;
}
/**
* 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
*
* @param jobName 任务名
* @param cls 任务
* @param time 时间设置,参考quartz说明文档
*/
public void addJob(String jobName, Class<? extends Job> cls, String time) {
try {
//用于描叙Job实现类及其他的一些静态信息,构建一个作业实例
JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName, JOB_GROUP_NAME).build();
//创建一个新的TriggerBuilder来规范一个触发器
//给触发器起一个名字和组名
CronTrigger trigger = TriggerBuilder
.newTrigger()
.withIdentity(jobName, TRIGGER_GROUP_NAME)
.withSchedule(CronScheduleBuilder.cronSchedule(time))
.build();
sched.scheduleJob(jobDetail, trigger);
if (!sched.isShutdown()) {
// 启动
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名 (带参数)
*
* @param jobName 任务名
* @param cls 任务
* @param time 时间设置,参考quartz说明文档
*/
void addJob(String jobName, Class<? extends Job> cls, String time, Map<String, Object> parameter) {
try {
JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName, JOB_GROUP_NAME).build();
jobDetail.getJobDataMap().putAll(parameter);
CronTrigger trigger = TriggerBuilder
.newTrigger()
.withIdentity(jobName, TRIGGER_GROUP_NAME)
.withSchedule(CronScheduleBuilder.cronSchedule(time))
.build();
sched.scheduleJob(jobDetail, trigger);
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 添加一个定时任务
*
* @param jobName 任务名
* @param jobGroupName 任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param jobClass 任务
* @param time 时间设置,参考quartz说明文档
*/
public void addJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName, Class<? extends Job> jobClass,
String time) {
try {
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
CronTrigger trigger = TriggerBuilder
.newTrigger()
.withIdentity(triggerName, triggerGroupName)
.withSchedule(CronScheduleBuilder.cronSchedule(time))
.build();
sched.scheduleJob(jobDetail, trigger);
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 添加一个定时任务 (带参数)
*
* @param jobName 任务名
* @param jobGroupName 任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param jobClass 任务
* @param time 时间设置,参考quartz说明文档
* @param parameter 参数
*/
public void addJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName, Class<? extends Job> jobClass,
String time, Map<String, Object> parameter) {
try {
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
jobDetail.getJobDataMap().put("parameterList", parameter);
CronTrigger trigger = TriggerBuilder
.newTrigger()
.withIdentity(triggerName, triggerGroupName)
.withSchedule(CronScheduleBuilder.cronSchedule(time))
.build();
sched.scheduleJob(jobDetail, trigger);
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
*
* @param jobName 任务名
* @param time 新的时间设置
*/
public void modifyJobTime(String jobName, String time) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME);
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME);
JobDetail jobDetail = sched.getJobDetail(jobKey);
Class<? extends Job> objJobClass = jobDetail.getJobClass();
removeJob(jobName);
addJob(jobName, objJobClass, time);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 修改一个任务的触发时间
*
* @param triggerName 任务名称
* @param triggerGroupName 传过来的任务名称
* @param time 更新后的时间规则
*/
public void modifyJobTime(String triggerName, String triggerGroupName, String time) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
if (trigger == null){
return;
}
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(trigger.getCronExpression());
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
trigger = (CronTrigger) trigger.getTriggerBuilder()
.withIdentity(triggerKey)
.withSchedule(scheduleBuilder)
.withSchedule(CronScheduleBuilder.cronSchedule(time))
.build();
sched.rescheduleJob(triggerKey, trigger);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
*
* @param jobName 任务名称
*/
public void removeJob(String jobName) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME);
JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME);
sched.pauseTrigger(triggerKey);
sched.unscheduleJob(triggerKey);
sched.deleteJob(jobKey);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 移除一个任务
*
* @param jobName 任务名
* @param jobGroupName 任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
*/
public void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
sched.pauseTrigger(triggerKey);
sched.unscheduleJob(triggerKey);
sched.deleteJob(jobKey);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 启动所有定时任务
*/
public void startJobs() {
try {
sched.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 关闭所有定时任务
*/
public void shutdownJobs() {
try {
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Job的接口实现例子
import org.quartz.Job;
import org.quartz.JobExecutionContext;
public class JobTodo implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
//do something
}
}
定时任务的注册
使用@Configuration注解,注入前文的定时任务管理工具类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author nanhao
*/
@Configuration
@Slf4j
public class InitializationJobManager {
private final
QuartzManager quartzManager;
@Autowired
public InitializationJobManager(QuartzManager quartzManager) {
this.quartzManager = quartzManager;
}
@Bean
public void initialization(){
quartzManager.addJob("AlarmStatisticsJob", AlarmStatisticsJob.class, "0/10 * * * * ?", params);
quartzManager.addJob("SimuHisWriteJob", SimuHisWriteJob.class, "0/1 * * * * ?");
}
private Map<String, Object> getAlarmStatisticsJobParams(){
ArrayList<Map> alarmList = modelService.getAlarmList();
Map<String, Object> params = new HashMap<>(16);
params.put("list", alarmList);
return params;
}
}
关于参数传递
在以Map的形式,将参数传至定时任务,通过下面方法在定时任务接口实现时取出参数。
JobDataMap paramMap = jobExecutionContext.getJobDetail().getJobDataMap();
ArrayList mapList = JSON.parseObject(JSON.toJSONString(paramMap.get("list")), ArrayList.class);
网友评论