mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
新增: CronUtil.isValidExpression方法,验证传入的Cron表达式是否合法;完善PatternParser.parseGroupPattern逻辑,加入对传入表达式为null和空的校验。
This commit is contained in:
parent
1564c55f7d
commit
be41fc2031
@ -20,6 +20,7 @@ package org.dromara.hutool.cron;
|
|||||||
import org.dromara.hutool.core.exception.HutoolException;
|
import org.dromara.hutool.core.exception.HutoolException;
|
||||||
import org.dromara.hutool.core.io.resource.NoResourceException;
|
import org.dromara.hutool.core.io.resource.NoResourceException;
|
||||||
import org.dromara.hutool.cron.pattern.CronPattern;
|
import org.dromara.hutool.cron.pattern.CronPattern;
|
||||||
|
import org.dromara.hutool.cron.pattern.parser.PatternParser;
|
||||||
import org.dromara.hutool.cron.task.Task;
|
import org.dromara.hutool.cron.task.Task;
|
||||||
import org.dromara.hutool.setting.Setting;
|
import org.dromara.hutool.setting.Setting;
|
||||||
import org.dromara.hutool.setting.SettingException;
|
import org.dromara.hutool.setting.SettingException;
|
||||||
@ -211,4 +212,20 @@ public class CronUtil {
|
|||||||
scheduler.stop(true);
|
scheduler.stop(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证是否为合法的Cron表达式
|
||||||
|
*/
|
||||||
|
public static boolean isValidExpression(String expression) {
|
||||||
|
if (expression == null) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
PatternParser.parse(expression);
|
||||||
|
return true;
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,7 @@ public class PatternParser {
|
|||||||
* @return {@link List}
|
* @return {@link List}
|
||||||
*/
|
*/
|
||||||
private static List<PatternMatcher> parseGroupPattern(final String groupPattern) {
|
private static List<PatternMatcher> parseGroupPattern(final String groupPattern) {
|
||||||
|
Assert.notBlank(groupPattern, "Cron expression must not be empty!");
|
||||||
final List<String> patternList = SplitUtil.splitTrim(groupPattern, "|");
|
final List<String> patternList = SplitUtil.splitTrim(groupPattern, "|");
|
||||||
final List<PatternMatcher> patternMatchers = new ArrayList<>(patternList.size());
|
final List<PatternMatcher> patternMatchers = new ArrayList<>(patternList.size());
|
||||||
for (final String pattern : patternList) {
|
for (final String pattern : patternList) {
|
||||||
|
@ -96,4 +96,15 @@ public class CronTest {
|
|||||||
CronUtil.setMatchSecond(true);
|
CronUtil.setMatchSecond(true);
|
||||||
CronUtil.start();
|
CronUtil.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
public void isValidExpressionTest() {
|
||||||
|
Console.log(CronUtil.isValidExpression("5 * * * *"));
|
||||||
|
Console.log(CronUtil.isValidExpression("3-18 5 * * * *"));
|
||||||
|
Console.log(CronUtil.isValidExpression(""));
|
||||||
|
Console.log(CronUtil.isValidExpression(null));
|
||||||
|
Console.log(CronUtil.isValidExpression("A B C D E F"));
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user