New to Akka. Creating a new Scala class that extends SupervisorStrategy
gives me the following template to work with:
class MySupervisorStrategy extends SupervisorStrategy {
override def decider: Decider = ???
override def handleChildTerminated(context: ActorContext, child: ActorRef,
children: Iterable[ActorRef]): Unit = ???
override def processFailure(context: ActorContext, restart: Boolean,
child: ActorRef, cause: Throwable, stats: ChildRestartStats, children: Iterable[ChildRestartStats]): Unit = ???
}
I'm looking for a way to access:
- The
Throwable
/Exception
that was thrown from the child actor - The child actor
ActorRef
that threw the exception - The message that was passed to the child actor that prompted the exception to be thrown
I think the Decider
(which is actually a PartialFunction[Throwable,Directive]
) gets passed the Throwable
whenever the child throws the exception, but I'm not seeing where I could get access to #2 and #3 from my list above. Any ideas?
Update
From the posted fiddle, it looks like a valid Decider
is:
{
case ActorException(ref,t,"stop") =>
println(s"Received 'stop' from ${ref}")
Stop
case ActorException(ref,t,"restart") =>
println(s"Received 'restart' from ${ref}")
Restart
case ActorException(ref,t,"resume") =>
println(s"Received 'resume' from ${ref}")
Resume
}
Above, I see all three:
- The exception that was thrown by the child
- The child (
ref
) that threw the exception - The message that was sent to the child originally (that caused the exception to be thrown)
It looks like there's nothing in that Decider
that needs to be defined inside that Supervisor
class. I'd like to pull the Decider
logic out into, say, MyDecider.scala
and find a way to refactor the Supervisor
so that its supervisorStrategy
uses an instance of MyDecider
, so maybe something similar to:
class Supervisor extends Actor {
import akka.actor.OneForOneStrategy
import akka.actor.SupervisorStrategy._
import scala.concurrent.duration._
var child: ActorRef = _
override val supervisorStrategy =
OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute, decider = myDecider)
...
}