val b = context.system.actorOf(Props[B], "b")
should be changed to val b = context.actorOf(Props[B], "b")
to make new actor a child, not a top-level actor.
Akka OneForOneStrategy does not work
-
22-06-2023 - |
Question
I have the following code:
class A extends Actor with ActorLogging {
override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 2) {
case _ => log.info("An actor has been killed"); Restart
}
val b = context.system.actorOf(Props[B], "b")
def receive = {
case _ => context.system.scheduler.schedule(5 seconds, 5 seconds, b, true)
}
}
class B extends Actor with ActorLogging {
def receive = { case true => self ! Kill }
}
After self ! Kill
in an instance of actor A
I don't see a message "An actor has been killed" and subsequent call to actor A
generates "dead letters" message so there was no restart. Why is OneForOneStrategy
not getting called?
It's strange in a way that I can just remove the whole OneForOneStrategy
override and there are no changes in program behavior whatsoever.
Solution
OTHER TIPS
You're simultaneously restarting the actor "B" and then throwing an exception when you restart it. Remove the self ! true
from your postRestart
code. Otherwise, what do you expect to happen? You're sending it into an infinitely recursive restart loop.
Here's the order or the actions you're seeing to illustrate it:
- Create actor "A"
- Create actor "B"
- send
true
to A - send
true
to B - "B" throws exception at
true
message - "A" restarts "B"
- after restart "B" sends itself
true
- repeat step 5