我有一个演员p: ParentActor
,我想要做一些清理工作,当它通过使用postStop
方法停止。其中一项工作涉及向儿童演员c: ChildActor
发送消息。之后,应该停止c
,然后p
。但是,一旦context.stop(p)
被称为c
似乎立即停止并且无法接收消息。不要立刻停止孩子Akka演员时,父母演员停止
下面是想我做的一个例子:
class ParentActor extends Actor {
...
override def postStop {
logger.info("Shutdown message received. Sending message to child...")
val future = c ? "doThing"
logger.info("Waiting for child to complete task...")
Await.result(future, Duration.Inf)
context.stop(c)
logger.info("Child stopped. Stopping self. Bye!")
context.stop(self)
}
}
结果错误消息:
[ERROR] [SetSystem-akka.actor.default-dispatcher-5] [akka://SetSystem/user/ParentActor] Recipient[Actor[akka://SetSystem/user/ParentActor/ChildActor#70868360]] had already been terminated. Sender[Actor[akka://SetSystem/user/ParentActor#662624985]] sent the message of type "java.lang.String".
另一种方法是发送p
一条消息说要关机,有上述行为发生的结果,但使用内置的停止功能似乎更好。
PS。这是一个新的应用程序,因此欢迎使用设计替代品