KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have an actor which creates a child actor to perform some lengthy computations. The problem is that the initialization of the child actor takes a few seconds and all messages that the parent actor sends to the child between it is created and gets fully initialized are dropped. This is the logic of the code that I am using: class ChildActor extends Actor { val tagger = IntializeTagger(...) // this takes a few seconds to complete def receive = { case Tag(text) => sender ! tagger.tag(text) case "hello" => println("Hello") case _ => println("Unknown message") } } class ParentActor extends Actor { val child = context.ActorOf(Props[ChildActor], name = "childactor") // the below two messages seem to get lost child ! "hello" child ! Tag("This is my sample text") def receive = { ... } } How could I get around that problem? Is it possible to make the parent actor wait until the child is fully initialized? I will be using the child actor with routing and possibly on remote actor systems. EDIT Following drexin's advice I have change my code into: class ChildActor extends Actor { var tagger: Tagger = _ override def preStart() = { tagger = IntializeTagger(...) // this takes a few seconds to complete } def receive = { case Tag(text) => sender ! tagger.tag(text) case "hello" => println("Hello") case _ => println("Unknown message") } } class ParentActor extends Actor { var child: ActorRef = _ override def preStart() = { child = context.ActorOf(Props[ChildActor], name = "childactor") // When I add // Thread.sleep(5000) // here messages are processed without problems // wihout hardcoding the 5 seconds waiting // the below two messages seem to get lost child ! "hello" child ! Tag("This is my sample text") } def receive = { ... } } <
Tags (comma-separated)
Save Edits
Cancel