0

I have to pass supervision.decider but in akka 2.6.0+ Materializer does not have similar settings.

Can anyone share how to write below code in higher version:

val mat: ActorMaterializer = ActorMaterializer(Actor Materializersettings(supervision.decider))

Tried new way, not picking as strategy class requires String:

1.

val mat: Materializer = Matrializer(actorsystem)
mat.system.settings.supervisionstratgyclass = ???
  1. ActorAttributes(supervision.decider)

Any idea would be appreciated how to pass supervision strategy through materializer.

How we pass custom supervision strategy in settings? I don't use streams. It will simple startup call with materializer.

1 Answers1

1

Sounds like you want to setup a supervision strategy for a stream. Here you have some examples of how to do it from the official documentation: Stream error - supervision strategies.

As you can see, in the following example a default supervision strategy is being set for the runnable graph

val decider: Supervision.Decider = {
  case _: ArithmeticException => Supervision.Resume
  case _                      => Supervision.Stop
}
val source = Source(0 to 5).map(100 / _)
val runnableGraph =
  source.toMat(Sink.fold(0)(_ + _))(Keep.right)

val withCustomSupervision = runnableGraph.withAttributes(ActorAttributes.supervisionStrategy(decider))

In this other one, a supervision strategy is being defined at Flow level

val decider: Supervision.Decider = {
  case _: ArithmeticException => Supervision.Resume
  case _                      => Supervision.Stop
}
val flow = Flow[Int]
  .filter(100 / _ < 50)
  .map(elem => 100 / (5 - elem))
  .withAttributes(ActorAttributes.supervisionStrategy(decider))
val source = Source(0 to 5).via(flow)
Gastón Schabas
  • 2,153
  • 1
  • 10
  • 17
  • Thanks for input. I am not using any streams. It's simple startup and want to system exit as custom decider startegy on exception. Looking to implement with new materializer changes. – rimmi anand Apr 17 '23 at 06:06
  • Oh, sorry. Based on the tags of the post, I thought you were working with akka-streams. In this case you would need to use `Behaviors.supervise(behavior).onFailure(supervisorStrategy)`. That helper will let you supervise an actor (`Behavior`) and then call the method `onFailure` to decide which supervision strategy you want to apply based on your needs. The official docs [Akka - fault tolerance](https://doc.akka.io/docs/akka/current/typed/fault-tolerance.html) have good examples about how to do it – Gastón Schabas Apr 17 '23 at 16:03