I have an existing Scala application that I am trying to refactor in order to use Akka. One of the problems I have is how to manage error-checking in actor-based applications.
Usually error-checking is done through one of two mechanisms:
- either one returns a value indicating an error condition, such as
Option[A]
orFailure[A]
, or - by the use of exceptions.
Neither of these style seems particulary useful here. On the one hand, actor messages are usually "fire and forget", hence there are no return values. [One can have a return value using Futures, but it certainly not customary to ask for futures on every message.] On the other hand, processing of the message usually happens on another thread, so that one cannot catch an exception arising from the processing of a message.
One could simulate the first mechanism by sending back error or confirmation messages, such as
class FooActor extends Actor {
def receive = {
case Foo => ...
if (errorCondition) sender ! ErrorMessage
}
}
But if one has to do this for every actor, it becomes a lot of boilerplate and it seems a poor man's simulation of stack unwinding.
What is a good strategy to recover from errors in actor-based applications?
!
and getting a fixed overhead. I do not think this is the customary way to build an actor-based application.