Scala Akka集群单例作为调度器

Scala Akka集群单例作为调度器,scala,playframework,akka,akka-cluster,Scala,Playframework,Akka,Akka Cluster,我正在用Akka集群运行游戏。 我需要一个“单例调度器”来每小时执行一些任务 到目前为止,我发现我应该使用ClusterSinglegonManager。 但我不确定我的演员长得怎么样。 在我看来,我不需要“接收”方法 也就是说,我如何稳定我的单身: system.actorOf( ClusterSingletonManager.props( singletonProps = MySingletonActor.props(configuration), termination

我正在用Akka集群运行游戏。 我需要一个“单例调度器”来每小时执行一些任务

到目前为止,我发现我应该使用ClusterSinglegonManager。 但我不确定我的演员长得怎么样。 在我看来,我不需要“接收”方法

也就是说,我如何稳定我的单身:

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方法的实现。但它是有效的

到这里最好的方式是什么? 安排一个“勾号”并在receive方法中处理勾号会让人感觉很尴尬

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中是否有任何类型的单例调度器?

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

如果您只想使用调度程序,可以直接从系统中使用它,调度消息,并在参数中指定一个
ActorRef
,由谁接收消息。唯一的好处是,你有一个单身演员,可以收到一条消息停止或取消计时器。我在应用程序启动时启动调度程序。我有类似cron作业的功能。因此,如果我在集群中启动5个应用程序,调度程序将启动5次。这正是我想要避免的。