Jakub Dziworski
Jakub Dziworski

Reputation: 404

How does Akka Failure message work?

I use Failure message to let sender know that something went wrong. Responding back to sender with sender() ! Failure(new RuntimeException())

Does it throw an exception in the actor or on the sender side (or maybe neither)?

Upvotes: 2

Views: 1043

Answers (1)

johanandren
johanandren

Reputation: 11479

There are (at least) two Failure classes, scala.util.Failure is one of the types in the Scala Try API which is something like an alternative ADT for doing something like a try-catch. You can find more info about it in the scaladocs for Try

The other one is part of akka.actor.Status which is a similar ADT but much simpler API signaling either a success with a value or a failure with an exception. It is used internally in Akka, but also by some of the public APIs for example the pipe pattern, which sends the result of a future as a message to an ActorRef (in the Akka docs).

When sending and receiving Failure nothing special is done automatically, so on the receiving side you would have to match on Failure to accept such a message in your actor.

Since it is possible to compose receive blocks you can define reusable handling of Failure yourself and combine with the regular receive block of an actor, for example as described here

Upvotes: 1

Related Questions