Ravi Sharma
Ravi Sharma

Reputation: 91

How to broadcast the same string message to all actors via ConsistentHashingPool in akka

I am new to Akka and stuck with this issue.

I have 4 actors but Somehow the broadcast message is always going to one actor

here is a sample code

        def hashMapping: ConsistentHashMapping = {
        case ReduceNameTitlePair(name,title) => {
        //println(s"\n *** Using ${name} as the key")
        name
        }
        }

        var actorReduceRouter = context.actorOf(RemoteRouterConfig(ConsistentHashingPool(numReducers,hashMapping = hashMapping), addresses).props(Props(classOf[ReduceActor])))

        actorReduceRouter ! Broadcast("SEND ME YOUR DATA"))

Please help

Upvotes: 0

Views: 241

Answers (1)

David Ogren
David Ogren

Reputation: 4810

In Classic Actors you can use Broadcast to send a message to all actors in a router, including ConsistentHashingRouter. When I run the below code I get the message received on all three actors.

You appear to be using Broadcast above, so I'm suspicious of your remoting configuration. But since you don't really post anything about your remoting setup here there's not much I can do to troubleshoot. I'd recommend using cluster-aware routers over manual remoting, but I don't know if that is in any way related to your problem.

import akka.actor.{Actor, ActorLogging, ActorSystem, Props}
import akka.routing.{Broadcast, ConsistentHashingPool}
import akka.routing.ConsistentHashingRouter.ConsistentHashMapping

object Worker {
  def props(): Props = Props(new Worker())
}
class Worker extends Actor with ActorLogging {
  def receive = {
    case s: String => log.info(s"${self.path.name} : $s")
  }
}

object AkkaQuickstart extends App {
  val system = ActorSystem("UntypedRouter")
  def hashHapping: ConsistentHashMapping = {
    case s: String => s
  }
  val router = system.actorOf(
    ConsistentHashingPool(3, hashMapping = hashHapping).props(Worker.props())
  )
  router ! Broadcast("hello")
}

Upvotes: 0

Related Questions