I'm trying to develop an application using Futures and Akka supervisors but when A future returns a Failure to an actor its supervisor is not getting the exception.
Here's my code.
1) Supervisor actor
class TransaccionActorSupervisor() extends Actor with ActorLogging {
val actor: ActorRef = context.actorOf(Props[TransaccionActor].withRouter(RoundRobinPool(nrOfInstances = 5)), "transaccion-actor")
def receive = {
case msg: Any => actor forward msg
}
override val supervisorStrategy = OneForOneStrategy() {
case exception =>
println("<<<<<<<<<<<<<<<<<<< IN SUPERVISOR >>>>>>>>>>>>>>>>>>>>>>>>>>>>")
Restart
}
}
Supervised actor
Class TransaccionActor() extends Actor with ActorLogging {
implicit val _: ExecutionContext = context.dispatcher
val transaccionAdapter = (new TransaccionComponentImpl with TransaccionRepositoryComponentImpl).adapter
def receive = {
case msg: GetTransaccionById =>
val currentSender: ActorRef = sender()
transaccionAdapter.searchTransaction(msg.id).onComplete {
case Success(transaction) => currentSender ! transaction
case Failure(error) => throw error
}
}
What am I doing wrong?
Thank you all very much!
Exceptions thrown in a future within an actor are not caught by the actor. You'll need to pipe the exception to
self
and then re-throw if you want it handled by the supervising actor.