2017-06-16 49 views
0

,我有以下我演員的(我稱之爲演員MasterActor)接收方法的代碼:停止的演員實例,並等待其停止

override def receive: Receive = { 
    case StopActor(id, actorConfig) => 
     log.info(s"Stopping actor with id = $id and config $actorConfig") 

     stopActor(id, powerPlantCfg).pipeTo(self) 
     context.become(waitForStop(sender())) 

    // Other messages... not shown here for simplicity 
    } 

那麼我上面做的是停止演員並將包含上述Receive方法的Actor的結果傳遞給Future [Continue](其中Continue是Monix Ack類型)的結果。該stopActor看起來是這樣的:

private def stopActor(id: Long, cfg: ActorConfig): Future[Ack] = async { 
    await(fetchActor(id).materialize) match { 
     case scala.util.Success(actorRef) => 
     log.info(s"Stopping Actor with id = $id") 
     context.watch(actorRef) 
     context.stop(actorRef) 
     Continue 
     case scala.util.Failure(fail) => 
     log.error(s"Could not fetch Actor instance for id = $id because of: $fail") 
     Continue 
    } 
    } 

我做的context.watch(actorRef),這是我waitForStop看起來像:

private def waitForStop(source: ActorRef): Receive = { 
    case Continue => 
     source ! Continue 
     context.become(receive) 

    case someShit => 
     log.error(s"Unexpected message $someShit received while waiting for an actor to be stopped") 
    } 

所以我有2個問題在這裏:

  1. 在做context.become(waitForStop(sender()))時,我關閉了sender(),所以我假設發件人在這種情況下是ActorRef,它包含上述所有代碼,它是MasterActor 。我對麼?

  2. 我如何明確知道,我試圖停止的這個ActorRef實際上是停止的,這樣一旦它停止,我就可以做一個context.unwatch(actorRef)?

有什麼建議嗎?

回答

2

您可以通過觀看來通知演員的停止。您已經熟悉手錶:

val kenny = context.actorOf(Props[Kenny], name = "Kenny") 
context.watch(kenny) 

然後您可以等待終止消息。一旦你收到它,你可以解開你需要的東西。

def receive = { 
    case Terminated(kenny) => println("OMG, they killed Kenny") 
    case _ => println("Parent received a message") 
} 

所以我的建議是簡單地看,等待終止,併發出停止命令。但我不確定你在問什麼,所以這個錯誤將會是錯誤的答案。 Blog post example

+0

很酷!那麼誰會發送Termintaed消息?它是否由ActorSystem發送? – sparkr

+0

@sparkr是的。這就是觀看的全部內容:) –

+0

但是,在您描述或獲取終止消息後,我不會發送停止命令嗎? – sparkr