gpt4 book ai didi

scala - Akka Actor /scala - 多个请求的单个 onSuccess

转载 作者:行者123 更新时间:2023-12-01 12:39:29 24 4
gpt4 key购买 nike

我有一系列的 akka Actor ,比如

A --> B --> C

Actor A“询问” Actor B, Actor B 又“询问” Actor C。Actor A 需要等到 Actor C 完成处理。B 是一个薄层,除了将消息传递(询问)给 C 并将 Future 返回给 A 之外什么都不做。基本上 B 只是做

        { case msgFromA => sender ! C ? msgFromA }

因此 A 得到的是 Future[Any]。

A 处理请求的方式是使用嵌套映射

actorRefFactory.actorOf(Props[A]) ? msgA map {
resp =>
// Type cast Any to Future and use another map to complete processing.
resp.asInstanceOf[Future[_]] map {
case Success =>
// Complete processing
case Failure(exc) =>
// Log error

这是有效的(即最终处理仅在 Actor C 完成处理时发生)但不用说它看起来很糟糕。我尝试使用平面图但无法使其工作。任何让它看起来不错的想法:)谢谢

最佳答案

更恰当的做法:

A 中:

val f: Future[MyType] = (B ? msg).mapTo[MyType]
f onComplete {
case Success(res) => // do something
case Failure(t) => // do something
}

B中,使用forward:

{ case msgFromA => C forward msgFromA }

C 中:

// call database
// update cache
sender() ! res // actually sends to A

关于scala - Akka Actor /scala - 多个请求的单个 onSuccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26343282/

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