本文介绍了Akka:向演员发送将来的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Actor中有以下代码
I have the following code inside an 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函数将结果发送回发送方参与者一样。我想知道是否有可能将其转换为如下形式:
I don't like how I have to use the onComplete function to send the result back to the sender actor. I'd like to know if it is possible to convert it into something like this:
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 } }I感觉将来的链接会更好。
I feel that this flows better with future chaining.
推荐答案您可以为此使用管道模式。只需 import akka.pattern.pipe ,您就可以使用 future pipeTo actor 。
You can use the pipe pattern for that. Just import akka.pattern.pipe and then you'll be able to pipe messages from futures to actors with future pipeTo actor.
更多推荐
Akka:向演员发送将来的消息
发布评论