验证 cron 表达式在 Java 中是否有效

2022-09-01 03:34:13

我正在使用Quartz用Java编写一个调度应用程序。我使用的是 CronTrigger,但我的 cron 表达式在调度之前被输入到数据库中,并且基于用户输入。

有没有办法在捕获 cron 表达式时验证它们是否有效?我宁愿这样做并给用户一个适当的错误消息,而不是等到调度程序运行,当我尝试创建触发器时,我得到一个ParseException。这可能是用户输入数据后的几天。


答案 1

难道你不能简单地创建一个触发器而不实际执行它吗?您可以简单地在ParseException的情况下提供适当的反馈。如果表达式正常,请将表达式保存到 DB。

编辑:或简单地这样做:

org.quartz.CronExpression.isValidExpression(expression);

答案 2

我已经修改了@ph4r05添加的以下代码来生成正则表达式;这是正则表达式:

^\s*($|#|\w+\s*=|(\?|\*|(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?(?:,(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?)*)\s+(\?|\*|(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?(?:,(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?)*)\s+(\?|\*|(?:[01]?\d|2[0-3])(?:(?:-|\/|\,)(?:[01]?\d|2[0-3]))?(?:,(?:[01]?\d|2[0-3])(?:(?:-|\/|\,)(?:[01]?\d|2[0-3]))?)*)\s+(\?|\*|(?:0?[1-9]|[12]\d|3[01])(?:(?:-|\/|\,)(?:0?[1-9]|[12]\d|3[01]))?(?:,(?:0?[1-9]|[12]\d|3[01])(?:(?:-|\/|\,)(?:0?[1-9]|[12]\d|3[01]))?)*)\s+(\?|\*|(?:[1-9]|1[012])(?:(?:-|\/|\,)(?:[1-9]|1[012]))?(?:L|W)?(?:,(?:[1-9]|1[012])(?:(?:-|\/|\,)(?:[1-9]|1[012]))?(?:L|W)?)*|\?|\*|(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?(?:,(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?)*)\s+(\?|\*|(?:[0-6])(?:(?:-|\/|\,|#)(?:[0-6]))?(?:L)?(?:,(?:[0-6])(?:(?:-|\/|\,|#)(?:[0-6]))?(?:L)?)*|\?|\*|(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?(?:,(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?)*)(|\s)+(\?|\*|(?:|\d{4})(?:(?:-|\/|\,)(?:|\d{4}))?(?:,(?:|\d{4})(?:(?:-|\/|\,)(?:|\d{4}))?)*))$

这是java代码:

private static String cronRegex = null;

public static String getCronRegex()
{
  if (cronRegex == null)
  {
    // numbers intervals and regex
    Map<String, String> numbers = new HashMap<String, String>();
    numbers.put("sec", "[0-5]?\\d");
    numbers.put("min", "[0-5]?\\d");
    numbers.put("hour", "[01]?\\d|2[0-3]");
    numbers.put("day", "0?[1-9]|[12]\\d|3[01]");
    numbers.put("month", "[1-9]|1[012]");
    numbers.put("dow", "[0-6]");
    numbers.put("year", "|\\d{4}");

    Map<String, String> field_re = new HashMap<String, String>();

    // expand regex to contain different time specifiers
    for (String field : numbers.keySet())
    {
      String number = numbers.get(field);
      String range = "(?:" + number + ")(?:(?:-|\\/|\\," +  ("dow".equals(field)? "|#" :    "") + 

        ")(?:" + number + "))?" +  ("dow".equals(field)? "(?:L)?" : ("month".equals(field)? "(?:L|W)?" : ""));
      field_re.put(field, "\\?|\\*|" + range + "(?:," + range + ")*");
    }

    // add string specifiers
    String monthRE = field_re.get("month");
    String monthREVal =   "JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC";
    String monthRERange = "(?:" + monthREVal + ")(?:(?:-)(?:" + monthREVal + "))?" ; 
    monthRE = monthRE +  "|\\?|\\*|" + monthRERange + "(?:," + monthRERange + ")*" ;
    field_re.put("month", monthRE);

    String dowRE = field_re.get("dow");
    String dowREVal = "MON|TUE|WED|THU|FRI|SAT|SUN";
    String dowRERange = "(?:" + dowREVal + ")(?:(?:-)(?:" + dowREVal + "))?" ;

    dowRE = dowRE + "|\\?|\\*|" + dowRERange + "(?:," + dowRERange + ")*" ;
    field_re.put("dow", dowRE);

    StringBuilder fieldsReSB = new StringBuilder();
    fieldsReSB.append("^\\s*(").append("$") //
      .append("|#") //
      .append("|\\w+\\s*=") //
      .append("|") //
      .append("(")//
      .append(field_re.get("sec")).append(")\\s+(")//
      .append(field_re.get("min")).append(")\\s+(")//
      .append(field_re.get("hour")).append(")\\s+(")//
      .append(field_re.get("day")).append(")\\s+(")//
      .append(field_re.get("month")).append(")\\s+(")//
      .append(field_re.get("dow")).append(")(|\\s)+(")//
      .append(field_re.get("year"))//
      .append(")")//
      .append(")")//
      .append("$");

    cronRegex = fieldsReSB.toString();
  }
  return cronRegex;
}

我绝不是正则表达式专家,但至少这似乎适用于石英文档给出的所有示例。


推荐