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