Akka actors are great for reactively and asynchronously handling work loads. Typically, for larger actor systems, it’s best to use a Supervisor to handle unexpected failure. However, for top-level actors, it’s nice to be able to intercept and handle exceptions for alerting purposes. For this specific purpose, we created an actor trait called AlertingActor
, which very simply defines a preRestart
that gets called with the Throwable
when an unhandled exception occurs in an actor.
trait AlertingActor extends Actor { def alert(reason: Throwable, message: Option[Any]) = { // hook into your pre-existing alerting / healthcheck system // i.e., send emails, log, or call an error API } override def preRestart(reason: Throwable, message: Option[Any]) { alert(reason, message) postStop() } } // and in use: class SomeActor extends AlertingActor { // ... } |