Quartz:永远不会执行的Cron表达式 [英] Quartz: Cron expression that will never execute

查看:1306
本文介绍了Quartz:永远不会执行的Cron表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有重复的此处,这可能正是我的情况,尽管它应该得到一些更好的解释,但我将在此处尝试提供.

I know there is a duplicate here, which probably is exactly my case, though it would deserve some better explanation, which I will try to provide here.

我使用Spring应用程序上下文来处理Java Web应用程序.在这种情况下,我使用Quartz定义了计划作业.这些作业由.properties文件中定义的cron触发.

I work with a Java web application using a Spring application context. In this context, I defined scheduled jobs using Quartz. These jobs are triggered by a cron defined in a .properties file.

Spring上下文嵌入战争中,而.properties文件位于应用程序服务器(在这种情况下为Tomcat)上.

The Spring context is embedded within the war, while the .properties file is on the application server (Tomcat in this particular case).

这很好,并且可以根据环境(开发,集成,生产...)定义不同的克朗.

This is just fine and allows to define different crons according to the environment (development, integration, production, ...).

现在,当在我自己的计算机上本地运行此应用程序时,我不希望执行这些作业.有没有办法编写永远不会触发的cron表达式?

Now, when running this application locally on my own computer, I do not wish these jobs to be executed. Is there a way to write a cron expression which will never trigger?

推荐答案

TL; DR

在Quartz 1中,您可以使用以下cron:59 59 23 31 12 ? 2099(上次有效日期).
在Quartz 2中,您可以使用以下cron:0 0 0 1 1 ? 2200

TL;DR

In Quartz 1, you may use this cron: 59 59 23 31 12 ? 2099 (last valid date).
In Quartz 2, you may use this cron: 0 0 0 1 1 ? 2200

使用org.quartz.CronExpression进行了一些快速测试.

Made some quick tests using org.quartz.CronExpression.

String exp = "0 0 0 1 1 ? 3000";
boolean valid = CronExpression.isValidExpression(exp);
System.out.println(valid);
if (valid) {
    CronExpression cronExpression = new CronExpression(exp);
    System.out.println(cronExpression.getNextValidTimeAfter(new Date()));
}

当我执行String exp = "# 0 0 0 1 1 ?";时,isValid测试返回false.

When I do String exp = "# 0 0 0 1 1 ?";, the isValid test returns false.

使用上面给出的示例,输出如下:

With the sample given above yet, the output is the following:

true
null

含义:

  • 表达式有效;
  • 没有与该表达式匹配的即将到来的日期.

尽管如此,要让调度程序接受cron触发器,后者必须与将来的日期匹配.

For the scheduler to accept a cron trigger, though, the latter must match a date in the future.

我尝试了几年,发现一旦年份超过2300,Quartz似乎就不再烦恼了(尽管我没有提到该年份的最大值

I tried several years and figured out that once the year is above 2300, Quartz seems not to bother anymore (though I did not find a mention to a maximal value for the year in Quartz 2's documentation). There might be a cleaner way to do this, but this will satisfy my needs for now.

因此,最后,我建议的时间表是0 0 0 1 1 ? 2200.

So, in the end, the cron I propose is 0 0 0 1 1 ? 2200.

请注意,在Quartz 1中, 2099是最后一个有效的年.因此,您可以调整cron表达式以使用 Maciej Matys的建议:59 59 23 31 12 ? 2099

Note that, in Quartz 1, 2099 is the last valid year. You can therefore adapt your cron expression to use Maciej Matys's suggestion: 59 59 23 31 12 ? 2099

Arnaud Denoyelle 提出了一些更优雅的建议,我在上面的测试中证实了这一正确表达:而不是选择一个日期,请选择较远的日期:

Arnaud Denoyelle suggested something more elegant, which my test above validates as a correct expression: instead of choosing a date in a far future, choose it in a far past:

0 0 0 1 1 ? 1970(根据Quartz文档的第一个有效表达式).

0 0 0 1 1 ? 1970 (the first valid expression according to Quartz documentation).

此解决方案无效.

hippofluff 强调了Quartz过去检测到一个表达式将不再执行,因此将引发异常. >

hippofluff highlighted that Quartz will detect an expression in past will never be executed again and therefore throw an exception.

org.quartz.SchedulerException: Based on configured schedule, the given trigger will never fire.

这似乎是在Quartz中

This seems to have been in Quartz for a long time.

这突显了我的测试的弱点:如果要测试CronExpression,请记住它必须具有nextValidTime 1 .否则,您将其传递给的调度程序将简单地拒绝它,并带有上述异常.

This highlights a weakness of my test: in case you want to test a CronExpression, remember it has to have a nextValidTime1. Otherwise, the scheduler you will pass it to will simply reject it with the above mentioned exception.

我建议按照以下方式修改测试代码:

I would advise adapting the test code as follows:

String exp = "0 0 0 1 1 ? 3000";
boolean valid = CronExpression.isValidExpression(exp);
if (valid) {
    CronExpression cronExpression = new CronExpression(exp);
    valid = cronExpression.getNextValidTimeAfter(new Date()) != null;
}
System.out.println("Can I use <" + exp + ">? " + (valid ? "Go ahead!" : "This shall fail."));

您去了:无需思考,只需阅读输出即可.

There you go: no need to think, just read the output.

1 这是我在测试Arnaud解决方案时忘记的部分,这使我傻了,并证明我的测试不可靠.

1 This is the part I forgot when testing Arnaud's solution making me the fool and proving my test wasn't me-proof.

这篇关于Quartz:永远不会执行的Cron表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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