gpt4 book ai didi

scala - Akka:向 Actor 发送 future 消息

转载 作者:行者123 更新时间:2023-12-03 15:10:43 24 4
gpt4 key购买 nike

我在 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 函数将结果发送回发送方参与者的方式。我想知道是否可以将其转换为这样的:
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
}
}

我觉得随着 future 的链接,这会更好地流动。

最佳答案

您可以为此使用管道模式。刚刚import akka.pattern.pipe然后你就可以使用 future pipeTo actor 将消息从 future 传送到 Actor 。 .

关于scala - Akka:向 Actor 发送 future 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16401376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com