Jay
2025-03-04 c350c9634dcc07db6a805d4cbc1bfea2677e61a1
提交 | 用户 | 时间
1c9291 1 package com.iailab.module.model.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.model.job.config.ScheduleJobListConfig;
8 import com.iailab.module.model.job.entity.ScheduleJobEntity;
9 import com.iailab.module.model.job.entity.ScheduleJobLogEntity;
10 import com.iailab.module.model.job.service.ScheduleJobLogService;
11 import org.quartz.DisallowConcurrentExecution;
12 import org.quartz.JobExecutionContext;
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  */
26 @DisallowConcurrentExecution
27 public class ScheduleJob extends QuartzJobBean {
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
35         // 过滤定时任务
0ff682 36         if (!ScheduleJobListConfig.scheduleJobList.contains(scheduleJob.getBeanName())) {
1c9291 37             logger.info("过滤定时任务,BeanName=" + scheduleJob.getBeanName());
L 38             return;
39         }
40
41         //设置当前租户
42         TenantContextHolder.setTenantId(scheduleJob.getTenantId());
43         Long tenantId = TenantContextHolder.getRequiredTenantId();
44
45         //数据库保存执行记录
46         ScheduleJobLogEntity log = new ScheduleJobLogEntity();
47         log.setId(System.currentTimeMillis());
48         log.setJobId(scheduleJob.getId());
49         log.setBeanName(scheduleJob.getBeanName());
50         log.setParams(scheduleJob.getParams());
51         log.setCreateDate(new Date());
52         log.setTenantId(tenantId);
53
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
83             ScheduleJobLogService scheduleJobLogService = SpringContextUtils.getBean(ScheduleJobLogService.class);
84             //scheduleJobLogService.insert(log);
85         }
86     }
87 }