Reputation: 353
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
Upvotes: 1
Views: 435
Reputation: 116
Supervise normally happens in a parent-child structure, if you want to supervise n
actors, create a Supervisor as a father of those n
actors.
In typed, the example like this:
def parent2: Behavior[String] = {
Behaviors.setup { ctx =>
val child1 = ctx.spawn(Behaviors.supervise(child(1)).onFailure[IllegalStateException](SupervisorStrategy.restart), "child1")
val child2 = ctx.spawn(Behaviors.supervise(child(2)).onFailure[IllegalStateException](SupervisorStrategy.restart), "child2")
Behaviors.empty
}
}
Upvotes: 0
Reputation: 13985
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))
}
Upvotes: 1