Search code examples
scalaakkaactorfault-tolerance

Akka OneForOneStrategy does not work


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

  • 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.