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