When you use context.actorSelection inside an actor, what you are saying is finding the actor under this current control over the subjects (running / controlled). Since actor1 was probably not started by actor2 (or actor2 is not controlled), he will not solve anything. If actor1 actually belonged / started with actor2, then you could do context.actorSelection("/actor1") to get this child actor actor2. The reason context.system.actorSelection is because you first start up to system before starting the search and fully define the path to the actor. The system "owns" actor1 if you started it as system.actorOf , so using this path will allow you to go to it starting with system .
A little code to show what I mean:
class Actor1 extends Actor{ override def preStart = { context.actorOf(Props[Actor2], "actor2") } def receive = { case _ => } } class Actor2 extends Actor{ override def preStart = {println("my path is: " + context.self.path)} def receive = { case _ => } } object FutureTesting { def main(args: Array[String]) { val sys = ActorSystem("test") implicit val ec = sys.dispatcher
When you run this example, you will see:
my path is: akka://test/user/actor1/actor2 my path is: akka://test/user/actor2
So you can see that I have 2 instances of Actor2 working on my system; one of them was created directly from sys bound to /user/actor2 as the search path, and one of them was launched from the Actor1 instance bound to /user/actor1/actor2 for its path.
The acting system is hierarchical, and this example shows this. ActorSystem itself is the root of everything. The choice of participants is then similar to XPath in the context in which you issue a choice of questions.
source share