提交 | 用户 | 时间
|
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; |
a6de49
|
7 |
import com.iailab.module.data.job.entity.ScheduleJobEntity; |
H |
8 |
import com.iailab.module.data.job.entity.ScheduleJobLogEntity; |
cd5f85
|
9 |
import com.iailab.module.data.job.service.ScheduleJobLogService; |
0fbd01
|
10 |
import org.quartz.DisallowConcurrentExecution; |
a6de49
|
11 |
import org.quartz.JobExecutionContext; |
H |
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 |
*/ |
0fbd01
|
25 |
@DisallowConcurrentExecution |
a6de49
|
26 |
public class ScheduleJob extends QuartzJobBean { |
H |
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 |
|
0fbd01
|
34 |
//设置当前租户 |
潘 |
35 |
TenantContextHolder.setTenantId(scheduleJob.getTenantId()); |
cd5f85
|
36 |
Long tenantId = TenantContextHolder.getRequiredTenantId(); |
0fbd01
|
37 |
|
a6de49
|
38 |
//数据库保存执行记录 |
H |
39 |
ScheduleJobLogEntity log = new ScheduleJobLogEntity(); |
cd5f85
|
40 |
log.setId(System.currentTimeMillis()); |
a6de49
|
41 |
log.setJobId(scheduleJob.getId()); |
H |
42 |
log.setBeanName(scheduleJob.getBeanName()); |
|
43 |
log.setParams(scheduleJob.getParams()); |
|
44 |
log.setCreateDate(new Date()); |
cd5f85
|
45 |
log.setTenantId(tenantId); |
a6de49
|
46 |
|
H |
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 |
cd5f85
|
76 |
ScheduleJobLogService scheduleJobLogService = SpringContextUtils.getBean(ScheduleJobLogService.class); |
91aa0e
|
77 |
//scheduleJobLogService.insert(log); |
a6de49
|
78 |
} |
H |
79 |
} |
|
80 |
} |