Commit a44aa5db by RuoYi

校验cron表达式是否有效

parent 6ee8053a
......@@ -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());
}
}
......@@ -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() {
......
......@@ -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() {
......
......@@ -89,4 +89,12 @@ public interface ISysJobService
* @return 结果
*/
public int updateJobCron(SysJob job);
/**
* 校验cron表达式是否有效
*
* @param cronExpression 表达式
* @return 结果
*/
public boolean checkCronExpressionIsValid(String cronExpression);
}
......@@ -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);
}
}
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());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment