1.技术思路
Quartz 核心概念
- Job 表示一个工作,要执行的具体内容。此接口中只有一个方法,如下:void execute(JobExecutionContext context)
- JobDetail 表示一个具体的可执行的调度程序,Job 是这个可执行程调度程序所要执行的内容,另外 JobDetail 还包含了这个任务调度的方案和策略。
- Trigger 代表一个调度参数的配置,什么时候去调。
- Scheduler 代表一个调度容器,一个调度容器中可以注册多个 JobDetail 和 Trigger。当 Trigger 与 JobDetail 组合,就可以被 Scheduler 容器调度了。
- 启动项目,启动task监听。
- 读取数据库,将开启的任务job和trigger加载到scheduler调度器。
- 根据任务调度运行job类。
2.引入quartz依赖
<!--引入quartz定时框架-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
3.动态任务实例
QuartzBean 实体类
/**
* @author dpf
* @version 1.0
* @date 2020-9-15 17:21
* @instruction 定时任务持久类
*/
public class QuartzBean {
/** 任务id */
private String id;
/** 任务名称 */
private String jobName;
/** 任务执行类 */
private String jobClass;
/** 任务状态 启动还是暂停*/
private Integer status;
/** 任务运行时间表达式 */
private String cronExpression;
/** 任务运行时间*/
private String cronTime;
/**创建时间*/
private String createTime;
/**完成时间*/
private String completeTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getJobName() {
return jobName;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public String getJobClass() {
return jobClass;
}
public void setJobClass(String jobClass) {
this.jobClass = jobClass;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getCronExpression() {
return cronExpression;
}
public void setCronExpression(String cronExpression) {
this.cronExpression = cronExpression;
}
public String getCronTime() {
return cronTime;
}
public void setCronTime(String cronTime) {
this.cronTime = cronTime;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getCompleteTime() {
return completeTime;
}
public void setCompleteTime(String completeTime) {
this.completeTime = completeTime;
}
}
basic_quartz数据表
CREATE TABLE `basic_quartz` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '定时任务的ID',
`jobName` varchar(255) DEFAULT NULL COMMENT '任务名称',
`jobClass` varchar(255) DEFAULT NULL COMMENT '执行类',
`status` smallint(255) DEFAULT NULL COMMENT '任务状态',
`cronExpression` varchar(255) DEFAULT NULL COMMENT '执行时间表达式',
`cronTime` datetime DEFAULT NULL COMMENT '执行时间(对应cron表达式)',
`createTime` datetime DEFAULT NULL COMMENT '创建时间',
`completeTime` datetime DEFAULT NULL COMMENT '完成时间',
PRIMARY KEY (`id`),
UNIQUE KEY `INDEX_JOB_NAME_UNIQUE` (`jobName`) USING BTREE COMMENT '任务名称唯一性索引'
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
4.quartz工具类
public class QuartzUtils {
/**
* 创建定时任务 定时任务创建之后默认启动状态
* @param scheduler 调度器
* @param quartzBean 定时任务信息类
* @throws Exception
*/
public static void createScheduleJob(Scheduler scheduler, QuartzBean quartzBean){
try {
//获取到定时任务的执行类 必须是类的绝对路径名称
//定时任务类需要是job类的具体实现 QuartzJobBean是job的抽象类。
Class<? extends Job> jobClass = (Class<? extends Job>) Class.forName(quartzBean.getJobClass());
// 构建定时任务信息
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(quartzBean.getJobName()).build();
// 设置定时任务执行方式
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzBean.getCronExpression());
// 构建触发器trigger
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(quartzBean.getJobName()).withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
} catch (ClassNotFoundException e) {
System.out.println("定时任务类路径出错:请输入类的绝对路径");
} catch (SchedulerException e) {
System.out.println("创建定时任务出错:"+e.getMessage());
}
}
/**
* 根据任务名称暂停定时任务
* @param scheduler 调度器
* @param jobName 定时任务名称
* @throws SchedulerException
*/
public static void pauseScheduleJob(Scheduler scheduler, String jobName){
JobKey jobKey = JobKey.jobKey(jobName);
try {
scheduler.pauseJob(jobKey);
} catch (SchedulerException e) {
System.out.println("暂停定时任务出错:"+e.getMessage());
}
}
/**
* 根据任务名称恢复定时任务
* @param scheduler 调度器
* @param jobName 定时任务名称
* @throws SchedulerException
*/
public static void resumeScheduleJob(Scheduler scheduler, String jobName) {
JobKey jobKey = JobKey.jobKey(jobName);
try {
scheduler.resumeJob(jobKey);
} catch (SchedulerException e) {
System.out.println("启动定时任务出错:"+e.getMessage());
}
}
/**
* 根据任务名称立即运行一次定时任务
* @param scheduler 调度器
* @param jobName 定时任务名称
* @throws SchedulerException
*/
public static void runOnce(Scheduler scheduler, String jobName){
JobKey jobKey = JobKey.jobKey(jobName);
try {
scheduler.triggerJob(jobKey);
} catch (SchedulerException e) {
System.out.println("运行定时任务出错:"+e.getMessage());
}
}
/**
* 更新定时任务
* @param scheduler 调度器
* @param quartzBean 定时任务信息类
* @throws SchedulerException
*/
public static void updateScheduleJob(Scheduler scheduler, QuartzBean quartzBean) {
try {
//获取到对应任务的触发器
TriggerKey triggerKey = TriggerKey.triggerKey(quartzBean.getJobName());
//设置定时任务执行方式
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzBean.getCronExpression());
//重新构建任务的触发器trigger
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
//重置对应的job
scheduler.rescheduleJob(triggerKey, trigger);
} catch (SchedulerException e) {
System.out.println("更新定时任务出错:"+e.getMessage());
}
}
/**
* 根据定时任务名称从调度器当中删除定时任务
* @param scheduler 调度器
* @param jobName 定时任务名称
* @throws SchedulerException
*/
public static void deleteScheduleJob(Scheduler scheduler, String jobName) {
JobKey jobKey = JobKey.jobKey(jobName);
try {
scheduler.deleteJob(jobKey);
} catch (SchedulerException e) {
System.out.println("删除定时任务出错:"+e.getMessage());
}
}
/**
* 将LocalDateTime转化为cron表达式
* @param localDateTime
* @return
*/
public static String localDatTimeToCron(LocalDateTime localDateTime){
Integer year = localDateTime.getYear();
Integer month = localDateTime.getMonthValue();
Integer day = localDateTime.getDayOfMonth();
Integer hour = localDateTime.getHour();
Integer minute = localDateTime.getMinute();
Integer second = localDateTime.getSecond();
StringBuffer stringBuffer = new StringBuffer("");
stringBuffer.append(second+" ");
stringBuffer.append(minute+" ");
stringBuffer.append(hour+" ");
stringBuffer.append(day+" ");
stringBuffer.append(month+" ");
stringBuffer.append("?"+" ");
stringBuffer.append(year+" ");
return stringBuffer.toString();
}
}
5.创建任务类
public class OpenCustomerCardTask extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//TODO
// 处理定时任务的逻辑
}
}
6.创建定时任务
public void createTimingOpenCard(Integer id,LocalDateTime executeTime){
String jobName = "openCard"+id;
try {
QuartzBean quartzBean = new QuartzBean();
quartzBean.setJobClass("com.rubin.gymtime.common.quartz.OpenCustomerCardTask");
quartzBean.setJobName("openCard"+id);
quartzBean.setStatus(1401);
quartzBean.setCreateTime(CommonUtil.dateTimeToStr(LocalDateTime.now()));
quartzBean.setCronExpression(QuartzUtils.localDatTimeToCron(executeTime));
quartzBean.setCronTime(CommonUtil.dateTimeToStr(executeTime));
QuartzUtils.createScheduleJob(scheduler,quartzBean);
if (!scheduler.isShutdown()) {
scheduler.start();
}
quartzService.addQuartz(quartzBean);
} catch (Exception e) {
e.printStackTrace();
System.out.println("---------------------定时任务"+jobName+":创建失败--------------------");
}
System.out.println("---------------------定时任务"+jobName+":创建成功--------------------");
}
7.系统启动检测并启动定时任务
@Component
public class QuartzRunner implements ApplicationRunner {
@Resource
private QuartzService quartzService;
@Resource
private Scheduler scheduler;
@Override
public void run(ApplicationArguments args) throws Exception {
// 数据库查询未执行的定时任务
List<QuartzBean> list = quartzService.listUnCompleteQuartz();
// 循环创建定时任务
for(QuartzBean quartzBean : list){
System.out.println("未完成任务:"+quartzBean.getJobName());
QuartzUtils.createScheduleJob(scheduler,quartzBean);
}
// 启动所有的定时任务
if (!scheduler.isShutdown()) {
scheduler.start();
}
}
}