I have a text field where users can enter a cron expression (e.g., 0 */5 * * * *
). I then split it and build a javax.ejb.ScheduleExpression
.
Now javax.ejb.ScheduleExpression
accepts any String for the different elements without validation. I can for example
scheduleExpression.minute("randomText");
and is accepted. If then try to use the ScheduleExpression I obviously get errors (e.g., when I try to create a timer with it).
I was beginning to write code to validate the input but the rules are not so short and trivial: http://docs.oracle.com/javaee/6/api/javax/ejb/ScheduleExpression.html
Is there a simple way (in Java EE) or a library that does already does the job?
Since I ran into the same problem I have builded a bean validator annotation for EJB timers.
Feel free to use it. For all those who do not want to or can use bean validation, have a look at the regex in file CronField.java to validate your strings manually.
The regular expressions are designed for "ScheduleExpression" not for "CronExpression" so my chosen name might seem a bit confusing.
If you have improvements or optimization, please send me a pull request or a message.
The source is available at this repository.
Usage: public class Month {
@CheckCronField(CronField.MONTH)
public String expression;
...
}
Some more examples are available in test folder in the same repository.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With