防止akka actor重启child actor

时间:2013-12-11 19:38:46

标签: scala akka akka-supervision

我正在尝试让一个角色Worker在其自己的主管SubWorker重新启动时不重启其子角色Mngr。即使在覆盖preRestart()中的postRestartWorker之后,我也未成功。日志仍显示SubWorker直到重新启动。我是Akka和演员模特的新手,我不知道我做错了什么。

case class Start()
case class ThrowExp()

class Mngr extends Actor {

  val log = Logging(context.system, this)

  override def preStart(): Unit = {
    self ! Start
  }

  def receive: Receive = {

    case Start =>
      context.actorOf(Props[Worker], "myWorker")

    case "walker_throw_exp" =>
      context.child("myWorker").get ! ThrowExp
  }
}

class Worker extends Actor {


  val log = Logging(context.system, this)

  override def preStart(): Unit = {
    self ! Start
  }


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


  override def postRestart(reason: Throwable): Unit = { }

  def receive: Receive = {

    case Start =>
      // create sub worker
      context.actorOf(Props[SubWorker], "mySubWorker")


    case ThrowExp => throw new Exception("Some exception")

  }
}

class SubWorker extends Actor {
  val log = Logging(context.system, this)

  def receive: Receive = {
    case _ => log.info("I'm a sub worker")
  }
}

val system = ActorSystem("MySystem")
import system.dispatcher

val manager = system.actorOf(Props[Mngr], "Manager")
system.scheduler.scheduleOnce(2.seconds, manager, "walker_throw_exp")

日志

[DEBUG] [12/11/2013 20:22:04.409] [main] [EventStream(akka://MySystem)] logger log1-Logging$DefaultLogger started
[DEBUG] [12/11/2013 20:22:04.411] [main] [EventStream(akka://MySystem)] Default Loggers started
[DEBUG] [12/11/2013 20:22:04.417] [MySystem-akka.actor.default-dispatcher-4] [akka://MySystem/system] now supervising Actor[akka://MySystem/system/deadLetterListener#-1362953699]
[DEBUG] [12/11/2013 20:22:04.419] [MySystem-akka.actor.default-dispatcher-2] [akka://MySystem/system/deadLetterListener] started (akka.event.DeadLetterListener@250f4a60)
[DEBUG] [12/11/2013 20:22:04.427] [MySystem-akka.actor.default-dispatcher-3] [akka://MySystem/user] now supervising Actor[akka://MySystem/user/Manager#-684317580]
[DEBUG] [12/11/2013 20:22:04.429] [MySystem-akka.actor.default-dispatcher-2] [akka://MySystem/user/Manager] started (com.gangfly.gangbot.Mngr@34b0e482)
[DEBUG] [12/11/2013 20:22:04.431] [MySystem-akka.actor.default-dispatcher-2] [akka://MySystem/user/Manager] now supervising Actor[akka://MySystem/user/Manager/myWorker#429127943]
[DEBUG] [12/11/2013 20:22:04.432] [MySystem-akka.actor.default-dispatcher-4] [akka://MySystem/user/Manager/myWorker] started (com.gangfly.gangbot.Worker@7b70a0d3)
[DEBUG] [12/11/2013 20:22:04.434] [MySystem-akka.actor.default-dispatcher-4] [akka://MySystem/user/Manager/myWorker] now supervising Actor[akka://MySystem/user/Manager/myWorker/mySubWorker#2129589969]
[DEBUG] [12/11/2013 20:22:04.435] [MySystem-akka.actor.default-dispatcher-2] [akka://MySystem/user/Manager/myWorker/mySubWorker] started (com.gangfly.gangbot.SubWorker@3c2a5fb9)
[ERROR] [12/11/2013 20:22:06.465] [MySystem-akka.actor.default-dispatcher-2] [akka://MySystem/user/Manager/myWorker] Some exception
java.lang.Exception: Some exception
    at com.gangfly.gangbot.Worker$$anonfun$receive$2.applyOrElse(Main.scala:57)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:498)
    at akka.actor.ActorCell.invoke(ActorCell.scala:456)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:237)
    at akka.dispatch.Mailbox.run(Mailbox.scala:219)
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

[DEBUG] [12/11/2013 20:22:06.465] [MySystem-akka.actor.default-dispatcher-2] [akka://MySystem/user/Manager/myWorker] restarting
[DEBUG] [12/11/2013 20:22:06.469] [MySystem-akka.actor.default-dispatcher-2] [akka://MySystem/user/Manager/myWorker] restarted
[DEBUG] [12/11/2013 20:22:06.470] [MySystem-akka.actor.default-dispatcher-3] [akka://MySystem/user/Manager/myWorker/mySubWorker] restarting
[DEBUG] [12/11/2013 20:22:06.471] [MySystem-akka.actor.default-dispatcher-3] [akka://MySystem/user/Manager/myWorker/mySubWorker] restarted

1 个答案:

答案 0 :(得分:4)

重新启动Actor,因为其内部状态已变为无效且无法再信任。由于它创建的子actor是actor的状态的一部分,因此它们也需要被清除 - 通过停止和重新创建(这是默认值)或依次重新启动。这是无法避免的。如果你有一个演员A应该在其他演员B中存活,那么A不能是B的孩子。所以在这种情况下你需要重组你的监督等级。