2

嗨,我想包装 AskTimeOutException 并发送正确的消息....

  class Application @Inject()(system: ActorSystem) extends Controller {
  def index = Action { Ok("Welcome")}
  val ref: ActorSelection = system.actorSelection("akka.tcp://NLPSystem@127.0.0.1:5150/user/nlp")
  implicit val timeout = Timeout(5 seconds)
  def nlp(query: String) = Action.async {
      val fut: Future[NLPResponse] = (ref ? NLPRequest(query)).mapTo[NLPResponse]
      fut map {
        case e: akka.pattern.AskTimeoutException => InternalServerError("NLP engine is down please try after some time")
        case res: NLPResponse => Ok(res.response)
      }
  }
}

请建议我正确的处理方法..

4

1 回答 1

2

应使用恢复组合器处理未来的错误:

fut map {
  case res: NLPResponse => Ok(res.response)
} recover {
  case _: akka.pattern.AskTimeoutException => InternalServerError("NLP engine is down please try after some time")
  case e => InternalServerError("Other error: " + e.getMessage)
}
于 2015-10-16T21:19:49.913 回答