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