Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka - Is it possible to get the message in the actor's supervisor on it's failure?

Tags:

scala

akka

actor

Is there a way to get a message in actor supervisor's SupervisorStrategy? I mean the one that caused actor's failure. I want to get some data from it.

like image 262
boneash Avatar asked Oct 01 '22 10:10

boneash


1 Answers

A possible approach:

  1. Define a new runtime exception exception class able to encapsulate the faulty message.
  2. In the supervised actor. Catch exceptions and throw instead the exception defined above with the faulty message.
  3. In the supervisor strategy, catch the new exception type and access the message.

For instance:

// The new exception type
case class MessageException( 
  akkaMessage: Any, 
  originalException: Throwable 
) extends RuntimeException("Exception due to message")

// In the supervised actor
def receive = {
  case msg => try{ process(msg) } catch { 
    case t => throw MessageException(msg,t) 
  }
}

// In the supervisor
override val supervisorStrategy =
  OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) {
     case MessageException(msg,t) => //decide what to do
  }
like image 101
paradigmatic Avatar answered Oct 13 '22 10:10

paradigmatic