2016-06-07 41 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