@Scheduled 没有具体日期
@Scheduled without specific date
我写了一个调度器
@Documented
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Scheduled(cron="0 0 0 * * *")
public @interface DeleteAndCopy{
}
每个午夜是什么意思。该过程大约需要 4 个小时。 (凌晨 0 点至凌晨 4 点)
现在 2017 年 7 月 28 日 1:00 下午有一个维护日。
我决定在这个日期不复制数据,以免出现意外状态。如何从执行中排除该日期?
cron 语法似乎不支持排除特定日期。请参阅来自 Unix 的 this answer 和 Linux StackExchange 站点:
Standard cron syntax is quite simple, it does not support exclusions. In some cases it is possible to create a list of several/many cron entries to implement such logic, but that tends be tedious and hard to understand or maintain; this approach is not applicable in your case though (not least because standard cron has no notion of the calendar year).
我可以为您的问题想出几个解决方案:
- 使定时器执行的逻辑有条件,如this answer中所述。你可以有一个布尔值 属性 来确定逻辑是否应该执行。不幸的是,计时器仍会触发,但您可以使用 属性 不执行其逻辑。
- 稍微复杂一点的解决方案与上述相同,但保留一个禁用执行的日期列表。
- 更简单,但不那么复杂,将日期的排除直接添加到代码中:
if (!LocalDate.now().equals(LocalDate.of(2017, 7, 28)))
。根据应用程序的类型,这可能是可以接受的。
- 第四个选项是以编程方式创建调度程序。您可以使用
HolidayCalendar
from the Quartz API to exclude the date. It has a method addExcludedDate(Date date)
which does exactly what you want. A full example, using Spring, is available in this preview of an E-book。我不确定我是否可以复制示例,所以我只发布 link.
Cron 不支持排除。
我能想到的最快的是像这样的双@scheduled 注释
@Scheduled(cron = "0 0 0 * 1-6,8-12 *") //every month except 7
@Scheduled(cron = "0 0 0 1-27,29-31 7 *") //every day of month 7 except 28
(先用几分钟尝试类似这样的东西,它似乎对我有用
@Scheduled(cron = "00 1-20,25-59 * * * *") //every minute except 21,22,23,24 for every hour
@Scheduled(cron = "00 21 10 * * *") //minute 21 for 10 am (dunno your timezone)
)
虽然绝对不优雅
原则上,可以在代码中要求 CMMS 提供计划的维护范围。
我写了一个调度器
@Documented
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Scheduled(cron="0 0 0 * * *")
public @interface DeleteAndCopy{
}
每个午夜是什么意思。该过程大约需要 4 个小时。 (凌晨 0 点至凌晨 4 点)
现在 2017 年 7 月 28 日 1:00 下午有一个维护日。
我决定在这个日期不复制数据,以免出现意外状态。如何从执行中排除该日期?
cron 语法似乎不支持排除特定日期。请参阅来自 Unix 的 this answer 和 Linux StackExchange 站点:
Standard cron syntax is quite simple, it does not support exclusions. In some cases it is possible to create a list of several/many cron entries to implement such logic, but that tends be tedious and hard to understand or maintain; this approach is not applicable in your case though (not least because standard cron has no notion of the calendar year).
我可以为您的问题想出几个解决方案:
- 使定时器执行的逻辑有条件,如this answer中所述。你可以有一个布尔值 属性 来确定逻辑是否应该执行。不幸的是,计时器仍会触发,但您可以使用 属性 不执行其逻辑。
- 稍微复杂一点的解决方案与上述相同,但保留一个禁用执行的日期列表。
- 更简单,但不那么复杂,将日期的排除直接添加到代码中:
if (!LocalDate.now().equals(LocalDate.of(2017, 7, 28)))
。根据应用程序的类型,这可能是可以接受的。 - 第四个选项是以编程方式创建调度程序。您可以使用
HolidayCalendar
from the Quartz API to exclude the date. It has a methodaddExcludedDate(Date date)
which does exactly what you want. A full example, using Spring, is available in this preview of an E-book。我不确定我是否可以复制示例,所以我只发布 link.
Cron 不支持排除。 我能想到的最快的是像这样的双@scheduled 注释
@Scheduled(cron = "0 0 0 * 1-6,8-12 *") //every month except 7
@Scheduled(cron = "0 0 0 1-27,29-31 7 *") //every day of month 7 except 28
(先用几分钟尝试类似这样的东西,它似乎对我有用
@Scheduled(cron = "00 1-20,25-59 * * * *") //every minute except 21,22,23,24 for every hour
@Scheduled(cron = "00 21 10 * * *") //minute 21 for 10 am (dunno your timezone)
)
虽然绝对不优雅
原则上,可以在代码中要求 CMMS 提供计划的维护范围。