Akka actorFor vs通过ActorRef

编程入门 行业动态 更新时间:2024-10-12 12:34:21
本文介绍了Akka actorFor vs通过ActorRef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在学习Akka,我试图弄清楚如何让演员互相交谈(我们称他们为 A 和 B )。这不是请求/响应方案, A 和 B 随时都在互相发送消息。

I'm learning Akka and I'm trying to figure out how to get actors talking to each other (let's call them A and B). It's not a request / response scenario, A and B are sending each other messages at any time.

此刻,我有两个同级演员,彼此双向传递消息。它们都直接在 ActorSystem 上创建。我最初将 A 的 ActorRef 传递给 B 。但是我不能将 B 的 ActorRef 传递给 A ,因为它尚不存在,即我不能将此方法用于循环引用。

At the moment I've got two sibling actors that pass messages in both directions to each other. They're both created directly on the ActorSystem. I had initially passed the ActorRef of A into the constructor of B. But I can't pass the ActorRef of B to the constructor of A because it doesn't exist yet, i.e. I can't use this method for circular references.

我一直在阅读 actorFor ,这将允许我使用其路径查找演员。但是,我对这种设置不满意,因为如果路径改变了,编译器就不会捕获它。

I've been reading about actorFor and this would let me look up an actor using it's path. However, I'm not comfortable with this setup, because if the path changes, it won't be caught by the compiler.

另一种选择是考虑到每个actor都可以访问传递给父级,就是将消息从 A 和 B 传递给父级,然后让父级传递消息返回到 A 和 B 。但这会将父母与来回传递的消息类型联系在一起。

Another alternative, considering every actor has access to it's parent, is to pass the messages from A and B to the parent and then have the parent pass the message back down to A and B. But this couples the parent to the message types being passed back and forth.

人们使用什么策略使演员彼此了解?我对按路径查找演员是否太谨慎了?

What are strategies are people using for making actors aware of each other? Am I being too cautious about looking up actors by path?

推荐答案

在我的拙见中,您有以下三种策略,我从中列出

In my humble opinion you have three strategies, which I list from the closer to your problem (but also to me the worst pattern, I am sorry)

策略1:创建actor A和actor B,将actorRef A传递给构造函数您的乒乓将从演员B发送消息给演员A,而演员A可以简单地使用发件人引用进行回复。 (或者相反)

Strategy 1: you create actor A and actor B, passing actorRef A to the constructor of actor B. Your ping-pong will start from actor B sending a message to actor A, and actor A can simply reply using the sender reference. (or the other way around)

策略2:在应用程序中创建一个图层,负责命名:它在创建actor以及查询时分配名称。这将问题集中在一个点上。

Strategy 2: you create a layer in your application which takes care of the naming: it assigns the name at creation of the actor, as well as when querying. This centralizes the problem in a single point.

策略3:您想知道两个兄弟姐妹演员是否在打乒乓球取代了更好的,更模块化的演员体系,基本上每个演员都只与他的父母和孩子沟通,而对兄弟姐妹一无所知。

Strategy 3: You wonder if two siblings actors playing ping-pong are not replacing a better, more modular actor hierarchy where basically each actor communicate only with his parent and his children and has no knowledge about his siblings.

更多推荐

Akka actorFor vs通过ActorRef

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

发布评论

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

>www.elefans.com

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