akka firstCompletedOf,识别邮件发件人

时间:2013-03-08 16:06:00

标签: scala akka future promise

我正在使用Scala 2.10 / Akka 2.1 / Play 2.1,我对firstCompletedOf有疑问。如何在firstCompletedOf主体中识别结果的发送者?请查看以下代码:

val futureString: Future[String] = myTestActor.ask(Message).mapTo[String]
val timeoutFuture = play.api.libs.concurrent.Promise.timeout("timed_out", 5 seconds)

Async {
  Future.firstCompletedOf(Seq(futureString, timeoutFuture)).map {
    case result: String => {
      println("got message " + result)
    }
  }
}

myTestActor内的某个位置有一个sender ! "actor_result",但超时可能会先出现。

是否有快速,方便的方法来识别结果的发件人? sender不起作用,我认为检查字符串(如果等于timed_out)会非常脏。

1 个答案:

答案 0 :(得分:0)

如下:

val futureString: Future[String] = myTestActor.ask(Message).mapTo[String]
val timeoutFuture: Future[String] = play.api.libs.concurrent.Promise.timeout(throw new TimeoutException(), 5 seconds)

Async {
  Future.firstCompletedOf(Seq(futureString, timeoutFuture)) map {
    case result: String => println("got message " + result)
  } recover {
    case _: TimeoutException => "Timed out?"
  }
}