2016-06-07 42 views
2

我正在运行使用Akka集群播放。 我需要一个“Singleton Scheduler”来每小时执行一些任务。Akka集群单例作为调度程序

我到目前为止发现的是,我应该使用ClusterSinglegonManager。 但我不确定演员的外观如何。 在我看来,我不需要一个“接收”方法。

也就是说,我怎么instatiate我辛格尔顿:

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方法...

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") } 
} 

处理所述蜱是有任何一个Singleton调度在阿卡?

回答

2

代替将???作为接收方法,您可以使用Actor.emptyBehavior不引发异常。这是一个完全不匹配任何消息的Receive-expression。

+0

如果您只想使用调度程序,则可以直接在系统中使用它,调度您的消息并在参数中指定一个'ActorRef',谁应该接收消息。唯一的好处是,你有一个单身演员,可以收到一条消息来停止或取消定时器。 –

+0

我在应用程序启动时启动调度程序。我有像cron工作那样的功能。 因此,如果我在群集中启动5个应用程序,调度程序将启动5次。这正是我想要避免的。 – gun