沙钢智慧能源系统后端代码
liriming
2024-12-02 a63c400ff367e3cec30db6425a142d67dbaf158d
提交 | 用户 | 时间
516ef4 1 package com.iailab.module.shasteel.job.utils;
L 2
3 import com.iailab.framework.common.constant.Constant;
4 import com.iailab.framework.common.enums.ErrorCode;
5 import com.iailab.module.shasteel.job.entity.ScheduleJobEntity;
6 import org.quartz.*;
7
8 /**
9  * 定时任务工具类
10  *
11  * @author Mark sunlightcs@gmail.com
12  */
13 public class ScheduleUtils {
14     private final static String JOB_NAME = "TASK_";
15     /**
16      * 任务调度参数key
17      */
18     public static final String JOB_PARAM_KEY = "JOB_PARAM_KEY";
19     
20     /**
21      * 获取触发器key
22      */
23     public static TriggerKey getTriggerKey(Long jobId) {
24         return TriggerKey.triggerKey(JOB_NAME + jobId);
25     }
26     
27     /**
28      * 获取jobKey
29      */
30     public static JobKey getJobKey(Long jobId) {
31         return JobKey.jobKey(JOB_NAME + jobId);
32     }
33
34     /**
35      * 获取表达式触发器
36      */
37     public static CronTrigger getCronTrigger(Scheduler scheduler, Long jobId) {
38         try {
39             return (CronTrigger) scheduler.getTrigger(getTriggerKey(jobId));
40         } catch (SchedulerException e) {
94c44e 41             throw new RuntimeException("获取表达式触发器异常", e);
516ef4 42         }
L 43     }
44
45     /**
46      * 创建定时任务
47      */
48     public static void createScheduleJob(Scheduler scheduler, ScheduleJobEntity scheduleJob) {
49         try {
50             //构建job信息
51             JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(getJobKey(scheduleJob.getId())).build();
52
53             //表达式调度构建器
54             CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression())
55                     .withMisfireHandlingInstructionDoNothing();
56
57             //按新的cronExpression表达式构建一个新的trigger
58             CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(scheduleJob.getId())).withSchedule(scheduleBuilder).build();
59
60             //放入参数,运行时的方法可以获取
61             jobDetail.getJobDataMap().put(JOB_PARAM_KEY, scheduleJob);
62
63             scheduler.scheduleJob(jobDetail, trigger);
64             
65             //暂停任务
66             if(scheduleJob.getStatus() == Constant.ScheduleStatus.PAUSE.getValue()){
67                 pauseJob(scheduler, scheduleJob.getId());
68             }
69         } catch (SchedulerException e) {
94c44e 70             throw new RuntimeException("创建定时任务异常", e);
516ef4 71         }
L 72     }
73     
74     /**
75      * 更新定时任务
76      */
77     public static void updateScheduleJob(Scheduler scheduler, ScheduleJobEntity scheduleJob) {
78         try {
79             TriggerKey triggerKey = getTriggerKey(scheduleJob.getId());
80
81             //表达式调度构建器
82             CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression())
83                     .withMisfireHandlingInstructionDoNothing();
84
85             CronTrigger trigger = getCronTrigger(scheduler, scheduleJob.getId());
86             
87             //按新的cronExpression表达式重新构建trigger
88             trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
89             
90             //参数
91             trigger.getJobDataMap().put(JOB_PARAM_KEY, scheduleJob);
92             
93             scheduler.rescheduleJob(triggerKey, trigger);
94             
95             //暂停任务
96             if(scheduleJob.getStatus() == Constant.ScheduleStatus.PAUSE.getValue()){
97                 pauseJob(scheduler, scheduleJob.getId());
98             }
99             
100         } catch (SchedulerException e) {
94c44e 101             throw new RuntimeException("更新定时任务异常", e);
516ef4 102         }
L 103     }
104
105     /**
106      * 立即执行任务
107      */
108     public static void run(Scheduler scheduler, ScheduleJobEntity scheduleJob) {
109         try {
110             //参数
111             JobDataMap dataMap = new JobDataMap();
112             dataMap.put(JOB_PARAM_KEY, scheduleJob);
113             
114             scheduler.triggerJob(getJobKey(scheduleJob.getId()), dataMap);
115         } catch (SchedulerException e) {
94c44e 116             throw new RuntimeException("立即执行任务异常", e);
516ef4 117         }
L 118     }
119
120     /**
121      * 暂停任务
122      */
123     public static void pauseJob(Scheduler scheduler, Long jobId) {
124         try {
125             scheduler.pauseJob(getJobKey(jobId));
126         } catch (SchedulerException e) {
94c44e 127             throw new RuntimeException("暂停任务异常", e);
516ef4 128         }
L 129     }
130
131     /**
132      * 恢复任务
133      */
134     public static void resumeJob(Scheduler scheduler, Long jobId) {
135         try {
136             scheduler.resumeJob(getJobKey(jobId));
137         } catch (SchedulerException e) {
94c44e 138             throw new RuntimeException("恢复任务异常", e);
516ef4 139         }
L 140     }
141
142     /**
143      * 删除定时任务
144      */
145     public static void deleteScheduleJob(Scheduler scheduler, Long jobId) {
146         try {
147             scheduler.deleteJob(getJobKey(jobId));
148         } catch (SchedulerException e) {
94c44e 149             throw new RuntimeException("删除定时任务异常", e);
516ef4 150         }
L 151     }
152 }