提交 | 用户 | 时间
|
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.exception.ExceptionUtils; |
|
5 |
import com.iailab.framework.common.util.spring.SpringContextUtils; |
0fbd01
|
6 |
import com.iailab.framework.tenant.core.context.TenantContextHolder; |
1c9291
|
7 |
import com.iailab.module.data.job.config.ScheduleJobListConfig; |
a6de49
|
8 |
import com.iailab.module.data.job.entity.ScheduleJobEntity; |
H |
9 |
import com.iailab.module.data.job.entity.ScheduleJobLogEntity; |
cd5f85
|
10 |
import com.iailab.module.data.job.service.ScheduleJobLogService; |
0fbd01
|
11 |
import org.quartz.DisallowConcurrentExecution; |
a6de49
|
12 |
import org.quartz.JobExecutionContext; |
H |
13 |
import org.slf4j.Logger; |
|
14 |
import org.slf4j.LoggerFactory; |
|
15 |
import org.springframework.scheduling.quartz.QuartzJobBean; |
|
16 |
|
|
17 |
import java.lang.reflect.Method; |
|
18 |
import java.util.Date; |
|
19 |
|
|
20 |
|
|
21 |
/** |
|
22 |
* 定时任务 |
|
23 |
* |
|
24 |
* @author Mark sunlightcs@gmail.com |
|
25 |
*/ |
0fbd01
|
26 |
@DisallowConcurrentExecution |
a6de49
|
27 |
public class ScheduleJob extends QuartzJobBean { |
H |
28 |
private Logger logger = LoggerFactory.getLogger(getClass()); |
|
29 |
|
|
30 |
@Override |
|
31 |
protected void executeInternal(JobExecutionContext context) { |
|
32 |
ScheduleJobEntity scheduleJob = (ScheduleJobEntity) context.getMergedJobDataMap(). |
|
33 |
get(ScheduleUtils.JOB_PARAM_KEY); |
|
34 |
|
1c9291
|
35 |
// 过滤定时任务 |
9ab5e8
|
36 |
if (!ScheduleJobListConfig.scheduleJobList.contains(scheduleJob.getBeanName())) { |
1c9291
|
37 |
logger.info("过滤定时任务,BeanName=" + scheduleJob.getBeanName()); |
L |
38 |
return; |
|
39 |
} |
|
40 |
|
0fbd01
|
41 |
//设置当前租户 |
潘 |
42 |
TenantContextHolder.setTenantId(scheduleJob.getTenantId()); |
cd5f85
|
43 |
Long tenantId = TenantContextHolder.getRequiredTenantId(); |
0fbd01
|
44 |
|
a6de49
|
45 |
//数据库保存执行记录 |
H |
46 |
ScheduleJobLogEntity log = new ScheduleJobLogEntity(); |
cd5f85
|
47 |
log.setId(System.currentTimeMillis()); |
a6de49
|
48 |
log.setJobId(scheduleJob.getId()); |
H |
49 |
log.setBeanName(scheduleJob.getBeanName()); |
|
50 |
log.setParams(scheduleJob.getParams()); |
|
51 |
log.setCreateDate(new Date()); |
cd5f85
|
52 |
log.setTenantId(tenantId); |
a6de49
|
53 |
|
H |
54 |
//任务开始时间 |
|
55 |
long startTime = System.currentTimeMillis(); |
|
56 |
|
|
57 |
try { |
|
58 |
//执行任务 |
|
59 |
logger.info("任务准备执行,任务ID:{}", scheduleJob.getId()); |
|
60 |
Object target = SpringContextUtils.getBean(scheduleJob.getBeanName()); |
|
61 |
Method method = target.getClass().getDeclaredMethod("run", String.class); |
|
62 |
method.invoke(target, scheduleJob.getParams()); |
|
63 |
|
|
64 |
//任务执行总时长 |
|
65 |
long times = System.currentTimeMillis() - startTime; |
|
66 |
log.setTimes((int)times); |
|
67 |
//任务状态 |
|
68 |
log.setStatus(Constant.SUCCESS); |
|
69 |
|
|
70 |
logger.info("任务执行完毕,任务ID:{} 总共耗时:{} 毫秒", scheduleJob.getId(), times); |
|
71 |
} catch (Exception e) { |
|
72 |
logger.error("任务执行失败,任务ID:{}", scheduleJob.getId(), e); |
|
73 |
|
|
74 |
//任务执行总时长 |
|
75 |
long times = System.currentTimeMillis() - startTime; |
|
76 |
log.setTimes((int)times); |
|
77 |
|
|
78 |
//任务状态 |
|
79 |
log.setStatus(Constant.FAIL); |
|
80 |
log.setError(ExceptionUtils.getErrorStackTrace(e)); |
|
81 |
}finally { |
|
82 |
//获取spring bean |
cd5f85
|
83 |
ScheduleJobLogService scheduleJobLogService = SpringContextUtils.getBean(ScheduleJobLogService.class); |
91aa0e
|
84 |
//scheduleJobLogService.insert(log); |
a6de49
|
85 |
} |
H |
86 |
} |
|
87 |
} |