Search code examples
scalaakkaakka-typed

How to use PreRestart in Akka Typed?


I just came across an issue in my work that corresponds to https://github.com/akka/akka/issues/26899.

How can I write a typed behavior where I can get

case (reason: Throwable, message: Option[Any]) =>

for restarting (similar as in untyped actors) inside my Behaviors.receive for handling exception for concrete messages?

So for now I only see receiving PreRestart:

Behaviors.receiveMessage {
  msg =>
    //...
}.receiveSignal {
  case (actor, signal) =>
    println(signal)
    //...
}

Solution

  • PreRestart is not an exception handler but an opportunity to do cleanup. To emphasize this, the signal was left as simple as possible in Akka Typed.

    You could say, that's also a consequence of the "Let It Crash" paradigm: if an exception is expected, than you can just handle it where it occurs. However, if the exception is unexpected, than this is exactly the case that actor supervision and automatic restarts should handle: don't assume anything but start from a blank slate.

    Typed supervision strategy (tutorial post) can be chosen by exception type. Even there the details cannot be looked at for similar reason.