ShiShiYiBan/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/AbstractQuartzJob.java

98 lines
3.3 KiB
Java
Raw Normal View History

2020-07-19 10:25:40 +08:00
package com.ruoyi.quartz.util;
import java.util.Date;
2021-02-13 21:13:09 +08:00
2020-07-19 10:25:40 +08:00
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.ScheduleConstants;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.domain.SysJobLog;
import com.ruoyi.quartz.service.ISysJobLogService;
/**
* 抽象quartz调用
*
* @author ruoyi
*/
2021-02-13 21:13:09 +08:00
public abstract class AbstractQuartzJob implements Job {
2020-07-19 10:25:40 +08:00
private static final Logger log = LoggerFactory.getLogger(AbstractQuartzJob.class);
/**
* 线程本地变量
*/
private static ThreadLocal<Date> threadLocal = new ThreadLocal<>();
@Override
2021-02-13 21:13:09 +08:00
public void execute(JobExecutionContext context) throws JobExecutionException {
2020-07-19 10:25:40 +08:00
SysJob sysJob = new SysJob();
BeanUtils.copyBeanProp(sysJob, context.getMergedJobDataMap().get(ScheduleConstants.TASK_PROPERTIES));
2021-02-13 21:13:09 +08:00
try {
2020-07-19 10:25:40 +08:00
before(context, sysJob);
2021-02-13 21:13:09 +08:00
if (sysJob != null) {
2020-07-19 10:25:40 +08:00
doExecute(context, sysJob);
}
after(context, sysJob, null);
2021-02-13 21:13:09 +08:00
} catch (Exception e) {
2020-07-19 10:25:40 +08:00
log.error("任务执行异常 - ", e);
after(context, sysJob, e);
}
}
/**
* 执行前
*
* @param context 工作执行上下文对象
2021-02-13 21:13:09 +08:00
* @param sysJob 系统计划任务
2020-07-19 10:25:40 +08:00
*/
2021-02-13 21:13:09 +08:00
protected void before(JobExecutionContext context, SysJob sysJob) {
2020-07-19 10:25:40 +08:00
threadLocal.set(new Date());
}
/**
* 执行后
*
* @param context 工作执行上下文对象
2021-02-13 21:13:09 +08:00
* @param sysJob 系统计划任务
2020-07-19 10:25:40 +08:00
*/
2021-02-13 21:13:09 +08:00
protected void after(JobExecutionContext context, SysJob sysJob, Exception e) {
2020-07-19 10:25:40 +08:00
Date startTime = threadLocal.get();
threadLocal.remove();
final SysJobLog sysJobLog = new SysJobLog();
sysJobLog.setJobName(sysJob.getJobName());
sysJobLog.setJobGroup(sysJob.getJobGroup());
sysJobLog.setInvokeTarget(sysJob.getInvokeTarget());
sysJobLog.setStartTime(startTime);
sysJobLog.setStopTime(new Date());
long runMs = sysJobLog.getStopTime().getTime() - sysJobLog.getStartTime().getTime();
sysJobLog.setJobMessage(sysJobLog.getJobName() + " 总共耗时:" + runMs + "毫秒");
2021-02-13 21:13:09 +08:00
if (e != null) {
2020-07-19 10:25:40 +08:00
sysJobLog.setStatus(Constants.FAIL);
String errorMsg = StringUtils.substring(ExceptionUtil.getExceptionMessage(e), 0, 2000);
sysJobLog.setExceptionInfo(errorMsg);
2021-02-13 21:13:09 +08:00
} else {
2020-07-19 10:25:40 +08:00
sysJobLog.setStatus(Constants.SUCCESS);
}
// 写入数据库当中
SpringUtils.getBean(ISysJobLogService.class).addJobLog(sysJobLog);
}
/**
* 执行方法由子类重载
*
* @param context 工作执行上下文对象
2021-02-13 21:13:09 +08:00
* @param sysJob 系统计划任务
2020-07-19 10:25:40 +08:00
* @throws Exception 执行过程中的异常
*/
protected abstract void doExecute(JobExecutionContext context, SysJob sysJob) throws Exception;
}