【问题标题】:Can we make a @Scheduled execution on Spring, mixed with cron parameter, forcing a first execution at boot time?我们可以在 Spring 上进行 @Scheduled 执行,与 cron 参数混合,强制在启动时首次执行吗?
【发布时间】:2020-10-14 23:36:48
【问题描述】:

我在互联网上多次搜索 Baeldung here 提到的选项,但我找不到任何示例。我想使用这样的东西:

@Scheduled(cron="@reboot")
@Scheduled(cron="0 0 5 * * *")
public void somethingToDoOnRebootTime() {
  // code here, to run every day at 5a.m., AND at boot first time... 
}

但它不起作用,因为“@reboot”不是一个有效的 cron 表达式......我试图使用这个“@reboot”作为该方法的正常注释,但它也不存在。 ..

有人可以帮助我吗? Baeldung的文章是不是错了?

【问题讨论】:

  • 创建一个ApplicationListener,在偶数ContextRefreshedEvent 时触发该方法。
  • 差不多了,@M.Deinum!我使用 ApplicationListener 但使用 ApplicationReadyEvent!
  • 您没有在问题中提到 Spring Boot,并且没有 Spring Boot,该事件不存在。

标签: spring cron scheduled-tasks spring-scheduled


【解决方案1】:

基于@M.Deinum 评论...我使用了ApplicationListener,但使用了ApplicationReadyEvent!所以,我的例子变成了:

@EventListener(ApplicationReadyEvent.class)
@Scheduled(cron="0 0 5 * * *")
public void somethingToDoOnRebootTime() {
  // code here, to run every day at 5a.m., AND at boot first time... 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-13
    • 2018-03-19
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多