Akka:向演员发送未来消息

时间:2013-05-06 14:52:14

标签: scala akka

我在Actor

中有以下代码
def receive = {
    case All() => {
        val collection: BSONCollection = db("ping")
        val future:Future[List[Ping]] = collection.find(BSONDocument()).cursor[Ping].toList()
        val zender = sender
        future onComplete {
            case Success(list) => zender ! list
            case Failure(throwable) => zender ! List()
        }
    }
}

我不喜欢如何使用onComplete函数将结果发送回发送方actor。我想知道是否可以将它转换成这样的东西:

def receive = {
    case All() => {
        val collection: BSONCollection = db("ping")
        val future:Future[List[Ping]] = collection.find(BSONDocument()).cursor[Ping].toList()
        "sender ! future" // one option
        "future.map( list => sender ! list)" //Another option. I know it's not map, but maybe another function        
    }
}

我觉得未来的链接会更好地流动。

2 个答案:

答案 0 :(得分:31)

您可以使用管道模式。只需import akka.pattern.pipe,然后您就可以将来自期货的邮件传递给future pipeTo actor的演员。

答案 1 :(得分:11)

如果您希望在发生故障时有一个空列表,您可能希望链接调用“recover”和“pipeTo”。

相关问题