简体   繁体   English

akka.net是一种获取或创建演员的方法

[英]akka.net is there a a way to get or create actor

For my actor hierarchy, I do not know all the actors I need until I process the data through a few actors, so I'm looking for a way to either return an existing ActorRef or create a new action. 对于我的actor层次结构,在通过几个actor处理数据之前,我不知道我需要的所有actor,因此我正在寻找一种方法来返回现有的ActorRef或创建一个新的动作。 This is what I would like the code below to either create an actor if one does not exist at "my-id-1" or return the one that already exists. 这就是我希望下面的代码创建一个actor,如果一个不存在于“my-id-1”或者返回已经存在的那个。

Context.ActorOf(MyActor.Props(message), "my-id-1");

The above code will (as documented) throw a InvalidActorNameException if the actor already exists. 如果actor已经存在,上面的代码将(如文档所述)抛出InvalidActorNameException How can I accomplish this in Akka.net? 我怎样才能在Akka.net中实现这一目标?

You can check if current actor has a child with provided name by using Context.Child(actorName) method. 您可以使用Context.Child(actorName)方法检查当前actor是否具有提供名称的子级。 It will return actor ref of the target actor if it exists or ActorRefs.Nobody if there is no such actor. 它将返回目标actor的actor ref(如果存在)或ActorRefs.Nobody如果没有这样的actor。

Code in your case could look like: 您的案例中的代码可能如下所示:

var child = Context.Child(actorName);
if (Equals(child, ActorRefs.Nobody))
    child = Context.ActorOf(MyActor.Props(message), actorName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM