我使用Akka 2.10和JAVA。
我有一个方法可以为我提供一个Actor引用 - 或者如果以前没有它就创建一个 - 但有时当我尝试创建一个时我会收到以下异常:
akka.actor.InvalidActorNameException: actor name [<ActorName>] is not unique!
at akka.actor.dungeon.ChildrenContainer$NormalChildrenContainer.reserve(ChildrenContainer.scala:130)
at akka.actor.dungeon.Children$class.reserveChild(Children.scala:77)
at akka.actor.ActorCell.reserveChild(ActorCell.scala:369)
at akka.actor.dungeon.Children$class.makeChild(Children.scala:202)
at akka.actor.dungeon.Children$class.attachChild(Children.scala:42)
at akka.actor.ActorCell.attachChild(ActorCell.scala:369)
at akka.actor.ActorSystemImpl.actorOf(ActorSystem.scala:552)
当然,我知道,例外是非常明确的:我试图创建多个具有相同标识符的演员,但我只是在找不到演员的时候这样做REF。及时:
private static ActorRef getActor(String id,Class actor) throws Exception{
ActorSelection sel = system.actorSelection(system.child(id));
Timeout t = new Timeout(4, TimeUnit.SECONDS);
AskableActorSelection asker = new AskableActorSelection(sel);
scala.concurrent.Future<Object> fut = asker.ask(new Identify(1), t);
ActorRef actorClient = null;
try{
//Try to get an Actor reference
ActorIdentity ident = (ActorIdentity)Await.result(fut, t.duration());
actorClient = ident.getRef();
} catch(Exception e){
System.out.println("Error:"+id);
} finally{
//IF I dont found create a new One
if(actorClient==null){
actorClient = system.actorOf(Props.create(actor),id);
//THROWS ME AN EXCEPTION
}
}
return actorClient;
}
我等了4秒没有答案......所以我创建了一个新的。
我通过解决方案搜索了我的案例,但没有成功......
任何人都可以帮我解决这个问题吗?
非常感谢!
答案 0 :(得分:7)
根据documentation你不应该重复使用演员路径,即使演员已经死亡。我的猜测是你之前创造了一个具有该名字的演员,它已经死了,现在无法通过演员选择机制找到。在尝试创建具有该名称的新actor时,您将遇到此异常。