pacman pacman - 2 months ago 19
Scala Question

Simple example of RoudnRobinGroup in Akka

I couldn't find worked example of RoundRobingGroup in Akka (examples in documentation couldn't help me). I want to tie two actors in one RoundRobinGroup and just send simple message with Akka (via router variable), but i always receive the deadletter in logs.

object Main extends App {
val system = ActorSystem()

val paths = List("w1", "w2")
val w1 = system.actorOf(Props[HelloActor], name = "w1")
val w2 = system.actorOf(Props[HelloActor], name = "w2")
val router = system.actorOf(new RoundRobinGroup(paths).props(), "shard")

router ! "hello"
}


I would like to know where is my mistake.

Answer

It resolved my problem

   class Master extends Actor {    
          var router = {
            val routees = Vector.fill(2) {
              val mappings = context.actorOf(Props[HelloActor])
              context watch mappings
              ActorRefRoutee(mappings)
            }
            Router(RoundRobinRoutingLogic(), routees)
          }

          def receive = {
            case message: String =>
              router.route(message, sender())
            case Terminated(a) =>
              router = router.removeRoutee(a)
              val r = context.actorOf(Props[HelloActor])
              context watch r
              router = router.addRoutee(r)
          }
        }

        class HelloActor extends Actor {
          override def receive: Receive = {
            case "hello" => println("Another actor said hello")
            case _ => println("Unrecognized message")
          }
        }

        object Main extends App {
          val system = ActorSystem()
          val master = system.actorOf(Props[Master], name = "parent")
          master ! "hello"
        }
Comments