Akka actorFor vs通过ActorRef [英] Akka actorFor vs passing an ActorRef

查看:115
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆