I'm using Akka 2.10 with JAVA.
I have a method that takes an Actor reference for me - or create one if there it wasnt before - but some times when I try to create one I receive the following exception:
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)
Of course, I know, the exception is very clear: I'm trying to create more than one actor with the same identifier, but I just do this when I can't find actor's ref. In time:
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;
}
I wait 4 seconds without an answer... so i create a new one.
I searched the internet through a solution for my case, but without success...
Can anyone help me to solve this problem?
Thanks a lot!
Global.onStart
? This way you have sure it will be available when you need it – Salem