Is it possible / advisable to have a different surveillance strategy for different Akka-2 children?

I play with Akka and I have a design in which the actor-supervisor has a child playing the role of A and several children playing the role of B. I want to define a supervision policy such as A failures, escalation (completion of the supervisor) and B cause individual participants to restart.

Is it possible? Is appropriate?

+7
scala akka actor-model akka-supervision
source share
1 answer

Yes, redefining supervisorStrategy . For example, from the documents :

 import akka.actor.OneForOneStrategy import akka.actor.SupervisorStrategy._ import scala.concurrent.duration._ override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: IllegalArgumentException => Stop case _: Exception => Escalate } 

Then, after reading the note:

If a strategy is declared inside the controlling subject (as opposed to inside the accompanying object), its decisive element has access to all internal states of the actor in thread safe mode, including receiving a link to the current process that has not passed the child (available as the sender of the error message).

Therefore, instead of matching with the type of exception, you would have to match with the sender (just enter here, it may not compile):

 override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { if (sender == SomeSpecialChild) Escalate else Restart } 

There is nothing wrong with that, but if you have groups of children playing different roles, you should probably follow the first Akka rule (well, my first Akka rule): when in doubt, you probably want another actor. Create one actor to control role A and another to control role B. You still need to override supervisorStrategy , but the test will be simpler. And it will be easier to manage any other special differences between roles A and B.

+7
source share

All Articles