Akka Futures Exceptions

最后都变了- 提交于 2019-12-03 04:23:25

For actors you need to catch the exception and return it as a failure status. Right now you're not returning anything to the sender so you're getting a timeout exception:

class Worker extends Actor {
  def receive = {
    case i: Int => {
      try {
        throw new RuntimeException
        sender ! "Some good result"
      } catch {
        case e: Exception =>
          sender ! akka.actor.Status.Failure(e) // Alert the sender of the failure
          throw e // Alert any supervisor actor of the failure
      }
    }
  }
}

Futures can handle this a little more gracefully since they always send a result, while actors do not (this would give you the same result as above):

  val future = Future {
    throw new RuntimeException
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!