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