cee572f2
RuoYi
若依 3.0
|
1
2
3
|
package com.ruoyi.quartz.util;
import java.util.Date;
|
c807de1f
YunaiV
初始化 quartz 模块~
|
4
|
|
cee572f2
RuoYi
若依 3.0
|
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.ScheduleConstants;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.domain.SysJobLog;
import com.ruoyi.quartz.service.ISysJobLogService;
/**
* 抽象quartz调用
*
* @author ruoyi
*/
|
c807de1f
YunaiV
初始化 quartz 模块~
|
25
|
public abstract class AbstractQuartzJob implements Job {
|
cee572f2
RuoYi
若依 3.0
|
26
27
28
29
30
31
32
33
|
private static final Logger log = LoggerFactory.getLogger(AbstractQuartzJob.class);
/**
* 线程本地变量
*/
private static ThreadLocal<Date> threadLocal = new ThreadLocal<>();
@Override
|
c807de1f
YunaiV
初始化 quartz 模块~
|
34
|
public void execute(JobExecutionContext context) throws JobExecutionException {
|
cee572f2
RuoYi
若依 3.0
|
35
36
|
SysJob sysJob = new SysJob();
BeanUtils.copyBeanProp(sysJob, context.getMergedJobDataMap().get(ScheduleConstants.TASK_PROPERTIES));
|
c807de1f
YunaiV
初始化 quartz 模块~
|
37
|
try {
|
cee572f2
RuoYi
若依 3.0
|
38
|
before(context, sysJob);
|
c807de1f
YunaiV
初始化 quartz 模块~
|
39
|
if (sysJob != null) {
|
cee572f2
RuoYi
若依 3.0
|
40
41
42
|
doExecute(context, sysJob);
}
after(context, sysJob, null);
|
c807de1f
YunaiV
初始化 quartz 模块~
|
43
|
} catch (Exception e) {
|
cee572f2
RuoYi
若依 3.0
|
44
45
46
47
48
49
50
51
52
|
log.error("任务执行异常 - :", e);
after(context, sysJob, e);
}
}
/**
* 执行前
*
* @param context 工作执行上下文对象
|
c807de1f
YunaiV
初始化 quartz 模块~
|
53
|
* @param sysJob 系统计划任务
|
cee572f2
RuoYi
若依 3.0
|
54
|
*/
|
c807de1f
YunaiV
初始化 quartz 模块~
|
55
|
protected void before(JobExecutionContext context, SysJob sysJob) {
|
cee572f2
RuoYi
若依 3.0
|
56
57
58
59
60
61
62
|
threadLocal.set(new Date());
}
/**
* 执行后
*
* @param context 工作执行上下文对象
|
c807de1f
YunaiV
初始化 quartz 模块~
|
63
|
* @param sysJob 系统计划任务
|
cee572f2
RuoYi
若依 3.0
|
64
|
*/
|
c807de1f
YunaiV
初始化 quartz 模块~
|
65
|
protected void after(JobExecutionContext context, SysJob sysJob, Exception e) {
|
cee572f2
RuoYi
若依 3.0
|
66
67
68
69
70
71
72
73
74
75
76
|
Date startTime = threadLocal.get();
threadLocal.remove();
final SysJobLog sysJobLog = new SysJobLog();
sysJobLog.setJobName(sysJob.getJobName());
sysJobLog.setJobGroup(sysJob.getJobGroup());
sysJobLog.setInvokeTarget(sysJob.getInvokeTarget());
sysJobLog.setStartTime(startTime);
sysJobLog.setStopTime(new Date());
long runMs = sysJobLog.getStopTime().getTime() - sysJobLog.getStartTime().getTime();
sysJobLog.setJobMessage(sysJobLog.getJobName() + " 总共耗时:" + runMs + "毫秒");
|
c807de1f
YunaiV
初始化 quartz 模块~
|
77
|
if (e != null) {
|
cee572f2
RuoYi
若依 3.0
|
78
79
80
|
sysJobLog.setStatus(Constants.FAIL);
String errorMsg = StringUtils.substring(ExceptionUtil.getExceptionMessage(e), 0, 2000);
sysJobLog.setExceptionInfo(errorMsg);
|
c807de1f
YunaiV
初始化 quartz 模块~
|
81
|
} else {
|
cee572f2
RuoYi
若依 3.0
|
82
83
84
85
86
87
88
89
90
91
92
|
sysJobLog.setStatus(Constants.SUCCESS);
}
// 写入数据库当中
SpringUtils.getBean(ISysJobLogService.class).addJobLog(sysJobLog);
}
/**
* 执行方法,由子类重载
*
* @param context 工作执行上下文对象
|
c807de1f
YunaiV
初始化 quartz 模块~
|
93
|
* @param sysJob 系统计划任务
|
cee572f2
RuoYi
若依 3.0
|
94
95
96
97
|
* @throws Exception 执行过程中的异常
*/
protected abstract void doExecute(JobExecutionContext context, SysJob sysJob) throws Exception;
}
|