提交 | 用户 | 时间
|
0cceb6
|
1 |
package com.jcdm.quartz.service.impl; |
Y |
2 |
|
|
3 |
import java.util.List; |
|
4 |
import javax.annotation.PostConstruct; |
|
5 |
import org.quartz.JobDataMap; |
|
6 |
import org.quartz.JobKey; |
|
7 |
import org.quartz.Scheduler; |
|
8 |
import org.quartz.SchedulerException; |
|
9 |
import org.springframework.beans.factory.annotation.Autowired; |
|
10 |
import org.springframework.stereotype.Service; |
|
11 |
import org.springframework.transaction.annotation.Transactional; |
|
12 |
import com.jcdm.common.constant.ScheduleConstants; |
|
13 |
import com.jcdm.common.exception.job.TaskException; |
|
14 |
import com.jcdm.quartz.domain.SysJob; |
|
15 |
import com.jcdm.quartz.mapper.SysJobMapper; |
|
16 |
import com.jcdm.quartz.service.ISysJobService; |
|
17 |
import com.jcdm.quartz.util.CronUtils; |
|
18 |
import com.jcdm.quartz.util.ScheduleUtils; |
|
19 |
|
|
20 |
/** |
|
21 |
* 定时任务调度信息 服务层 |
|
22 |
* |
|
23 |
* @author jc |
|
24 |
*/ |
|
25 |
@Service |
|
26 |
public class SysJobServiceImpl implements ISysJobService |
|
27 |
{ |
|
28 |
@Autowired |
|
29 |
private Scheduler scheduler; |
|
30 |
|
|
31 |
@Autowired |
|
32 |
private SysJobMapper jobMapper; |
|
33 |
|
|
34 |
/** |
|
35 |
* 项目启动时,初始化定时器 主要是防止手动修改数据库导致未同步到定时任务处理(注:不能手动修改数据库ID和任务组名,否则会导致脏数据) |
|
36 |
*/ |
|
37 |
@PostConstruct |
|
38 |
public void init() throws SchedulerException, TaskException |
|
39 |
{ |
|
40 |
scheduler.clear(); |
|
41 |
List<SysJob> jobList = jobMapper.selectJobAll(); |
|
42 |
for (SysJob job : jobList) |
|
43 |
{ |
|
44 |
ScheduleUtils.createScheduleJob(scheduler, job); |
|
45 |
} |
|
46 |
} |
|
47 |
|
|
48 |
/** |
|
49 |
* 获取quartz调度器的计划任务列表 |
|
50 |
* |
|
51 |
* @param job 调度信息 |
|
52 |
* @return |
|
53 |
*/ |
|
54 |
@Override |
|
55 |
public List<SysJob> selectJobList(SysJob job) |
|
56 |
{ |
|
57 |
return jobMapper.selectJobList(job); |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* 通过调度任务ID查询调度信息 |
|
62 |
* |
|
63 |
* @param jobId 调度任务ID |
|
64 |
* @return 调度任务对象信息 |
|
65 |
*/ |
|
66 |
@Override |
|
67 |
public SysJob selectJobById(Long jobId) |
|
68 |
{ |
|
69 |
return jobMapper.selectJobById(jobId); |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* 暂停任务 |
|
74 |
* |
|
75 |
* @param job 调度信息 |
|
76 |
*/ |
|
77 |
@Override |
|
78 |
@Transactional(rollbackFor = Exception.class) |
|
79 |
public int pauseJob(SysJob job) throws SchedulerException |
|
80 |
{ |
|
81 |
Long jobId = job.getJobId(); |
|
82 |
String jobGroup = job.getJobGroup(); |
|
83 |
job.setStatus(ScheduleConstants.Status.PAUSE.getValue()); |
|
84 |
int rows = jobMapper.updateJob(job); |
|
85 |
if (rows > 0) |
|
86 |
{ |
|
87 |
scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup)); |
|
88 |
} |
|
89 |
return rows; |
|
90 |
} |
|
91 |
|
|
92 |
/** |
|
93 |
* 恢复任务 |
|
94 |
* |
|
95 |
* @param job 调度信息 |
|
96 |
*/ |
|
97 |
@Override |
|
98 |
@Transactional(rollbackFor = Exception.class) |
|
99 |
public int resumeJob(SysJob job) throws SchedulerException |
|
100 |
{ |
|
101 |
Long jobId = job.getJobId(); |
|
102 |
String jobGroup = job.getJobGroup(); |
|
103 |
job.setStatus(ScheduleConstants.Status.NORMAL.getValue()); |
|
104 |
int rows = jobMapper.updateJob(job); |
|
105 |
if (rows > 0) |
|
106 |
{ |
|
107 |
scheduler.resumeJob(ScheduleUtils.getJobKey(jobId, jobGroup)); |
|
108 |
} |
|
109 |
return rows; |
|
110 |
} |
|
111 |
|
|
112 |
/** |
|
113 |
* 删除任务后,所对应的trigger也将被删除 |
|
114 |
* |
|
115 |
* @param job 调度信息 |
|
116 |
*/ |
|
117 |
@Override |
|
118 |
@Transactional(rollbackFor = Exception.class) |
|
119 |
public int deleteJob(SysJob job) throws SchedulerException |
|
120 |
{ |
|
121 |
Long jobId = job.getJobId(); |
|
122 |
String jobGroup = job.getJobGroup(); |
|
123 |
int rows = jobMapper.deleteJobById(jobId); |
|
124 |
if (rows > 0) |
|
125 |
{ |
|
126 |
scheduler.deleteJob(ScheduleUtils.getJobKey(jobId, jobGroup)); |
|
127 |
} |
|
128 |
return rows; |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* 批量删除调度信息 |
|
133 |
* |
|
134 |
* @param jobIds 需要删除的任务ID |
|
135 |
* @return 结果 |
|
136 |
*/ |
|
137 |
@Override |
|
138 |
@Transactional(rollbackFor = Exception.class) |
|
139 |
public void deleteJobByIds(Long[] jobIds) throws SchedulerException |
|
140 |
{ |
|
141 |
for (Long jobId : jobIds) |
|
142 |
{ |
|
143 |
SysJob job = jobMapper.selectJobById(jobId); |
|
144 |
deleteJob(job); |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
/** |
|
149 |
* 任务调度状态修改 |
|
150 |
* |
|
151 |
* @param job 调度信息 |
|
152 |
*/ |
|
153 |
@Override |
|
154 |
@Transactional(rollbackFor = Exception.class) |
|
155 |
public int changeStatus(SysJob job) throws SchedulerException |
|
156 |
{ |
|
157 |
int rows = 0; |
|
158 |
String status = job.getStatus(); |
|
159 |
if (ScheduleConstants.Status.NORMAL.getValue().equals(status)) |
|
160 |
{ |
|
161 |
rows = resumeJob(job); |
|
162 |
} |
|
163 |
else if (ScheduleConstants.Status.PAUSE.getValue().equals(status)) |
|
164 |
{ |
|
165 |
rows = pauseJob(job); |
|
166 |
} |
|
167 |
return rows; |
|
168 |
} |
|
169 |
|
|
170 |
/** |
|
171 |
* 立即运行任务 |
|
172 |
* |
|
173 |
* @param job 调度信息 |
|
174 |
*/ |
|
175 |
@Override |
|
176 |
@Transactional(rollbackFor = Exception.class) |
|
177 |
public boolean run(SysJob job) throws SchedulerException |
|
178 |
{ |
|
179 |
boolean result = false; |
|
180 |
Long jobId = job.getJobId(); |
|
181 |
String jobGroup = job.getJobGroup(); |
|
182 |
SysJob properties = selectJobById(job.getJobId()); |
|
183 |
// 参数 |
|
184 |
JobDataMap dataMap = new JobDataMap(); |
|
185 |
dataMap.put(ScheduleConstants.TASK_PROPERTIES, properties); |
|
186 |
JobKey jobKey = ScheduleUtils.getJobKey(jobId, jobGroup); |
|
187 |
if (scheduler.checkExists(jobKey)) |
|
188 |
{ |
|
189 |
result = true; |
|
190 |
scheduler.triggerJob(jobKey, dataMap); |
|
191 |
} |
|
192 |
return result; |
|
193 |
} |
|
194 |
|
|
195 |
/** |
|
196 |
* 新增任务 |
|
197 |
* |
|
198 |
* @param job 调度信息 调度信息 |
|
199 |
*/ |
|
200 |
@Override |
|
201 |
@Transactional(rollbackFor = Exception.class) |
|
202 |
public int insertJob(SysJob job) throws SchedulerException, TaskException |
|
203 |
{ |
|
204 |
job.setStatus(ScheduleConstants.Status.PAUSE.getValue()); |
|
205 |
int rows = jobMapper.insertJob(job); |
|
206 |
if (rows > 0) |
|
207 |
{ |
|
208 |
ScheduleUtils.createScheduleJob(scheduler, job); |
|
209 |
} |
|
210 |
return rows; |
|
211 |
} |
|
212 |
|
|
213 |
/** |
|
214 |
* 更新任务的时间表达式 |
|
215 |
* |
|
216 |
* @param job 调度信息 |
|
217 |
*/ |
|
218 |
@Override |
|
219 |
@Transactional(rollbackFor = Exception.class) |
|
220 |
public int updateJob(SysJob job) throws SchedulerException, TaskException |
|
221 |
{ |
|
222 |
SysJob properties = selectJobById(job.getJobId()); |
|
223 |
int rows = jobMapper.updateJob(job); |
|
224 |
if (rows > 0) |
|
225 |
{ |
|
226 |
updateSchedulerJob(job, properties.getJobGroup()); |
|
227 |
} |
|
228 |
return rows; |
|
229 |
} |
|
230 |
|
|
231 |
/** |
|
232 |
* 更新任务 |
|
233 |
* |
|
234 |
* @param job 任务对象 |
|
235 |
* @param jobGroup 任务组名 |
|
236 |
*/ |
|
237 |
public void updateSchedulerJob(SysJob job, String jobGroup) throws SchedulerException, TaskException |
|
238 |
{ |
|
239 |
Long jobId = job.getJobId(); |
|
240 |
// 判断是否存在 |
|
241 |
JobKey jobKey = ScheduleUtils.getJobKey(jobId, jobGroup); |
|
242 |
if (scheduler.checkExists(jobKey)) |
|
243 |
{ |
|
244 |
// 防止创建时存在数据问题 先移除,然后在执行创建操作 |
|
245 |
scheduler.deleteJob(jobKey); |
|
246 |
} |
|
247 |
ScheduleUtils.createScheduleJob(scheduler, job); |
|
248 |
} |
|
249 |
|
|
250 |
/** |
|
251 |
* 校验cron表达式是否有效 |
|
252 |
* |
|
253 |
* @param cronExpression 表达式 |
|
254 |
* @return 结果 |
|
255 |
*/ |
|
256 |
@Override |
|
257 |
public boolean checkCronExpressionIsValid(String cronExpression) |
|
258 |
{ |
|
259 |
return CronUtils.isValid(cronExpression); |
|
260 |
} |
|
261 |
} |