Stopping a router when your child router ends

Asked

Viewed 107 times

4

I have a Ator that would be the Root and this actor Root has a router with 5 instances, and this same router also has a router with 5 instances. How can I stop ArquivoParaProcessar and warn the Root that all instances of arquivoRouter 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

1 answer

3

First, let’s understand your hierarchy:

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

As you are using routers, you have intermediary actors representing 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 seems. Coincidentally, in version 2.3.2 of Akka, the RoundRobinRouter has been deprecated, and the new way to create these routers does something that may be useful to you: the first example of documentation shows the use of context.watch being applied to each routee individually. This page can help you:

http://doc.akka.io/docs/akka/2.3.2/scala/routing.html

  • Thanks for the help, I’ll try that way and I’ll answer here as it turned out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.