我对 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 aPromise
into anActorRef
and it also needs to be reachable through remoting. So always prefertell
for performance, and onlyask
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/