【问题标题】:Spring - schedule task in some period of timeSpring - 在某个时间段内安排任务
【发布时间】:2018-09-09 16:36:34
【问题描述】:

目前我正在使用 Spring 注释每 20 分钟安排一次任务:

@Scheduled(fixedRate = 1200000)

我想更改我的任务仅在凌晨 2 点到 5 点之间执行的计划(仍然每 20 分钟一次)。

Spring Scheduler 可以吗?或者有解决这个问题的最佳实践吗?

谢谢

【问题讨论】:

  • 或者你可以使用 cron 表达式

标签: java spring spring-boot scheduled-tasks scheduling


【解决方案1】:

@Scheduled 有 'cron' 元素,因此您可以使用 cron 的全部功能

【讨论】:

    【解决方案2】:
    @Scheduled(cron = "0 0/20 2-5 * * ?")
    

    0/20 -> 每 20 分钟

    2-5 -> 凌晨 2 点到 5 点

    【讨论】:

      猜你喜欢
      • 2019-09-29
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 2021-02-06
      • 2011-10-11
      相关资源
      最近更新 更多