0

I am trying to improve the error handling with the actors in my system. Sometimes, when processing data, something goes wrong, and I need to stop and restart the actor, as well as log some information regarding this failure.

I have a Supervisor, which has 5 actors working for it. So I need to be able to supervise all of them. I found this link:

https://doc.akka.io/docs/akka/current/typed/fault-tolerance.html

regarding this, but I don't think it is very clear on where to implement the code:

Behaviors.supervise(behavior).onFailure[IllegalStateException](SupervisorStrategy.restart)

Where exactly is this code supposed to go?

Thanks

TreyBCollier
  • 213
  • 3
  • 8
  • It's not totally clear whether you're asking about the classic or the typed API (if it's the typed API I'd suggest adding the `akka-typed` tag (perhaps replacing the `actor` tag). Supervision in the classic API (e.g. you have code that `extends Actor`) is very different and documented at https://doc.akka.io/docs/akka/current/supervision-classic.html – Levi Ramsey Oct 04 '21 at 16:15
  • Hi, yes sorry you are right. I am using Classic Actors. I am trying to find something that seems applicable to my scenario. Each supervisor has 5 child actors, and if one of these actors fail, I need to log it at error level, and retrieve the data that this actor was processing. @LeviRamsey – TreyBCollier Oct 05 '21 at 09:49

1 Answers1

1

You can think of this supervisor as another behavioiur which wraps your behaviour inside of it.

Lets say you want to have following HelloWorld actor.

object HelloWorldActor {

  sealed trait Command
  case class HelloCommand(name: String) extends Command

  def apply(): Behavior[Command] =
    Behaviors.receiveMessage[Command] { 
      case msg: HelloCommand =>
        println(s"Hello ${msg.name}")
        Behaviors.same
    }

}

Now, you can "wrap" this "behaviour" with a "supervisor"

object SupervisedHelloWorldActor {

  sealed trait Command
  case class HelloCommand(name: String) extends Command

  def apply(): Behavior[Command] =
    Behaviors.supervise(
      Behaviors.receiveMessage[Command] { 
        case HelloCommand(name) =>
          println(s"Hello ${name}")
          Behaviors.same
      }
    ).onFailure(onFailure[IllegalStateException](SupervisorStrategy.restart))

}
sarveshseri
  • 13,738
  • 28
  • 47