懒羊羊
2023-10-17 5d91e0a52879bf16511817b3cd20496f07717ab1
提交 | 用户 | 时间
1ac2bc 1 package cn.stylefeng.guns.modular.zsx.bs.job.quartz;
2
3 import cn.stylefeng.guns.modular.zsx.bs.job.constant.QuartzEnum;
4 import cn.stylefeng.guns.modular.zsx.bs.job.entity.SysJob;
5 import lombok.SneakyThrows;
6 import org.quartz.*;
7 import org.springframework.util.ObjectUtils;
8
9 import java.util.Date;
10
11
12 /**
13  * @author fulin
14  * @since 2023/6/14 15:23
15  * <p>
16  * 此类作为操作job的工具类,这里的job与quartz中的 JobDetail一一对应 ,包括任务的增删改 {@link JobDetail}
17  * </p>
18  */
19 public class QuartzUtil {
20
21     /**
22      * 将新增的任务放入quartz调度中心中执行
23      *
24      * @param sysJob 具体任务信息
25      * @throws Exception 异常
26      */
27     @SneakyThrows
28     public static void insertOrUpdateJob(SysJob sysJob) {
29         // 获取调度器 Scheduler
30         Scheduler scheduler = SchedulerStatic.getScheduler();
31         Long jobId = sysJob.getId();
32         String jobGroup = sysJob.getJobGroup();
33
34         // 构造一个job
35         JobKey jobKey = JobKey.jobKey(jobId.toString(), jobGroup);
36         JobDetail jobDetail = JobBuilder
37                 .newJob(QuartzJob.class)
38                 .withIdentity(jobKey)
39                 .build();
40
41         // 构造cron调度器
42         CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(sysJob.getCronExpression());
43         getMisfirePolicy(sysJob, cronScheduleBuilder);
44
45         // 构造触发器 trigger
46         TriggerKey triggerKey = TriggerKey.triggerKey(jobId.toString(), jobGroup);
47         CronTrigger trigger = TriggerBuilder
48                 .newTrigger()
49                 .withIdentity(triggerKey)
50                 .withSchedule(cronScheduleBuilder)
51                 .build();
52
53         // 放入job信息,为后续执行改任务的具体方法做铺垫(需要反射调用), 在execute中获取并应用
54         jobDetail.getJobDataMap().put(QuartzEnum.jobKey, sysJob);
55         // 判断该任务是否存在,修改任务,先删除然后添加
56         if (scheduler.checkExists(jobKey)) {
57             // 防止创建时存在数据问题 先移除,然后在执行创建操作
58             scheduler.deleteJob(jobKey);
59         }
60         // 判断任务是否过期
61         CronExpression cron = new CronExpression(sysJob.getCronExpression());
62         Date nextValidTimeAfter = cron.getNextValidTimeAfter(new Date(System.currentTimeMillis()));
63         if (!ObjectUtils.isEmpty(nextValidTimeAfter)) {
64             // 执行调度任务
65             scheduler.scheduleJob(jobDetail, trigger);
66         }
67         // 暂停任务
68         if (sysJob.getStatus().equals(QuartzEnum.PAUSE)) {
69             scheduler.pauseJob(jobKey);
70         }
71     }
72
73
74     /**
75      * 根据计划执行错误策略,设置调度器
76      *
77      * @param sysJob              任务信息
78      * @param cronScheduleBuilder cron调度器
79      */
80     private static void getMisfirePolicy(SysJob sysJob, CronScheduleBuilder cronScheduleBuilder) {
81         switch (sysJob.getMisfirePolicy()) {
82             case QuartzEnum.MISFIRE_DEFAULT:
83                 break;
84             case QuartzEnum.MISFIRE_IGNORE_MISFIRES:
85                 cronScheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
86                 break;
87             case QuartzEnum.MISFIRE_FIRE_AND_PROCEED:
88                 cronScheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
89                 break;
90             case QuartzEnum.MISFIRE_DO_NOTHING:
91                 cronScheduleBuilder.withMisfireHandlingInstructionDoNothing();
92                 break;
93             default:
94                 throw new RuntimeException("The task misfire policy '" + sysJob.getMisfirePolicy() + "' cannot be used in cron schedule tasks");
95         }
96     }
97
98     /**
99      * 删除任务
100      *
101      * @param sysJob 具体任务信息(获取jobkey)
102      */
103     @SneakyThrows
104     public static void deleteJob(SysJob sysJob) {
105         // 获取调度器 Scheduler
106         Scheduler scheduler = SchedulerStatic.getScheduler();
107         scheduler.deleteJob(JobKey.jobKey(sysJob.getId().toString(), sysJob.getJobGroup()));
108     }
109 }