校验cron表达式是否有效

master
RuoYi 6 years ago committed by Limy
parent 2b05623397
commit 756bdcaa19

@ -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);
}
}

@ -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());
}
}
}
Loading…
Cancel
Save