如何在父母重新开始时仅停止儿童演员?

Las*_*asf 5 scala akka scala-2.10 akka-persistence

我有一个有几个孩子的Akka父母演员.当重新启动父actor时,我需要它来简单地停止它的子节点,而不是停止并重新创建或重新启动它们.(如果需要,将在以后手动创建一个孩子.)有没有办法做到这一点?也许以preRestart某种方式覆盖父母的方法?

yǝs*_*ǝla 3

默认情况下,Actor重新启动时会处理其子项。这是Actor.preRestart代码:

\n\n
  /**\n   * User overridable callback: '''By default it disposes of all children and then calls `postStop()`.'''\n   * @param reason the Throwable that caused the restart to happen\n   * @param message optionally the current message the actor processed when failing, if applicable\n   * <p/>\n   * Is called on a crashed Actor right BEFORE it is restarted to allow clean\n   * up of resources before Actor is terminated.\n   */\n  @throws(classOf[Exception]) // when changing this you MUST also change UntypedActorDocTest\n  //#lifecycle-hooks\n  def preRestart(reason: Throwable, message: Option[Any]): Unit = {\n    context.children foreach { child \xe2\x87\x92\n      context.unwatch(child)\n      context.stop(child)\n    }\n    postStop()\n  }\n
Run Code Online (Sandbox Code Playgroud)\n\n

正如您所看到的,父级将停止并取消监视其子级。\n您可以像这样覆盖它,以使演员保持其子级存活:

\n\n
override def preRestart(reason: Throwable, message: Option[Any]): Unit = ()\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,出于您的目的,您不需要覆盖preRestart并且您将获得所需的行为。如果您想要更多自定义行为(例如在启动时启动子项但不在重新启动事件时启动子项),您可以查看其他回调。

\n