Stopping a router when your child router terminates

4

I have Ator that would be Root and this actor Root has a router with 5 instances, and that same router also has a router with 5 instances. How can I stop ArquivoParaProcessar and tell Root that all instances of arquivoRouter have stopped?

class Root extends Actor {

  val arquivoRouter = context.actorOf(Props(new ArquivoParaProcessar(agregadorDeLinha)).withRouter(RoundRobinRouter(nrOfInstances = 5)))

  override def receive: Actor.Receive = {

    case "init" => {
      context.watch(arquivoRouter)
      arquivoRouter ! new Servidor(Servidor1)
      arquivoRouter ! new Servidor(Servidor2)
      arquivoRouter ! new Servidor(Servidor3)
      arquivoRouter ! new Servidor(Servidor4)
    }

    case Terminated(corpse) => {
      context.system.shutdown()
    }
  }
}

class ArquivoParaProcessar(agregadorDeLinha: ActorRef) extends Actor {

  val linhaRouter = context.actorOf(Props(new LinhaActor(agregadorDeLinha)).withRouter(RoundRobinRouter(nrOfInstances = 5)))

  context.watch(linhaRouter)

  override def receive = {
    case Servidor(caminho) => {
      for {
        arquivo <- new File(caminho).listFiles()
        if (arquivo.isFile)
        linha <- Source.fromFile(arquivo).getLines()
      } yield linhaRouter ! new LinhaParaProcessar(linha)

      linhaRouter ! PoisonPill
    }

    case Terminated(corpse) => {
         println("terminou")
         context stop self
    }
}

class LinhaActor(agregadorLinha: ActorRef) extends Actor
  //mais código
    
asked by anonymous 12.04.2014 / 02:32

1 answer

3

First, let's understand your hierarchy:

Root -> arquivoRouters -> "arquivoRouttees" (5) -> linhaRouter -> "linhaRoutees" (5)

As you are using routers, you have intermediate actors that represent these guys. Your context.watch is watching these intermediate actors, not the routees.

That is, what you want to do is more complicated than it looks. Coincidentally, in version 2.3.2 of Akka, RoundRobinRouter has been deprecated, and the new way of creating these routers does something that might be useful for you: the first example of the documentation shows the use of context.watch being applied to each routee individually. This page can help you:

link

    
12.04.2014 / 18:08