akka - making a single actor the child of several other actors (in a router)

323 views Asked by At

I'm not sure if this is feasible - can I make a single actor a child of many other actors? I can certainly pass in the ref to the other actors but I need the supervision to work in this case too - is this at all possible?

eg here is an example with a typed actor that is a child of system - I want it to be a child of the ConfigResponders in the config router

val zoolittle = Zoolittle(system, None, 
  Connect(LocalConfigService.zkAddress, LocalConfigService.zkSessionTimeout))
val configRouter = system.actorOf(Props(classOf[ConfigResponder], zoolittle).withRouter(
  RoundRobinRouter(nrOfInstances = 5)))

I don't want a bunch of connections to the db in this case.

Thanks in advance!

2

There are 2 answers

0
Roland Kuhn On BEST ANSWER

Each actor is created by exactly one other actor, and that actor is its parent and its supervisor. This is fundamental to the Actor Model and to what supervision means.

If you want an actor to be a child of your ConfigResponders, then each of the ConfigResponders will have to create such a child. If you are just after the supervisorStrategy then have another actor supervise it. Using a Router to install a supervisorStrategy is possible but a little hackish, since normally the supervisor will also have to perform other duties like distributing work or handling the lifecycle of the child.

0
JasonG On

I figured this out somewhat. The issue is you want to have a certain supervision strategy - you can use the supervision strategy from the class like so by creating a router of one actor.

  val actor: ActorRef = system.actorOf(Props(classOf[ActorClass], system.deadLetters).withRouter(
    RoundRobinRouter(1, supervisorStrategy = zkSupervisor)))


  val zkSupervisor =
    AllForOneStrategy() {
      case _: WhateverException ⇒
       //do stuff
        Restart
    }