关于Akka的新手问题-我正在阅读Akka Essentials,有人能解释一下Akka Stop/Poison Pill和Kill之间的区别吗?这本书给出了一个简单的解释:“杀戮是同步的,而毒丸是异步的。”但是以什么方式呢?在此期间调用actor线程是否锁定?在杀死、停止后调用等过程中是否通知了儿童演员?一个概念与另一个概念的对比?

很多谢谢!


stop和PoisonPill都将终止参与者并停止消息队列。它们将导致actor停止处理消息,向它的所有子进程发送一个stop调用,等待它们终止,然后调用它的postStop钩子。所有进一步的消息都被发送到死信邮箱。

不同之处在于在此序列开始之前处理哪些消息。在停止调用的情况下,当前正在处理的消息将首先完成,而所有其他消息将被丢弃。当发送一个毒药药丸时,这只是队列中的另一条消息,所以序列将在收到毒药药丸时开始。队列中在它前面的所有消息将首先被处理。

相比之下,Kill消息会导致参与者抛出一个ActorKilledException,该异常将使用正常的监督机制进行处理。所以这里的行为取决于你在监督策略中定义的内容。默认值是停止参与者。但是邮箱仍然存在,因此当参与者重新启动时,除了导致失败的消息外,它仍然拥有旧的消息。

也可以查看文档中的“停止演员”,“杀死演员”部分:

http://doc.akka.io/docs/akka/snapshot/scala/actors.html

更多关于监管策略:

http://doc.akka.io/docs/akka/snapshot/scala/fault-tolerance.html


在PoisonPill之前,在处理完所有接收到邮箱的消息后,它会异步地停止actor。


尽可能使用毒丸。它被放在邮箱上,并像任何其他消息一样使用。你也可以在actor中使用"context.stop(self)"。


You can use both actor stop and poison pill to stop processing of actors, and kill to terminate the actor in its entirety. x.stop is a call you make in akka receive method, will only replace actor state with new actor after calling postStop. x ! PoisonPill is a method that you pass to actor to stop processing when the actor is running(Recommended). will also replace actor state after calling postStop. x.kill will terminate the actor and will remove the actor in the actor Path and replace the entire actor with a new actor.