Hi Yogesh, Actor creation is asynchronous: at the time when you get back the ActorRef from the actorOf call the Actor instance might not have been initialized at all, but the ActorRef is already usable for message sends (they will be delivered once the actor starts up). I admit that the sentence in the doc "Actors are automatically started asynchronously when created." is a bit buried :)
Also, you should be aware that in case of restart, a new Actor instance will replace the old one, so the constructor will run again. You might want to hook into actor lifecyle events instead: http://doc.akka.io/docs/akka/2.3.5/java/untyped-actors.html#Start_Hook If possible, you should avoid making calls directly from an Actor instance to some shared entity, try to implement all communication with message sends instead. -Endre On Sun, Aug 24, 2014 at 1:20 AM, Yogesh Shetty <[email protected]> wrote: > I have recently ported some of my code to leverage akka framework and I > randomly come across errors where I think actor constructor is not > initialized completed. > > I have this ActorFactory class which spins of supervisor actor, and in > each supervisor actor constructor i have code to spawn child actor as > described below. > > class ActorFactory(factorybuilder:IFactoryBuilder) extends IActorFactory{ > > createActor[ConfigSupervisor](Props(new ConfigSupervisor(this))) > createActor[RabbitSupervisor](Props(new > RabbitSupervisor(this,ConfigReader.getRabbitConfig))) > createActor[DAOChiefSupervisor](Props(new > DAOChiefSupervisor(this,factorybuilder.daoFactory))) > > } > > > class DAOChiefSupervisor(factory:IActorFactory,daofactory:IDAOFactory) > extends BaseSupervisor > { > val log = LoggerFactory.getLogger(this.getClass()) > > //Code to spin off child actor > val dao = factory.createChild[DAOSupervisor](this, Props(new > DAOSupervisor(factory,daofactory))) > } > > trait IActorFactory { > > val system = ActorSystem() > var actorMaps= TrieMap[String,IActorURI]() > val log = LoggerFactory.getLogger(this.getClass()) > > def createActor[A:TypeTag](instance:Props) : IActorURI = > { > val name = getName[A] > > val newactor = ActorURI(system.actorOf(instance,name)) > actorMaps += (name -> newactor) > log.info("Creating Actor " +name) > newactor > } > > def createChild[A:TypeTag](parent:Actor, instance:Props) :IActorURI = > { > val name = getName[A] > val newactor = ActorURI(parent.context.actorOf(instance,name)) > actorMaps += (name -> newactor) > log.info("Creating Child Actor " +name) > newactor > } > def getActor[A:TypeTag]() = > { > val name = getName[A] > val actor = actorMaps.get(name) > actor match > { > case None => log.error(f"Actor $name not found") > case _ => > } > actor > } > > > So what I am seeing is when I try to access DAOSupervisor from different > lines of code it gives me error message saying Actor not found, so it looks > like Actor constructor is not fully initialized when i get reference back. > > Thanks in advance for your help. > > -- > >>>>>>>>>> Read the docs: http://akka.io/docs/ > >>>>>>>>>> Check the FAQ: > http://doc.akka.io/docs/akka/current/additional/faq.html > >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user > --- > You received this message because you are subscribed to the Google Groups > "Akka User List" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/d/optout. > -- Akka Team Typesafe - The software stack for applications that scale Blog: letitcrash.com Twitter: @akkateam -- >>>>>>>>>> Read the docs: http://akka.io/docs/ >>>>>>>>>> Check the FAQ: >>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user --- You received this message because you are subscribed to the Google Groups "Akka User List" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.
