简体   繁体   English

Java Quartz-Cron表达式

[英]Java Quartz - cron expression

I have a report creator program made in Java that needs to run every Monday and I used Quartz for my scheduling part. 我有一个用Java开发的报表创建器程序,该程序需要在每个星期一运行,并且我将Quartz用作调度部分。 The trick is, the amount of reports needed to be generated varies; 诀窍是,需要生成的报告数量各不相同; depending if the Monday is the first of the month, or the succeeding Mondays. 取决于星期一是每月的第一天还是随后的星期一。

To achieve this, what i did is create a trigger for each Monday of the month with the first Monday pointing to a specific job class while the rest of the Mondays(triggers) point to another job class. 为此,我要做的是为该月的每个星期一创建一个触发器,第一个星期一指向特定的工作类别,而其余的星期一(触发器)则指向另一个工作类别。

        .withIdentity("trigger1", "group1")
        .withSchedule(cronSchedule("0 1 0 ? 1/1 MON#1 *"))
        .build();

        .withIdentity("trigger2", "group1")
        .withSchedule(cronSchedule("0 1 0 ? 1/1 MON#2 *"))
        .build();

and so on until i reach 依此类推,直到我到达

        .withIdentity("trigger5", "group1")
        .withSchedule(cronSchedule("0 1 0 ? 1/1 MON#5 *"))
        .build();

It reached MON#5 because there could be 5 Mondays in a month; 它到达了MON#5,因为一个月中可能有5个星期一。 like this October. 像今年十月 This is fine, it works (I think), but my question is this. 很好,可以(我认为),但是我的问题是。 Is there a way to combine my cron schedule so that I will only need one trigger for the succeeding mondays? 有没有一种方法可以合并我的Cron日程安排,以便以后的星期一只需要触发一个触发器? Something like; 就像是;

        .withSchedule(cronSchedule("0 1 0 ? 1/1 MON#2,MON#3,MON#4,MON#5 *"))

If there is, please kindly enlighten me. 如果有的话,请赐教。 If I'm just missing something, something in my programming, or if there exists an unknown page in the labyrinth which is the web that leads me to the truth, then kindly point me to the right direction. 如果我只是在编程中缺少某些东西,或者迷宫中有一个未知的页面(就是引导我了解真相的网络),那么请向我指出正确的方向。

Your kind words are greatly appreciated. 非常感谢您的客气话。 Thanks 谢谢

ps: I included a cron tag even though it says it's for UNIX computers. ps:即使它说它是用于UNIX计算机的,我也包含了cron标记。 I believe there is little or no difference between Quartz cron and Unix cron. 我相信Quartz cron和Unix cron之间几乎没有差异。 If there is, then please do tell. 如果有,那么请告诉。

The usual unix workaround to having cron run something on the first X-day of the month is to have cron trigger a script on the form: 使cron在每月的第一个X天运行某些操作的通常的uniix解决方法是让cron触发表单上的脚本:

#! /usr/bin/bash
day=$( date +%d )
if ( ( day <= 7 ) ) ; then
    exec firstdayofthemonthshellscript.sh
fi

with a crontab entry on the form 在表单上带有crontab条目

0 0 * * 1 wrapperscript.sh

(for eg. monday). (例如星期一)。

To summarize, I don't think your goal is easily achievable using Quartz cron either, but perhaps you can work with the trick above in your implementation. 总而言之,我也不认为使用Quartz cron可以轻松实现您的目标,但是也许您可以在实现中使用上述技巧。

Cheers, 干杯,

It looks like there is no formal solution for your problem. 看来您的问题没有正式的解决方案。

Here is a quote from Quartz 2.0 documentation : 这是Quartz 2.0 文档的报价:

The '#' character is allowed for the day-of-week field. “星期几”字段允许使用“#”字符。 This character is used to specify "the nth" XXX day of the month. 此字符用于指定月份的“第n个” XXX天。 For example, the value of "6#3" in the day-of-week field means the third Friday of the month (day 6 = Friday and "#3" = the 3rd one in the month). 例如,“星期几”字段中的“ 6#3”值表示该月的第三个星期五(第6天=星期五,“#3” =该月的第三个星期五)。 Other examples: "2#1" = the first Monday of the month and "4#5" = the fifth Wednesday of the month. 其他示例:“ 2#1” =该月的第一个星期一,“ 4#5” =该月的第五个星期三。 Note that if you specify "#5" and there is not 5 of the given day-of-week in the month, then no firing will occur that month. 请注意,如果您指定“#5”,并且该月的指定星期几中没有5个,则该月将不会触发。 If the '#' character is used, there can only be one expression in the day-of-week field ("3#1,6#3" is not valid, since there are two expressions). 如果使用“#”字符,则“星期几”字段中只能有一个表达式(“ 3#1,6#3”无效,因为有两个表达式)。

There is also an open bug on this. 对此也有一个开放的错误

Personally, I would put this kind of logic in Java instead of within the Quartz configuration. 就个人而言,我将这种逻辑放在Java中,而不是在Quartz配置中。 Make Quartz always execute a Java job dispatcher which then determines which reports to run depending on the date. 使Quartz始终执行Java作业调度程序,然后根据日期确定要运行的报告。

As stated by Tomer, Quartz does not support this use case. 如Tomer所述,Quartz不支持该用例。 But if Quartz is not a constraint, a valid expression can be created at cron-utils . 但是,如果Quartz不是约束,则可以在cron-utils创建一个有效的表达式。 Below an example: 下面是一个例子:

String multicron = "0 1 0 ? 1/1 MON#2|MON#3|MON#4|MON#5 *";
CronDefinition definition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
CronParser parser = new CronParser(definition);
Cron cron = parser.parse(multicron);

cron-utils currently does not provide means to execute jobs, but will provide you next/previous execution times for any given date. cron-utils当前不提供执行作业的方法,但会为您提供任何给定日期的下一个/上一个执行时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM