为什么调用监管策略默认案例?

时间:2016-10-27 12:48:50

标签: scala akka akka-supervision

您好我已经对Parent actor进行了监督,而且孩子正在抛出MongoTimeOutException(我故意为测试目的提出了这个异常),但默认情况是在监督中调用的。

以下是代码:

class child extends Actor {
 def receive = {
  case insertData(data) => 
     //insertion code ,MongoTimeOutException is raised i have killed the service intentionally 
 }
}

class parent extends Actor {

val child=context.actorOf.....//actor creation code

override val supervisorStrategy = OneForOneStrategy(
                                    maxNrOfRetries = 10, withinTimeRange = 10 seconds) {
    case _:MongoException=>
      log.error("Got some MongosException, Supervision Strategy says Resume")  
      Resume
    case _:ElasticsearchTimeoutException=>
      log.error("Got some ElasticsearchTimeoutException, Supervision Strategy says Resume")  
      Resume
    case _:ElasticsearchException=>
      log.error("Got some ElasticsearchException, Supervision Strategy says Resume")  
      Resume
    case _ => 
      log.error("Got some Exception, Supervision Strategy says Resume")
      Resume
  }

 def receive = {
  case Msg(data) => 
     child ! insertData(data)
 }
}

object test extends App
{
 val parent = system.actorof....//actor creation code
 parent !Msg(data)
}

当我运行此代码时,确实发生了MongoTimeOutException但是这个案例在监督中被调用了。

case _ => 
      log.error("Got some Exception, Supervision Strategy says Resume")
      Resume

为什么会这样?据我说它应该引用这个案例:

case _:MongoException=>
  log.error("Got some MongosException, Supervision Strategy says Resume")  

我做错了吗?

请帮帮我。

1 个答案:

答案 0 :(得分:0)

打印默认案例值的类,看看什么是真正的例外,以便您可以处理它。

这是您的代码

case _ => 
      log.error("Got some Exception, Supervision Strategy says Resume")
      Resume

以下是您需要做的事情(打印例外类)

case ex =>
     log.error(s"Exception $ex of type ${ex.getClass}.Got some Exception, Supervision Strategy says Resume", ex)
    Resume

上述代码将帮助您了解无法捕获的确切消息。