If the parent-child actor uses withRouter() to configure the router, the child will not be able to see context.parent.I asked because I wanted to know the solution and the reason.
First of all, the context.parent can be referenced.
case object Ping
case object Bong
object Sample1 {
def run() = {
valsystem=ActorSystem()
val parent=system.actorOf(Props(classOf[Parent]), "parent")
US>parent! Ping
}
class Parent extensions Actor {
valworker=context.actorOf(Props(classOf[Child]), "child")
def receive = {
case Ping = >
worker! Ping
casePong=>
println("PONG!")
}
}
class Child extensions Actor {
def receive = {
case Ping = >
println(context.parent)
context.parent!Pong
}
}
}
Actor [akka://default/user/parent#-1479915643]
PONG!
The output is like this, and it's working as expected.
If you set withRouter when creating a worker,
valworker=context.actorOf(Props(classOf[Child]).withRouter(RoundRobinPool(3)), "child")
Results
Actor [akka://default/user/parent/child#-761629135]
Like this, PONG! is no longer available, and context.parent is my child.
Of course, if you use sender, you can return it to the sender parent, but when you hook it with postRestart, you can't return it to the sender, so if you use context.parent, you'll encounter this phenomenon.
Please let me know if you know more.
Thank you m(__)m
scala akka
I don't know much about it myself, but the official document has the following examples and descriptions, so
valescalator=OneForOneStratey(){
casee t testActor!e;SupervisorStrate.Escalate
}
val router=system.actorOf(RoundRobinPool(1, supervisorStratey=escalator).props(
routeProps=Props [TestActor]))
Would you like to set it up by yourself through the constructor of RoundRobinPool
when creating the router?
If the hierarchy generates a router,
Therefore, in this case, if you look at the parent from the child, it becomes router.The behavior of context.parent is not strange.(We unified them under the name of child, but if you use router, you are a grandchild from the perspective of your parents.)
The following sections of http://doc.akka.io/docs/akka/2.3.10/scala/routing.html#supervision apply to the official description:
Routes that are created by a pool router will be created as the router's children.
In the questioner's code, the router is named child, so context.parent is not shown as the Child class's Actor, but a router named child.
valworker=context.actorOf(Props(classOf[Child]).withRouter(RoundRobinPool(3)), "router")
If you try to println the parent and self and shoot the ping three times in a row, you can see that it is placed on router/$a or router/$b.
self: Actor [akka://default/user/parent/router/$a#-1029801143]
parent —Actor [akka://default/user/parent/router#-1266735066]
self: Actor [akka:// default/user/parent/router/$b#1593806271 ]
parent —Actor [akka://default/user/parent/router#-1266735066]
self: Actor [akka:// default/user/parent/router/$c#1456555264 ]
parent —Actor [akka://default/user/parent/router#-1266735066]
I don't know the details of the case where sender doesn't work, but it might be better to find out.
© 2024 OneMinuteCode. All rights reserved.