Akka 集群单例作为调度器

Akka cluster singleton as scheduler

我在运行玩Akka集群。 我需要一个 "Singleton Scheduler" 来每小时执行一些任务。

到目前为止我发现我应该使用 ClusterSinglegonManager。 但我不确定我的 Actor 应该是什么样子。 在我看来,我不需要 "receive" 方法。

也就是说,我是如何实例化我的单身人士的:

system.actorOf(
  ClusterSingletonManager.props(
    singletonProps = MySingletonActor.props(configuration),
    terminationMessage = PoisonPill,
    settings = ClusterSingletonManagerSettings(system)),
  name = "mysingletonactor")

适合:

object MySingletonActor {
  def props(configuration: Configuration): Props = Props(new MySingletonActor(configuration))
}

class MySingletonActor(configuration: Configuration) extends Actor with ActorLogging {
  context.system.scheduler.schedule(2 seconds, 2 seconds)(println("Hallo Welt"))
  def receive = ???
}

但它当然会引发异常,因为缺少 receive 方法的实现。但它有效。

去这里最好的方法是什么? 只安排一个 "Tick" 并在接收方法中处理 Tick 感觉很尴尬...

class MySingletonActor(configuration: Configuration) extends Actor with ActorLogging {
  case object Tick
  context.system.scheduler.schedule(2 seconds, 2 seconds, self, Tick)
  def receive = { case Tick => println("Hallo Welt") }
}

Akka 中有任何类型的单例调度程序吗?

您可以使用 Actor.emptyBehavior 来不引发异常,而不是将 ??? 编写为接收方法。这是一个完全不匹配任何消息的接收表达式。