1

I have a Supervisor Actor that creates child actors like this:

class SupervisorActor extends Actor {

  val child  = context.actorOf(MyConnectorActor.props(config),"MyConnector")

  def receive = {
    case Message => child ! "throw"
  }
}

class MyConnectorActor extends Actor {

  def receive = {
    case "throw" => throw new Exception()
  }
}

Now in my MyConnectorActor child, I throw an exception voluntarily. How should the SupervisorActor handle this? Do I need to add the supervisor strategy?

  override val supervisorStrategy =
    OneForOneStrategy(maxNrOfRetries = 30, withinTimeRange = 1.minute) {
      case _ =>
        println("RESTARTING CHILD FROM SUPERVISOR")
        Restart
    }

Even after adding this, I cannot see my child actor restarting? Any ideas as to what else needs to be done?

joesan
  • 13,963
  • 27
  • 95
  • 232

1 Answers1

0

Put a normal exception throw (throw new ...Exception) in place of the ??? as it's doing much more harm than a normal Exception does.

The ??? method defined in Predef actually throws an Error (sic!), an NotImplementedError to be precise. And Errors are not handled by the NonFatal() extraction that Akka uses in the exception handling code. This is because Errors are deemed as "the system MUST now STOP, ASAP", and not just an "Exception".

Replace the ??? with something more realistic, like a RuntimeException and you should see the Actor be restarted as expected.

Community
  • 1
  • 1
Konrad 'ktoso' Malawski
  • 13,102
  • 3
  • 47
  • 52
  • The exception is actually thrown in the child and I want this supervisor to react to that exception and Restart the child. I edited my post! Please take a look! – joesan Feb 16 '16 at 22:36
  • 1
    It around be restarting in any case since that's the default behavior. Your code should override the supervisionStrategy in the parent with OneForOneStrategy, not just have the one for one defined somewhere. Read up on details in the akka docs – Konrad 'ktoso' Malawski Feb 16 '16 at 22:42
  • How are you observing the lack of restart if I may ask? – Konrad 'ktoso' Malawski Feb 16 '16 at 22:44
  • I have a preRestart in my child and I see it is never being called! – joesan Feb 16 '16 at 22:47
  • Ok it was my mistake! There was a stale jar file and I cleared it up with sbt clean and then re-ran my application and I can see that the child is restarted. But how do I make it do the initialization that it did when started originally before the exception? – joesan Feb 16 '16 at 22:54
  • How could I do the initialization? I'm doing the initialization using the postRestart method. Is this the preferred way? – joesan Feb 16 '16 at 23:00
  • Yeah, the lifecycle hooks are the right spot to do things like init. – Konrad 'ktoso' Malawski Feb 18 '16 at 11:37