嵌套期货在Akka

时间:2013-10-17 11:08:25

标签: scala akka

我有两个演员,A和B.我的伪代码会这样读:

if A has a given state
    return "ok"
else 
    send a message to B and return "ok" when B is done handling the message

这是我使用Await的实现:

val f1 = (A ? GetState).mapTo[Option[State]]
f1.map {
    case Some(state) => "OK"
    case None =>
        val f2 = B ? Process
        Await.result(f2, 1 seconds) // todo: get rid of this
        "OK"    
}

我无法弄清楚如何在没有Await的情况下实现这一点。任何人吗?

1 个答案:

答案 0 :(得分:3)

尝试以下代码,看看它是否适合您:

val f1 = (A ? GetState).mapTo[Option[State]]
f1.flatMap {
    case Some(state) => Future.successful("OK")
    case None =>
        val f2 = B ? Process
        f2.map(t =>  "OK")    
}