简体   繁体   English

如何通过 akka 中的 ConsistentHashingPool 向所有参与者广播相同的字符串消息

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

I am new to Akka and stuck with this issue.我是 Akka 的新手,并且遇到了这个问题。

I have 4 actors but Somehow the broadcast message is always going to one actor我有 4 个演员,但不知何故,广播消息总是发给一个演员

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请帮忙

In Classic Actors you can use Broadcast to send a message to all actors in a router, including ConsistentHashingRouter .在 Classic Actors 中,您可以使用Broadcast向路由器中的所有 Actors 发送消息,包括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")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM