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