我正在尝试了解Akka Akka的监管策略。当我在下面有这样的代码时,我确实得到了
java.lang.ArithmeticException:/零
case object CreateChildren
case class DivideNumbers(n: Int , d:Int)
object SuperVision extends App {
val actorSystem = ActorSystem("SupervisingActorSystem")
val actor = actorSystem.actorOf(Props[ParentActor], "ParentActor")
actor ! CreateChildren
val child1 = actorSystem.actorSelection("/user/ParentActor/childActor")
child1 ! DivideNumbers(4,0)
class ParentActor extends Actor{
override def receive: Receive = {
case CreateChildren =>
context.actorOf(Props[ChildActor], "childActor")
}
}
class ChildActor extends Actor{
override def receive: Receive = {
case DivideNumbers(n,d) => println(n/d)
}
}
actorSystem.terminate()
}
但是当我没有创建Child Actor并且有类似的东西时,我看不到异常。
val actorSystem = ActorSystem("SupervisingActorSystem")
val actor = actorSystem.actorOf(Props[ParentActor], "ParentActor")
actor ! DivideNumbers(4, 2)
class ParentActor extends Actor {
override def receive: Receive = {
case DivideNumbers(n, d) => println(n / d)
//case DivideNumbers(n, d) => throw new Exception
//Even this doesn't throw an exception
}
}
actorSystem.terminate()
答案 0 :(得分:1)
您没有收到异常,仅仅是因为异常引发之前,actor系统已经终止,然后应用程序退出了。
尝试在Thread.sleep(1000)
之前添加actorSystem.terminate()
,您将看到异常。
顺便说一句:此行为与if you use only one actor or with a child
无关。如果您使用孩子只是因为它是与时间顺序有关的随机行为,您会得到例外。