验证cron表达式在Java中有效 [英] Verifying a cron expression is valid in Java
问题描述
我在使用Quartz编写一个Java调度应用程序。我使用CronTrigger,但是我的cron表达式在计划之前并且基于用户输入而输入到数据库。
I'm writing a scheduling application in Java using Quartz. I'm using the CronTrigger, but my cron expressions are entered into a database before they are scheduled and are based on user input.
有一种方法,我可以验证cron表达式在捕获它们时有效吗?我宁愿这样做,并给用户一个适当的错误消息,等待,直到调度程序运行,我得到一个ParseException当我尝试和创建触发器。
Is there a way I can verify that the cron expressions are valid when I capture them? I'd rather do this and give the user an appropriate error message than wait until the scheduler is run and I get a ParseException when I try and create the trigger. Which could be days after the user inputs the data.
推荐答案
你不能简单地创建一个触发器而不实际执行它吗?你可以简单地给出适当的反馈,以防ParseException异常。如果表达式是好的,则将表达式持久保存到DB。
Can't you simply create a trigger without actually executing it? You could simply give appropriate feedback in case of a ParseException. If the expression is okay, persist the expression to DB.
编辑:或者只是这样:
org.quartz.CronExpression.isValidExpression(expression);
这篇关于验证cron表达式在Java中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!