Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka model supervision

Tags:

scala

akka

I have a problem with managing supervision.

Let's say I have actor A which creates actor B and sends him a message.

 val B = context.actorOf(Props[B],"B")
 B ! RandomMessage(param1, param2)

B do so calculations or ask remote service. Basically it could fail with Exception.

 override val supervisorStrategy = OneForOneStrategy(loggingEnabled = false) {
case exception:NetworkException => {
  Restart
  // here I don't have access to param1 and param2 to send message again
}

}

It works fine, because child actor is restarted, but what if I want to retry this failed message. How can I do this ?

like image 326
Axpom Avatar asked Dec 21 '25 13:12

Axpom


1 Answers

The message is available in preRestart.

override def preRestart(reason: Throwable, message: Option[Any]) = {
    self ! message.get
    //to continue default behavior. ie, to restart all child actors
    //you could even manage how you deal with your child actor here instead of calling super.preRestart
    super.preRestart(reason, message) 
}

Make sure that you configure supervision strategy in such that a way that it doesnt fall into infinite loop.

EDIT

As @Viktor Klang mentioned, dont use Option#get instead use Option#getOrElse and handle the odd case.

like image 53
Johny T Koshy Avatar answered Dec 24 '25 03:12

Johny T Koshy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!