【问题标题】:Schedule a job for the first day of each month在每个月的第一天安排工作
【发布时间】:2016-10-25 09:19:14
【问题描述】:

我需要在每个月的第一天安排一个任务。到目前为止,我一直在使用这个:

system.scheduler.schedule(0.microseconds, 30.days, schedulerActor, "update")

但正如您可能已经猜到的那样,这有时会导致任务每月运行两次(三月)或一个月不运行(二月)。有没有更好的方法使用 Akka Scheduler 来安排每个月的第一天的任务?

【问题讨论】:

    标签: scala akka scheduled-tasks


    【解决方案1】:

    内置的 Akka 调度器与其说是调度器,不如说是一个延迟器。我建议使用akka-quartz-scheduler。此模块允许您实际安排任务在需要时运行。

    用法很简单。一些配置:

    akka {
      quartz {
        schedules {
          YourScheduleName {
            description = "A cron job that fires off every first of the month at 5AM"
            expression = "0 0 5 1 1/1 ? *"
          }
        }
      }
    }
    

    然后在代码中:

    case object Tick
    val yourActor = system.actorOf(Props[YourActor])
    QuartzSchedulerExtension(system).schedule("YourScheduleName", yourActor, Tick)
    

    【讨论】:

      猜你喜欢
      • 2013-09-22
      • 2013-05-23
      • 2017-11-19
      • 2021-01-09
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 2020-10-02
      相关资源
      最近更新 更多