我如何从Akka主管那里获得异常详情?

时间:2011-08-07 14:24:20

标签: scala akka akka-supervision

我正在测试我正在处理的新Actor如何处理意外消息。我想断言它会在这些情况下抛出GibberishException。这是迄今为止的测试和实施:

测试:

"""throw a GibberishException for unrecognized messages""" in {
  //define a service that creates gibberish-speaking repositories
  val stubs = new svcStub(
    actorOf(new Actor{
      def receive = { case _ => {
          self.channel ! "you're savage with the cabbage"
        }
      }
    })
  )
  val model = actorOf(new HomeModel(stubs.svc,stubs.store))
  val supervisor = Supervisor(
    SupervisorConfig(
      OneForOneStrategy(List(classOf[Exception]), 3, 1000),
      Supervise(model,Permanent) :: Nil
    )
  )
  try{
    intercept[GibberishException] {
      supervisor.start
      model !! "plan"
    }
  } finally {
    supervisor.shutdown
  }
  stubs.store.plan should equal (null)
  stubs.svcIsOpen should be (false)
}

实现:

class HomeModel(service: PlanService, store: HomeStore)
extends Actor {
  private val loaderRepo = service.getRepo()
  private var view: Channel[Any] = null

  override def postStop() = {
    service.close()
  }

  def receive = {
    case "plan" => {
      view=self.channel
      loaderRepo ! LoadRequest()
    }
    case p: Plan => {
      store.plan=p
      view ! store.plan
    }
    case _ => throw new GibberishException(_)
  }
}

然而,当我运行测试时,异常细节会进入我建立的Supervisor,但我不知道如何对它们做任何事情(比如记录它们或测试它们的类型)。我希望能够从主管那里获得异常详细信息,以便我可以在我的测试中重新抛出并拦截它们。在测试方法之外,如果您想在正在运行的应用程序的UI中报告异常的性质,我可以想象这很有用。有没有办法从Supervisor发生这种情况?

1 个答案:

答案 0 :(得分:3)

将OneForOneStrategy更改为仅处理GibberishException,应该解决它。