ÎļþÃû´Ó ruoyi/src/main/java/com/ruoyi/common/utils/job/AbstractQuartzJob.java ÐÞ¸Ä |
| | |
| | | package com.ruoyi.common.utils.job; |
| | | |
| | | import java.util.Date; |
| | | 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.project.monitor.domain.SysJob; |
| | | import com.ruoyi.project.monitor.domain.SysJobLog; |
| | | import com.ruoyi.project.monitor.service.ISysJobLogService; |
| | | |
| | | /** |
| | | * æ½è±¡quartzè°ç¨ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public abstract class AbstractQuartzJob implements Job |
| | | { |
| | | private static final Logger log = LoggerFactory.getLogger(AbstractQuartzJob.class); |
| | | |
| | | /** |
| | | * çº¿ç¨æ¬å°åé |
| | | */ |
| | | private static ThreadLocal<Date> threadLocal = new ThreadLocal<>(); |
| | | |
| | | @Override |
| | | public void execute(JobExecutionContext context) throws JobExecutionException |
| | | { |
| | | SysJob sysJob = new SysJob(); |
| | | BeanUtils.copyBeanProp(sysJob, context.getMergedJobDataMap().get(ScheduleConstants.TASK_PROPERTIES)); |
| | | try |
| | | { |
| | | before(context, sysJob); |
| | | if (sysJob != null) |
| | | { |
| | | doExecute(context, sysJob); |
| | | } |
| | | after(context, sysJob, null); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("任塿§è¡å¼å¸¸ - ï¼", e); |
| | | after(context, sysJob, e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ§è¡å |
| | | * |
| | | * @param context 工使§è¡ä¸ä¸æå¯¹è±¡ |
| | | * @param sysJob ç³»ç»è®¡åä»»å¡ |
| | | */ |
| | | protected void before(JobExecutionContext context, SysJob sysJob) |
| | | { |
| | | threadLocal.set(new Date()); |
| | | } |
| | | |
| | | /** |
| | | * æ§è¡å |
| | | * |
| | | * @param context 工使§è¡ä¸ä¸æå¯¹è±¡ |
| | | * @param sysScheduleJob ç³»ç»è®¡åä»»å¡ |
| | | */ |
| | | protected void after(JobExecutionContext context, SysJob sysJob, Exception e) |
| | | { |
| | | 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 + "毫ç§"); |
| | | if (e != null) |
| | | { |
| | | sysJobLog.setStatus(Constants.FAIL); |
| | | String errorMsg = StringUtils.substring(ExceptionUtil.getExceptionMessage(e), 0, 2000); |
| | | sysJobLog.setExceptionInfo(errorMsg); |
| | | } |
| | | else |
| | | { |
| | | sysJobLog.setStatus(Constants.SUCCESS); |
| | | } |
| | | |
| | | // åå
¥æ°æ®åºå½ä¸ |
| | | SpringUtils.getBean(ISysJobLogService.class).addJobLog(sysJobLog); |
| | | } |
| | | |
| | | /** |
| | | * æ§è¡æ¹æ³ï¼ç±åç±»éè½½ |
| | | * |
| | | * @param context 工使§è¡ä¸ä¸æå¯¹è±¡ |
| | | * @param sysJob ç³»ç»è®¡åä»»å¡ |
| | | * @throws Exception æ§è¡è¿ç¨ä¸çå¼å¸¸ |
| | | */ |
| | | protected abstract void doExecute(JobExecutionContext context, SysJob sysJob) throws Exception; |
| | | } |
| | | package com.ruoyi.quartz.util;
|
| | |
|
| | | import java.util.Date;
|
| | | 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
|
| | | */
|
| | | public abstract class AbstractQuartzJob implements Job
|
| | | {
|
| | | private static final Logger log = LoggerFactory.getLogger(AbstractQuartzJob.class);
|
| | |
|
| | | /**
|
| | | * çº¿ç¨æ¬å°åé
|
| | | */
|
| | | private static ThreadLocal<Date> threadLocal = new ThreadLocal<>();
|
| | |
|
| | | @Override
|
| | | public void execute(JobExecutionContext context) throws JobExecutionException
|
| | | {
|
| | | SysJob sysJob = new SysJob();
|
| | | BeanUtils.copyBeanProp(sysJob, context.getMergedJobDataMap().get(ScheduleConstants.TASK_PROPERTIES));
|
| | | try
|
| | | {
|
| | | before(context, sysJob);
|
| | | if (sysJob != null)
|
| | | {
|
| | | doExecute(context, sysJob);
|
| | | }
|
| | | after(context, sysJob, null);
|
| | | }
|
| | | catch (Exception e)
|
| | | {
|
| | | log.error("任塿§è¡å¼å¸¸ - ï¼", e);
|
| | | after(context, sysJob, e);
|
| | | }
|
| | | }
|
| | |
|
| | | /**
|
| | | * æ§è¡å
|
| | | *
|
| | | * @param context 工使§è¡ä¸ä¸æå¯¹è±¡
|
| | | * @param sysJob ç³»ç»è®¡åä»»å¡
|
| | | */
|
| | | protected void before(JobExecutionContext context, SysJob sysJob)
|
| | | {
|
| | | threadLocal.set(new Date());
|
| | | }
|
| | |
|
| | | /**
|
| | | * æ§è¡å
|
| | | *
|
| | | * @param context 工使§è¡ä¸ä¸æå¯¹è±¡
|
| | | * @param sysScheduleJob ç³»ç»è®¡åä»»å¡
|
| | | */
|
| | | protected void after(JobExecutionContext context, SysJob sysJob, Exception e)
|
| | | {
|
| | | 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 + "毫ç§");
|
| | | if (e != null)
|
| | | {
|
| | | sysJobLog.setStatus(Constants.FAIL);
|
| | | String errorMsg = StringUtils.substring(ExceptionUtil.getExceptionMessage(e), 0, 2000);
|
| | | sysJobLog.setExceptionInfo(errorMsg);
|
| | | }
|
| | | else
|
| | | {
|
| | | sysJobLog.setStatus(Constants.SUCCESS);
|
| | | }
|
| | |
|
| | | // åå
¥æ°æ®åºå½ä¸
|
| | | SpringUtils.getBean(ISysJobLogService.class).addJobLog(sysJobLog);
|
| | | }
|
| | |
|
| | | /**
|
| | | * æ§è¡æ¹æ³ï¼ç±åç±»éè½½
|
| | | *
|
| | | * @param context 工使§è¡ä¸ä¸æå¯¹è±¡
|
| | | * @param sysJob ç³»ç»è®¡åä»»å¡
|
| | | * @throws Exception æ§è¡è¿ç¨ä¸çå¼å¸¸
|
| | | */
|
| | | protected abstract void doExecute(JobExecutionContext context, SysJob sysJob) throws Exception;
|
| | | }
|