diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysJobController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysJobController.java index 14e676ef..1fbf851e 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysJobController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysJobController.java @@ -149,4 +149,14 @@ public class SysJobController extends BaseController job.setUpdateBy(ShiroUtils.getLoginName()); return toAjax(jobService.updateJobCron(job)); } + + /** + * 校验cron表达式是否有效 + */ + @PostMapping("/checkCronExpressionIsValid") + @ResponseBody + public boolean checkCronExpressionIsValid(SysJob job) + { + return jobService.checkCronExpressionIsValid(job.getCronExpression()); + } } diff --git a/ruoyi-admin/src/main/resources/templates/monitor/job/add.html b/ruoyi-admin/src/main/resources/templates/monitor/job/add.html index 8e860b95..cabcf566 100644 --- a/ruoyi-admin/src/main/resources/templates/monitor/job/add.html +++ b/ruoyi-admin/src/main/resources/templates/monitor/job/add.html @@ -74,11 +74,26 @@ }, cronExpression:{ required:true, + remote: { + url: prefix + "/checkCronExpressionIsValid", + type: "post", + dataType: "json", + data: { + "cronExpression": function() { + return $.common.trim($("#cronExpression").val()); + } + }, + dataFilter: function(data, type) { + return data; + } + } }, }, - submitHandler:function(form){ - $.operate.save(prefix + "/add", $('#form-job-add').serialize()); - } + messages: { + "cronExpression": { + remote: "表达式不正确" + } + } }); function submitHandler() { diff --git a/ruoyi-admin/src/main/resources/templates/monitor/job/edit.html b/ruoyi-admin/src/main/resources/templates/monitor/job/edit.html index e2e7d52e..320370d3 100644 --- a/ruoyi-admin/src/main/resources/templates/monitor/job/edit.html +++ b/ruoyi-admin/src/main/resources/templates/monitor/job/edit.html @@ -75,8 +75,26 @@ }, cronExpression:{ required:true, + remote: { + url: prefix + "/checkCronExpressionIsValid", + type: "post", + dataType: "json", + data: { + "cronExpression": function() { + return $.common.trim($("#cronExpression").val()); + } + }, + dataFilter: function(data, type) { + return data; + } + } }, - } + }, + messages: { + "cronExpression": { + remote: "表达式不正确" + } + } }); function submitHandler() { 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 a02690df..0cf3a000 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 @@ -89,4 +89,12 @@ public interface ISysJobService * @return 结果 */ public int updateJobCron(SysJob job); + + /** + * 校验cron表达式是否有效 + * + * @param cronExpression 表达式 + * @return 结果 + */ + public boolean checkCronExpressionIsValid(String cronExpression); } 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 6a4531d2..9f7f1fcb 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 @@ -11,6 +11,7 @@ import com.ruoyi.common.support.Convert; import com.ruoyi.quartz.domain.SysJob; import com.ruoyi.quartz.mapper.SysJobMapper; import com.ruoyi.quartz.service.ISysJobService; +import com.ruoyi.quartz.util.CronUtils; import com.ruoyi.quartz.util.ScheduleUtils; /** @@ -204,4 +205,15 @@ public class SysJobServiceImpl implements ISysJobService } return rows; } + + /** + * 校验cron表达式是否有效 + * + * @param cronExpression 表达式 + * @return 结果 + */ + public boolean checkCronExpressionIsValid(String cronExpression) + { + return CronUtils.isValid(cronExpression); + } } diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/CronUtils.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/CronUtils.java new file mode 100644 index 00000000..dd538397 --- /dev/null +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/CronUtils.java @@ -0,0 +1,63 @@ +package com.ruoyi.quartz.util; + +import java.text.ParseException; +import java.util.Date; +import org.quartz.CronExpression; + +/** + * cron表达式工具类 + * + * @author ruoyi + * + */ +public class CronUtils +{ + /** + * 返回一个布尔值代表一个给定的Cron表达式的有效性 + * + * @param cronExpression Cron表达式 + * @return boolean 表达式是否有效 + */ + public static boolean isValid(String cronExpression) + { + return CronExpression.isValidExpression(cronExpression); + } + + /** + * 返回一个字符串值,表示该消息无效Cron表达式给出有效性 + * + * @param cronExpression Cron表达式 + * @return String 无效时返回表达式错误描述,如果有效返回null + */ + public static String getInvalidMessage(String cronExpression) + { + try + { + new CronExpression(cronExpression); + return null; + } + catch (ParseException pe) + { + return pe.getMessage(); + } + } + + /** + * 返回下一个执行时间根据给定的Cron表达式 + * + * @param cronExpression Cron表达式 + * @return Date 下次Cron表达式执行时间 + */ + public static Date getNextExecution(String cronExpression) + { + try + { + CronExpression cron = new CronExpression(cronExpression); + return cron.getNextValidTimeAfter(new Date(System.currentTimeMillis())); + } + catch (ParseException e) + { + throw new IllegalArgumentException(e.getMessage()); + } + } +}