在Akka中,我如何知道在actorOf()中注册后何时可以使用actor? [英] In Akka, how do I know when an actor is ready to use after having been registered with actorOf()?

查看:104
本文介绍了在Akka中,我如何知道在actorOf()中注册后何时可以使用actor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Akka中使用context()。actorOf()创建一个actor,我将获得一个有效的ActorRef。但是,如果我做同样的事情,但是使用actorFor和一个我知道该actor将会出现的路径创建一个ActorRef,我将无法可靠地获得一个有效的ActorRef。我怎么知道演员已经成功注册?

If I create an actor using context().actorOf() in Akka, I get back a valid ActorRef. However, if I do the same but create an ActorRef using actorFor and the path I know that the actor will appear at, I do not reliably get a valid ActorRef back. How can I tell that an actor has been registered successfully?

在上面的描述中,我可以简单地使用actorOf()返回的ActorRef。但是,在我的实际情况中,我创建了一个演员,该演员本身注册了一个子演员,因此我需要解决该问题,所以通常的问题是如何等待/注册以通知某个演员已经在已知路径注册? 。

In the description above, I could simply use the ActorRef returned from actorOf(). However, in my actual case I create an actor that itself registers a child actor and I need to resolve that, so the problem in general is "how can I wait / register to be informed of an actor having been registered at a known path?".

推荐答案

首先,不能保证您从actorOf返回的ActorRef仍然有效,因为

First of all, there's no guarantee that the ActorRef you get back from actorOf is still alive, since the constructor of it could have failed.

其次,actorFor可能会找到该actor,但是它在被发现之后以及在您开始使用它之前就死了。

Secondly, actorFor might find the actor but it died just after being found and just before you started working with it.

第三,通常良好的做法是构建应用程序,以便以逻辑方式传播依赖关系,从而使参与者之间有一个天然的交汇点。

Thirdly, it's normally good practice to structure your application so that dependencies are propagated in a logical fashion, so that there is a natural rendez-vous point between your actors.

希望以上任何一种帮助,

Hope any of the above helps,

祝你好运!

这篇关于在Akka中,我如何知道在actorOf()中注册后何时可以使用actor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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