iwalktheline iwalktheline - 29 days ago 13
Scala Question

Replace a Router routee with an Actor of the same type

I am using a

akka.routing.Router
actor to broadcast a
Work
to different kind of actors. I use a
akka.routing.BroadcastRoutingLogic
to have this behaviour. The
Router
contains one single actor of each type (
Type1Actor
does job 1 with
Work
,
Type2Actor
does job 2 with the same
Work
...).

When an actor is terminated (no matter how) I receive a :

def receive = {
//...
case Terminated(a) => //HERE DO SOMETHING
}


I want to update my routees to always keep the same amount of active actors and to have the same amount of active actors of a type.
The type of
Terminated
actor is important : I don't want a work to be done twice by two actors of the same type, I don't want a work not to be done by an actor
TypeXActor
.

Is there a way to retrieve the appropriate
Props
from a terminated ref (
a
here) ? If not what are my options ?

Answer

Name your workers

Name your workers and then retrieve the name to decide the type of the actor

val router: ActorRef =
  context.actorOf(BroadcastPool(5).props(Props(new Worker(), "worker-type1")), "router")

Insider the router

case Terminated(actorRef) =>
  actorRef.path.name match {
    case "type1" => //add type1 worker
    case "type2" => //add type2 worker
    case _ => //ignore
  }