,我有以下我演員的(我稱之爲演員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個問題在這裏:
在做context.become(waitForStop(sender()))時,我關閉了sender(),所以我假設發件人在這種情況下是ActorRef,它包含上述所有代碼,它是MasterActor 。我對麼?
我如何明確知道,我試圖停止的這個ActorRef實際上是停止的,這樣一旦它停止,我就可以做一個context.unwatch(actorRef)?
有什麼建議嗎?
很酷!那麼誰會發送Termintaed消息?它是否由ActorSystem發送? – sparkr
@sparkr是的。這就是觀看的全部內容:) –
但是,在您描述或獲取終止消息後,我不會發送停止命令嗎? – sparkr