Akka:向演员发送将来的消息

编程入门 行业动态 更新时间:2024-10-24 12:25:49
本文介绍了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:向演员发送将来的消息

本文发布于:2023-11-25 03:04:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628060.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将来   演员   消息   Akka

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!