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