简体   繁体   English

Java Quartz CronExpression验证错误的cron表达式

[英]Java Quartz CronExpression validates wrong cron expression

I am using quartz-scheduler to schedule my jobs. 我正在使用quartz-scheduler来安排我的工作。 I have a cron expression which is wrong: 我有一个错误的cron表达式:

1 15 10 ? * *ssddddfd

When I do: 当我做:

String cronExpression = "1 15 10 ? * *ssdddfd";
boolean checkCronExpression = CronExpression.isValidExpression(cronExpression);

checkCronExpression is true . checkCronExpressiontrue However when I look at the expression it is obviously wrong. 但是,当我看到表达时,这显然是错误的。 Has anyone else also faced similar issue? 有没有其他人也遇到过类似的问题?

Note: The package is: package org.quartz; 注意:包是:package org.quartz;

Version of quartz is: 2.2.3 石英版本是:2.2.3

Please check the CronExpression class from http://www.javadoc.io/doc/org.quartz-scheduler/quartz/2.2.3 请从http://www.javadoc.io/doc/org.quartz-scheduler/quartz/2.2.3查看CronExpression类

I also tried validateExpression method of CronExpression class with no success. 我也尝试过CronExpression类的validateExpression方法CronExpression没有成功。

This has recently been filed as a bug: 这最近已被提交为一个错误:

https://github.com/quartz-scheduler/quartz/issues/254 https://github.com/quartz-scheduler/quartz/issues/254

And you're right this is probably unexpected. 你是对的,这可能是意料之外的。

However being very captious one could argue that the JavaDoc of isValidCronExpression states 然而,非常挑剔的人可能会争辩说isValidCronExpressionJavaDoc状态

Indicates whether the specified cron expression can be parsed into a valid cron expression 指示是否可以将指定的cron表达式解析为有效的cron表达式

And actually the expression you provided can be parsed into a valid CronExpression although the expression is not valid itself. 实际上,您提供的表达式可以解析为有效的CronExpression尽管表达式本身无效。 Basically the guarantee that you get from calling CronExpression.isValidCronExpression(expression) is, that you can safely call new CronExpression(expression) without fearing that a ParseException will be thrown. 基本上,您从调用CronExpression.isValidCronExpression(expression)获得的保证是,您可以安全地调用new CronExpression(expression)而不必担心会抛出ParseException However as java.text.ParseException is a checked exception, this is rather pointless. 但是,由于java.text.ParseException是一个经过检查的异常,因此这是毫无意义的。

In summary the current implementation of CronExpression seems to be a little bit too lenient. 总之, CronExpression的当前实现似乎有点过于宽松。

Maybe you want to give other libraries a try (just searched on google): 也许你想尝试其他库(只在谷歌搜索):

But if you want to use the expression with quartz later on your best option is to stay with the quartz validation as patterns that are valid for other libraries may not be valid for quartz and vice versa. 但是如果你想在以后使用石英表达式,最好的选择就是保持石英验证,因为对其他库有效的模式可能对石英无效,反之亦然。 The source code of quartz for example states that patterns with both „day of week“ and „day of month“ are currently not supported although this is supported by cron expressions in general. 例如,石英的源代码表明当前不支持具有“星期几”和“日期”的模式,尽管这通常由cron表达式支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM