验证 cron 表达式在 Java 中是否有效 [英] Verifying a cron expression is valid in Java

查看:115
本文介绍了验证 cron 表达式在 Java 中是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Quartz 用 Ja​​va 编写调度应用程序.我正在使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆