Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Quartz CronExpression validates wrong cron expression

I am using quartz-scheduler to schedule my jobs. I have a cron expression which is wrong:

1 15 10 ? * *ssddddfd

When I do:

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

checkCronExpression is true. 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;

Version of quartz is: 2.2.3

Please check the CronExpression class from http://www.javadoc.io/doc/org.quartz-scheduler/quartz/2.2.3

I also tried validateExpression method of CronExpression class with no success.

like image 783
Sadiksha Gautam Avatar asked Sep 10 '18 06:09

Sadiksha Gautam


1 Answers

This has recently been filed as a bug:

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

Indicates whether the specified cron expression can be parsed into a valid cron expression

And actually the expression you provided can be parsed into a valid CronExpression although the expression is not valid itself. 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. However as java.text.ParseException is a checked exception, this is rather pointless.

In summary the current implementation of CronExpression seems to be a little bit too lenient.

Maybe you want to give other libraries a try (just searched on google):

  • https://github.com/mariotaku/cron-expression-lite
  • https://github.com/jmrozanec/cron-utils

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.

like image 78
dpr Avatar answered Oct 31 '22 20:10

dpr