Akka actor没有收到带有DistributedPubSub的消息

时间:2017-02-05 15:51:19

标签: scala akka

我尝试使分布式消息的akka​​集群工作,但我被卡住了。我的演员已正确启动并订阅主题但未收到任何消息。这是代码

import akka.actor.{Actor, ActorSystem, Props}
import akka.cluster.client.ClusterClient.Publish
import akka.cluster.pubsub.DistributedPubSub
import akka.cluster.pubsub.DistributedPubSubMediator.{Subscribe, SubscribeAck}

case object DistributedMessage

object ClusterExample extends App {
  val system = ActorSystem("ClusterSystem")
  val actor = system.actorOf(Props(classOf[ClusterExample]), "clusterExample")
}

class ClusterExample extends Actor {
  private val mediator = DistributedPubSub(context.system).mediator

  mediator ! Subscribe("content", self)

  override def receive = {
    case SubscribeAck(Subscribe("content", None, `self`)) =>
      (1 to 100) foreach (_ => {
        mediator ! Publish("content", msg = DistributedMessage)
      })

    case DistributedMessage => println("received message from queue!")
  }
}

这是配置:

akka {
  log-dead-letters = 0
  log-dead-letters-during-shutdown = on

  actor {
    provider = "akka.cluster.ClusterActorRefProvider"
    enable-additional-serialization-bindings = on
  }
  remote {
    log-remote-lifecycle-events = off
    netty.tcp {
      hostname = "127.0.0.1"
      port = 2552

      bind-hostname = "0.0.0.0"
      bind-port = 2552
    }
  }
  extensions = ["akka.cluster.pubsub.DistributedPubSub"]

  cluster {
    seed-nodes = [
      "akka.tcp://ClusterSystem@127.0.0.1:2552"
    ]
  }
}

“从队列收到的消息”实际上从未打印过

1 个答案:

答案 0 :(得分:3)

愚蠢的错误。所以,问题是导入无效。这样:

import akka.cluster.client.ClusterClient.Publish

应替换为:

import akka.cluster.pubsub.DistributedPubSubMediator.Publish