From 7177ed7a993067672da1ad95dbadda9210a6ca10 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Fri, 20 Jul 2018 17:21:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=89=A7=E8=A1=8C=E7=AD=96?= =?UTF-8?q?=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/constant/ScheduleConstants.java | 20 +++++-- .../common/exception/job/TaskException.java | 35 +++++++++++++ .../ruoyi/project/monitor/job/domain/Job.java | 15 ++++++ .../project/monitor/job/util/ScheduleJob.java | 2 +- .../monitor/job/util/ScheduleUtils.java | 52 ++++++++++++++----- .../resources/mybatis/monitor/JobMapper.xml | 6 ++- .../resources/templates/monitor/job/add.html | 12 ++++- .../resources/templates/monitor/job/edit.html | 12 ++++- .../resources/templates/system/user/user.html | 3 +- .../resources/templates/tool/gen/gen.html | 2 +- 10 files changed, 137 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/ruoyi/common/exception/job/TaskException.java diff --git a/src/main/java/com/ruoyi/common/constant/ScheduleConstants.java b/src/main/java/com/ruoyi/common/constant/ScheduleConstants.java index 7f8692b0..6b774ac8 100644 --- a/src/main/java/com/ruoyi/common/constant/ScheduleConstants.java +++ b/src/main/java/com/ruoyi/common/constant/ScheduleConstants.java @@ -7,10 +7,22 @@ package com.ruoyi.common.constant; */ public interface ScheduleConstants { - /** - * 任务调度参数key - */ - public static final String JOB_PARAM_KEY = "JOB_PARAM_KEY"; + + public static final String TASK_CLASS_NAME = "__TASK_CLASS_NAME__"; + + 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 { diff --git a/src/main/java/com/ruoyi/common/exception/job/TaskException.java b/src/main/java/com/ruoyi/common/exception/job/TaskException.java new file mode 100644 index 00000000..1fa75ddf --- /dev/null +++ b/src/main/java/com/ruoyi/common/exception/job/TaskException.java @@ -0,0 +1,35 @@ +package com.ruoyi.common.exception.job; + +/** + * 计划策略异常 + * + * @author ruoyi + */ +public class TaskException extends Exception +{ + private static final long serialVersionUID = 1L; + + private Code code; + + public TaskException(String msg, Code code) + { + this(msg, code, null); + } + + public TaskException(String msg, Code code, Exception nestedEx) + { + super(msg, nestedEx); + this.code = code; + } + + public Code getCode() + { + return code; + } + + public enum Code + { + TASK_EXISTS, NO_TASK_EXISTS, TASK_ALREADY_STARTED, UNKNOWN, CONFIG_ERROR, TASK_NODE_NOT_AVAILABLE + } + +} \ No newline at end of file diff --git a/src/main/java/com/ruoyi/project/monitor/job/domain/Job.java b/src/main/java/com/ruoyi/project/monitor/job/domain/Job.java index e1a0ba8b..b97531cc 100644 --- a/src/main/java/com/ruoyi/project/monitor/job/domain/Job.java +++ b/src/main/java/com/ruoyi/project/monitor/job/domain/Job.java @@ -2,6 +2,7 @@ package com.ruoyi.project.monitor.job.domain; import java.io.Serializable; +import com.ruoyi.common.constant.ScheduleConstants; import com.ruoyi.framework.aspectj.lang.annotation.Excel; import com.ruoyi.framework.web.domain.BaseEntity; @@ -38,6 +39,10 @@ public class Job extends BaseEntity implements Serializable @Excel(name = "执行表达式 ") private String cronExpression; + /** cron计划策略 */ + @Excel(name = "计划策略 ") + private String misfirePolicy = ScheduleConstants.MISFIRE_DEFAULT; + /** 任务状态(0正常 1暂停) */ @Excel(name = "任务状态") private String status; @@ -102,6 +107,16 @@ public class Job extends BaseEntity implements Serializable this.cronExpression = cronExpression; } + public String getMisfirePolicy() + { + return misfirePolicy; + } + + public void setMisfirePolicy(String misfirePolicy) + { + this.misfirePolicy = misfirePolicy; + } + public String getStatus() { return status; diff --git a/src/main/java/com/ruoyi/project/monitor/job/util/ScheduleJob.java b/src/main/java/com/ruoyi/project/monitor/job/util/ScheduleJob.java index 0e974b92..4d1c0730 100644 --- a/src/main/java/com/ruoyi/project/monitor/job/util/ScheduleJob.java +++ b/src/main/java/com/ruoyi/project/monitor/job/util/ScheduleJob.java @@ -33,7 +33,7 @@ public class ScheduleJob extends QuartzJobBean protected void executeInternal(JobExecutionContext context) throws JobExecutionException { Job job = new Job(); - BeanUtils.copyBeanProp(job, context.getMergedJobDataMap().get(ScheduleConstants.JOB_PARAM_KEY)); + BeanUtils.copyBeanProp(job, context.getMergedJobDataMap().get(ScheduleConstants.TASK_PROPERTIES)); IJobLogService jobLogService = (IJobLogService) SpringUtils.getBean(IJobLogService.class); diff --git a/src/main/java/com/ruoyi/project/monitor/job/util/ScheduleUtils.java b/src/main/java/com/ruoyi/project/monitor/job/util/ScheduleUtils.java index fe8ce8fe..8f0c1384 100644 --- a/src/main/java/com/ruoyi/project/monitor/job/util/ScheduleUtils.java +++ b/src/main/java/com/ruoyi/project/monitor/job/util/ScheduleUtils.java @@ -13,6 +13,8 @@ import org.quartz.TriggerKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.ruoyi.common.constant.ScheduleConstants; +import com.ruoyi.common.exception.job.TaskException; +import com.ruoyi.common.exception.job.TaskException.Code; import com.ruoyi.project.monitor.job.domain.Job; /** @@ -25,14 +27,12 @@ public class ScheduleUtils { private static final Logger log = LoggerFactory.getLogger(ScheduleUtils.class); - private final static String JOB_NAME = "TASK_"; - /** * 获取触发器key */ public static TriggerKey getTriggerKey(Long jobId) { - return TriggerKey.triggerKey(JOB_NAME + jobId); + return TriggerKey.triggerKey(ScheduleConstants.TASK_CLASS_NAME + jobId); } /** @@ -40,7 +40,7 @@ public class ScheduleUtils */ public static JobKey getJobKey(Long jobId) { - return JobKey.jobKey(JOB_NAME + jobId); + return JobKey.jobKey(ScheduleConstants.TASK_CLASS_NAME + jobId); } /** @@ -70,14 +70,14 @@ public class ScheduleUtils JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(getJobKey(job.getJobId())).build(); // 表达式调度构建器 - CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); + CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); + cronScheduleBuilder = handleCronScheduleMisfirePolicy(job, cronScheduleBuilder); // 按新的cronExpression表达式构建一个新的trigger - CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(job.getJobId())) - .withSchedule(scheduleBuilder).build(); + CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(job.getJobId())).withSchedule(cronScheduleBuilder).build(); // 放入参数,运行时的方法可以获取 - jobDetail.getJobDataMap().put(ScheduleConstants.JOB_PARAM_KEY, job); + jobDetail.getJobDataMap().put(ScheduleConstants.TASK_PROPERTIES, job); scheduler.scheduleJob(jobDetail, trigger); @@ -91,6 +91,10 @@ public class ScheduleUtils { log.error(e.getMessage()); } + catch (TaskException e) + { + log.error(e.getMessage()); + } } /** @@ -103,15 +107,16 @@ public class ScheduleUtils TriggerKey triggerKey = getTriggerKey(job.getJobId()); // 表达式调度构建器 - CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); + CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); + cronScheduleBuilder = handleCronScheduleMisfirePolicy(job, cronScheduleBuilder); CronTrigger trigger = getCronTrigger(scheduler, job.getJobId()); // 按新的cronExpression表达式重新构建trigger - trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); + trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build(); // 参数 - trigger.getJobDataMap().put(ScheduleConstants.JOB_PARAM_KEY, job); + trigger.getJobDataMap().put(ScheduleConstants.TASK_PROPERTIES, job); scheduler.rescheduleJob(triggerKey, trigger); @@ -126,6 +131,10 @@ public class ScheduleUtils { log.error(e.getMessage()); } + catch (TaskException e) + { + log.error(e.getMessage()); + } } /** @@ -138,7 +147,7 @@ public class ScheduleUtils { // 参数 JobDataMap dataMap = new JobDataMap(); - dataMap.put(ScheduleConstants.JOB_PARAM_KEY, job); + dataMap.put(ScheduleConstants.TASK_PROPERTIES, job); scheduler.triggerJob(getJobKey(job.getJobId()), dataMap); rows = 1; @@ -194,4 +203,23 @@ public class ScheduleUtils log.error(e.getMessage()); } } + + public static CronScheduleBuilder handleCronScheduleMisfirePolicy(Job 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); + } + } } diff --git a/src/main/resources/mybatis/monitor/JobMapper.xml b/src/main/resources/mybatis/monitor/JobMapper.xml index ee598950..ea57f0fe 100644 --- a/src/main/resources/mybatis/monitor/JobMapper.xml +++ b/src/main/resources/mybatis/monitor/JobMapper.xml @@ -11,6 +11,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + @@ -20,7 +21,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - select job_id, job_name, job_group, method_name, method_params, cron_expression, status, create_by, create_time, remark from sys_job + select job_id, job_name, job_group, method_name, method_params, cron_expression, misfire_policy, status, create_by, create_time, remark from sys_job +
@@ -35,6 +36,14 @@
+
+ +
+ + + +
+
@@ -59,6 +68,7 @@
+