Akka Futures Exceptions

后端 未结 1 1119
自闭症患者
自闭症患者 2021-02-06 12:13

What happens when an actor of a future throws an exception?

According to the Akka documentation at http://doc.akka.io/docs/akka/snapshot/scala/futures.html:

相关标签:
1条回答
  • 2021-02-06 12:44

    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
      }
    
    0 讨论(0)
提交回复
热议问题