Akka - router supervisorStrategy- how to send message to router on event

127 views Asked by At

Hi I want to implement a router strategy that on some event will send a message to the router, but I don't know how to get a reference to the router inside the strategy implementation.

This is what I'm trying to do:

 val router = system.actorOf(RoundRobinPool(5, supervisorStrategy = OneForOneStrategy(){
      case _: ActorKilledException => Escalate
      case _: ActorInitializationException => Escalate
      case _ => self ! SomeMsg(); Restart
    }).props(Props(classOf[MyClass]))) 

Where self should be the router ActorRef, but self is not recognized in this context.

1

There are 1 answers

1
paul On

I normally never create the case into the Router but outside

  val workerRouter: ActorRef = context.actorOf(
    Props[Worker].withRouter(RoundRobinPool(nrOfWorkers)), name = "workerRouter")

  val msgRouter: ActorRef = context.actorOf(
    Props[Worker].withRouter(RoundRobinPool(nrOfWorkers)), name = "msgRouter")

Then in my actor receiver function

def receive: PartialFunction[Any, Unit] = {
    case RunWorkersMsg =>
      workerRouter ! WorkMsg(i , numberOfElements)
    case ResultMsg(value) =>
      msgRouter ! WorkMsg()
  }

Here you can see my Akka project with an example https://github.com/politrons/Akka