scala - Akka 中的询问模式

标签 scala akka

我对 Akka 中的 Ask 模式有疑问。

我正在使用应用程序中的询问模式调用 Actor 。我的应用程序是一个普通的类(不是 Actor 本身):

(actor1Ref ? msg) mapTo[ResponseMsg]

但是上面调用的 Actor 调用第二个 Actor ,例如:

secondActor ! msg(sender)

这是第二个 Actor 返回实际响应,例如:

originalSender ! responseMsg

所以基本上涉及到间接寻址,当 Actor1 调用 Actor2 时,它使用 !

我测试了代码并且它有效。但我不确定以上是否是编写此代码的正确方法。

那么我应该始终在 actor1 和 actor2 调用之间使用 并尝试 pipeTo 结果吗?或者我可以在中间调用之间使用 ! ,当我想返回原始调用者时,只需向我传递的引用发送一条消息?

最佳答案

您当前的方法非常好。事实上,最好使用 tell (!) 进行角色间的通信。来自 documentation :

There are performance implications of using ask since something needs to keep track of when it times out, there needs to be something that bridges a Promise into an ActorRef and it also needs to be reachable through remoting. So always prefer tell for performance, and only ask if you must.

[Tell] is the preferred way of sending messages. No blocking waiting for a message. This gives the best concurrency and scalability characteristics.

关于scala - Akka 中的询问模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46077704/

相关文章:

scala - 在构建SBT项目IntelliJ中,原点位置必须是绝对错误

java - Play Framework 2.0 中的动态模板

scala - 我一般如何处理 Scala 集合?

Akka 聚类冲突

scala - Akka-Http:如何测试一个请求

scala - Akka Streams 自定义合并

scala - 如何在我的 Play/Scala 项目中为 Swagger 的 @ApiOperation 指定 json 响应

scala - 在 Scala 中导入 avro 模式

actor - 关于Akka库和基于参与者的并发模型的好书

java - Apache 弗林克 : Standalone Cluster tries to connect with username "flink"