From 250c5ba226c63afb3f884d75f4be93778453a7b9 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Fri, 29 Jul 2022 20:28:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=BB=E5=8A=A1=E8=BF=87?= =?UTF-8?q?=E6=9C=9F=E4=B8=8D=E6=89=A7=E8=A1=8C=E8=B0=83=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/quartz/controller/SysJobController.java | 6 +++--- .../java/com/ruoyi/quartz/service/ISysJobService.java | 2 +- .../ruoyi/quartz/service/impl/SysJobServiceImpl.java | 11 +++++++++-- .../java/com/ruoyi/quartz/util/ScheduleUtils.java | 7 ++++++- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/controller/SysJobController.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/controller/SysJobController.java index 7040003..3cd7ced 100644 --- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/controller/SysJobController.java +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/controller/SysJobController.java @@ -167,8 +167,8 @@ public class SysJobController extends BaseController @PutMapping("/run") public AjaxResult run(@RequestBody SysJob job) throws SchedulerException { - jobService.run(job); - return AjaxResult.success(); + boolean result = jobService.run(job); + return result ? success() : error("任务不存在或已过期!"); } /** @@ -180,6 +180,6 @@ public class SysJobController extends BaseController public AjaxResult remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException { jobService.deleteJobByIds(jobIds); - return AjaxResult.success(); + return success(); } } diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/ISysJobService.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/ISysJobService.java index 53f0989..afdb707 100644 --- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/ISysJobService.java +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/ISysJobService.java @@ -74,7 +74,7 @@ public interface ISysJobService * @param job 调度信息 * @return 结果 */ - public void run(SysJob job) throws SchedulerException; + public boolean run(SysJob job) throws SchedulerException; /** * 新增任务 diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SysJobServiceImpl.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SysJobServiceImpl.java index 57b571c..71540b3 100644 --- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SysJobServiceImpl.java +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SysJobServiceImpl.java @@ -174,15 +174,22 @@ public class SysJobServiceImpl implements ISysJobService */ @Override @Transactional(rollbackFor = Exception.class) - public void run(SysJob job) throws SchedulerException + public boolean run(SysJob job) throws SchedulerException { + boolean result = false; Long jobId = job.getJobId(); String jobGroup = job.getJobGroup(); SysJob properties = selectJobById(job.getJobId()); // 参数 JobDataMap dataMap = new JobDataMap(); dataMap.put(ScheduleConstants.TASK_PROPERTIES, properties); - scheduler.triggerJob(ScheduleUtils.getJobKey(jobId, jobGroup), dataMap); + JobKey jobKey = ScheduleUtils.getJobKey(jobId, jobGroup); + if (scheduler.checkExists(jobKey)) + { + result = true; + scheduler.triggerJob(jobKey, dataMap); + } + return result; } /** diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/ScheduleUtils.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/ScheduleUtils.java index 9833d20..e6dea10 100644 --- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/ScheduleUtils.java +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/ScheduleUtils.java @@ -83,7 +83,12 @@ public class ScheduleUtils scheduler.deleteJob(getJobKey(jobId, jobGroup)); } - scheduler.scheduleJob(jobDetail, trigger); + // 判断任务是否过期 + if (StringUtils.isNotNull(CronUtils.getNextExecution(job.getCronExpression()))) + { + // 执行调度任务 + scheduler.scheduleJob(jobDetail, trigger); + } // 暂停任务 if (job.getStatus().equals(ScheduleConstants.Status.PAUSE.getValue()))