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

时间:2014-07-26 02:02:12

标签: scala akka scala-2.10 akka-persistence

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

1 个答案:

答案 0 :(得分:3)

默认情况下Actor在重启时处理它的子节点。这是Actor.preRestart代码:

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

正如您所见,父母会停下来并且不看其孩子。 你可以像这样覆盖它,让一个演员让孩子们活着:

override def preRestart(reason: Throwable, message: Option[Any]): Unit = ()

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