mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-01 02:38:43 +08:00 
			
		
		
		
	1. 优化 JobHandlerInvoker 的实现,增加重试机制
2. 去除作业异常,自动暂时作业的机制。
This commit is contained in:
		| @@ -1,56 +0,0 @@ | ||||
| package com.ruoyi.common.constant; | ||||
|  | ||||
| /** | ||||
|  * 任务调度通用常量 | ||||
|  * | ||||
|  * @author ruoyi | ||||
|  */ | ||||
| public class ScheduleConstants { | ||||
|     public static final String TASK_CLASS_NAME = "TASK_CLASS_NAME"; | ||||
|  | ||||
|     /** | ||||
|      * 执行目标key | ||||
|      */ | ||||
|     public static final String TASK_PROPERTIES = "TASK_PROPERTIES"; | ||||
|  | ||||
|     /** | ||||
|      * 默认 | ||||
|      */ | ||||
|     public static final String MISFIRE_DEFAULT = "0"; | ||||
|  | ||||
|     /** | ||||
|      * 立即触发执行 | ||||
|      */ | ||||
|     public static final String MISFIRE_IGNORE_MISFIRES = "1"; | ||||
|  | ||||
|     /** | ||||
|      * 触发一次执行 | ||||
|      */ | ||||
|     public static final String MISFIRE_FIRE_AND_PROCEED = "2"; | ||||
|  | ||||
|     /** | ||||
|      * 不触发立即执行 | ||||
|      */ | ||||
|     public static final String MISFIRE_DO_NOTHING = "3"; | ||||
|  | ||||
|     public enum Status { | ||||
|         /** | ||||
|          * 正常 | ||||
|          */ | ||||
|         NORMAL("0"), | ||||
|         /** | ||||
|          * 暂停 | ||||
|          */ | ||||
|         PAUSE("1"); | ||||
|  | ||||
|         private String value; | ||||
|  | ||||
|         private Status(String value) { | ||||
|             this.value = value; | ||||
|         } | ||||
|  | ||||
|         public String getValue() { | ||||
|             return value; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,54 +0,0 @@ | ||||
| package com.ruoyi.quartz.service.impl; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.stereotype.Service; | ||||
| import com.ruoyi.quartz.domain.SysJobLog; | ||||
| import com.ruoyi.quartz.mapper.SysJobLogMapper; | ||||
| import com.ruoyi.quartz.service.ISysJobLogService; | ||||
|  | ||||
| /** | ||||
|  * 定时任务调度日志信息 服务层 | ||||
|  * | ||||
|  * @author ruoyi | ||||
|  */ | ||||
| @Service | ||||
| public class SysJobLogServiceImpl implements ISysJobLogService { | ||||
|  | ||||
|     @Autowired | ||||
|     private SysJobLogMapper jobLogMapper; | ||||
|  | ||||
|     /** | ||||
|      * 获取quartz调度器日志的计划任务 | ||||
|      * | ||||
|      * @param jobLog 调度日志信息 | ||||
|      * @return 调度任务日志集合 | ||||
|      */ | ||||
|     @Override | ||||
|     public List<SysJobLog> selectJobLogList(SysJobLog jobLog) { | ||||
|         return jobLogMapper.selectJobLogList(jobLog); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 通过调度任务日志ID查询调度信息 | ||||
|      * | ||||
|      * @param jobLogId 调度任务日志ID | ||||
|      * @return 调度任务日志对象信息 | ||||
|      */ | ||||
|     @Override | ||||
|     public SysJobLog selectJobLogById(Long jobLogId) { | ||||
|         return jobLogMapper.selectJobLogById(jobLogId); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 新增任务日志 | ||||
|      * | ||||
|      * @param jobLog 调度日志信息 | ||||
|      */ | ||||
|     @Override | ||||
|     public void addJobLog(SysJobLog jobLog) { | ||||
|         jobLogMapper.insertJobLog(jobLog); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,97 +0,0 @@ | ||||
| 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 sysJob  系统计划任务 | ||||
|      */ | ||||
|     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; | ||||
| } | ||||
| @@ -1,44 +0,0 @@ | ||||
| package com.ruoyi.quartz.util; | ||||
|  | ||||
| import org.quartz.CronScheduleBuilder; | ||||
| import org.quartz.CronTrigger; | ||||
| import org.quartz.Job; | ||||
| import org.quartz.JobBuilder; | ||||
| import org.quartz.JobDetail; | ||||
| import org.quartz.JobKey; | ||||
| import org.quartz.Scheduler; | ||||
| import org.quartz.SchedulerException; | ||||
| import org.quartz.TriggerBuilder; | ||||
| import org.quartz.TriggerKey; | ||||
| import com.ruoyi.common.constant.ScheduleConstants; | ||||
| import com.ruoyi.common.exception.job.TaskException; | ||||
| import com.ruoyi.common.exception.job.TaskException.Code; | ||||
| import com.ruoyi.quartz.domain.SysJob; | ||||
|  | ||||
| /** | ||||
|  * 定时任务工具类 | ||||
|  * | ||||
|  * @author ruoyi | ||||
|  */ | ||||
| public class ScheduleUtils { | ||||
|  | ||||
|     /** | ||||
|      * 设置定时任务策略 | ||||
|      */ | ||||
|     public static CronScheduleBuilder handleCronScheduleMisfirePolicy(SysJob job, CronScheduleBuilder cb) | ||||
|             throws TaskException { | ||||
|         switch (job.getMisfirePolicy()) { | ||||
|             case ScheduleConstants.MISFIRE_DEFAULT: | ||||
|                 return cb; | ||||
|             case ScheduleConstants.MISFIRE_IGNORE_MISFIRES: | ||||
|                 return cb.withMisfireHandlingInstructionIgnoreMisfires(); | ||||
|             case ScheduleConstants.MISFIRE_FIRE_AND_PROCEED: | ||||
|                 return cb.withMisfireHandlingInstructionFireAndProceed(); | ||||
|             case ScheduleConstants.MISFIRE_DO_NOTHING: | ||||
|                 return cb.withMisfireHandlingInstructionDoNothing(); | ||||
|             default: | ||||
|                 throw new TaskException("The task misfire policy '" + job.getMisfirePolicy() | ||||
|                         + "' cannot be used in cron schedule tasks", Code.CONFIG_ERROR); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV