scala – Akka:发送未来的消息给Actor
发布时间:2020-12-16 09:15:29 所属栏目:安全 来源:网络整理
导读:我在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(lis
我在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 } } 我觉得随着未来的链接,这会更好. 解决方法
您可以使用管道图案.只需导入akka.pattern.pipe,然后您将能够将未来的邮件从未来的pipeTo演员管理到演员.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |