Actor's Choice

Why, when I choose the absolute path with the ActorContext, it does not work (the actor is incorrectly selected and does not receive the HelloResponse message)?

//From Actor2: //This doesn't work (Message never received) context.actorSelection("/user/actor1") ! HelloResponse("hello back1") //This works (actor 1 receives the message) context.system.actorSelection("/user/actor1") ! HelloResponse("hello back2") 

I am new to Scala / Akka, but reading the documentation seems to work.

+6
source share
3 answers
+4
source

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 //Starting an Actor2 from system sys.actorOf(Props[Actor2], "actor2") //Starting an Actor1 from system which in turn starts an Actor2 sys.actorOf(Props[Actor1], "actor1") } } 

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.

+2
source

From actor2 you will need to use

 context.actorSelection("/actor1") 

I agree that this is unintuitive, given that the metaphor is a file system, and when using a file system, the leading / is the absolute path that signifies the beginning at the root. Also it does not match actorFor , because

 context.actorFor("/user/actor1") 

returns the top level of Actor1 (see Absolute and relative paths )

EDIT - this was a bug that was fixed in Akka 2.1.4 (see Roland's answer). Starting with version 2.1.4 you can use context.actorSelection("/user/actor1") .

+1
source

Source: https://habr.com/ru/post/943810/


All Articles