akka.net是否有一种获取或创建演员的方法 [英] akka.net is there a a way to get or create actor

查看:49
本文介绍了akka.net是否有一种获取或创建演员的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的actor层次结构,直到我通过几个actor处理数据时,我才知道我需要的所有actor,所以我正在寻找一种返回现有ActorRef或创建新动作的方法.这就是我希望下面的代码创建一个actor(如果一个角色不存在于"my-id-1"处)或返回一个已经存在的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. 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.

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

上面的代码(已记录)将在参与者已经存在的情况下抛出 InvalidActorNameException .如何在Akka.net上完成此任务?

The above code will (as documented) throw a InvalidActorNameException if the actor already exists. How can I accomplish this in Akka.net?

推荐答案

您可以使用 Context.Child(actorName)方法检查当前actor的孩子是否具有提供的名称.如果存在,则返回目标actor的actor ref;如果不存在,则返回 ActorRefs.Nobody .

You can check if current actor has a child with provided name by using Context.Child(actorName) method. It will return actor ref of the target actor if it exists or ActorRefs.Nobody if there is no such 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);

这篇关于akka.net是否有一种获取或创建演员的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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