Akka Singleton - 不接受消息

时间:2013-06-21 11:02:18

标签: akka akka-cluster

这是我的愚蠢 - 我没有将索引器道具传递给系统创建。我会在这里留下答案,以防任何人获得一些好处*

我正在创建一个单例并发送如下消息:

 val indexerProps = ClusterSingletonManager.props(had => Props(
            classOf[SingleCoreIndexer], dataProvider, publisher, name), name, End, None)

        val coreIndexer = system.actorOf(indexerProps, name)
        //val coreIndexer = system.actorOf(Props(classOf[SingleCoreIndexer], dataProvider, publisher, name))

        coreIndexer ! "start_indexing"

注释掉的行显示可以正常工作的非单身道具

当我运行应用程序时,我收到以下错误:

[WARN] [06/21/2013 11:55:32.443] [deadcoreindexerstest-akka.actor.default-dispatcher-5] [akka://deadcoreindexerstest/user/node1] unhandled event start_indexing in state Start

所有其他功能都停止工作,这与暗示“coreIndexer”actor没有收到“start_indexing”消息的消息相关

更多代码:

class Indexer(systemCreator: SystemCreator, publisherProps: Props, dataProviderProps: Props, name: String) {

    def start {
        val system = systemCreator.create
        val dataProvider = system.actorOf(dataProviderProps)
        val publisher = system.actorOf(publisherProps)

        val indexerProps = ClusterSingletonManager.props(
            singletonProps = had => Props(classOf[SingleCoreIndexer], dataProvider, publisher, name),
            singletonName = "aaa",
            terminationMessage = End,
            role = None
            )

        val coreIndexer = system.actorOf(Props(classOf[SingleCoreIndexer], dataProvider, publisher, name))
        coreIndexer ! "start_indexing"
    }
}



class SingleCoreIndexer(dataProvider: ActorRef, publisher: ActorRef, name: String) extends Actor {

    def receive = {

        case "start_indexing" => {
            println("Single core indexer starting indexing")
            dataProvider ! new NextBatchOfDataPlease
        }

        case BatchOfData(data) => {
            publisher ! (name, data)
            self ! "next_batch"
        }

        case "next_batch" => {
            dataProvider ! new NextBatchOfDataPlease
        }
    }
}

看起来我正在向经理发送消息而不是单身人士。但是,当我向单身人士发送消息时,没有任何反应:

class Indexer(systemCreator: SystemCreator, publisherProps: Props, dataProviderProps: Props, name: String) {

    def start {
        val system = systemCreator.create
        val dataProvider = system.actorOf(dataProviderProps)
        val publisher = system.actorOf(publisherProps)

        val indexerProps = ClusterSingletonManager.props(
            singletonProps = had => Props(classOf[SingleCoreIndexer], dataProvider, publisher, name),
            singletonName = "singlecoreindexer",
            terminationMessage = End,
            role = None
            )

        system.actorOf(Props(classOf[SingleCoreIndexer], dataProvider, publisher, name))
        val coreIndexer = system.actorSelection(s"/user/$name/singlecoreindexer")
        coreIndexer ! "start_indexing"
    }
}

2 个答案:

答案 0 :(得分:3)

我知道这已经解决了,但即使使用此处提供的信息,我还需要一段时间才能弄清楚如何向单身人士发送消息并认为我离开了我在这里找到的信息

Cluster Singleton文档未能很好解释的两个关键概念是:

  1. 使用ClusterSingletonManager.props创建的actor是实际实例的父级,并且
  2. 您应该仅使用该演员的地址来创建ClusterSingletonProxy
  3. 群集中的每个节点都将创建单例管理器,最终胜出的那个节点是最老的,是您实际想要与之交谈的单例的父节点。 ClusterSingletonProxy确保您正在与实际代理进行通信,即使单例暂时不可用或迁移到另一个节点,您也始终在与正确的实例进行通信。

    鉴于该信息,代码应为:

    class Indexer(systemCreator: SystemCreator, publisherProps: Props, dataProviderProps: Props, name: String) {
    
      def start {
        val system = systemCreator.create
        val dataProvider = system.actorOf(dataProviderProps)
        val publisher = system.actorOf(publisherProps)
    
        val indexerProps = ClusterSingletonManager.props(
          singletonProps = had => Props(classOf[SingleCoreIndexer], dataProvider, publisher, name),
          singletonName = "singlecoreindexer",
          terminationMessage = End,
          role = None
        )
    
        val singletonManager = system.actorOf(
          Props(classOf[SingleCoreIndexer],dataProvider, publisher, name)
        )
    
        val indexerPath = (singletonManager.path / name)
        val coreIndexer = system.actorOf(
          ClusterSingletonProxy.props(indexerPath, None),
          s"$name-proxy"
        )
    
        coreIndexer ! "start_indexing"
      }
    }
    

答案 1 :(得分:2)

您所看到的问题(我认为)源于您向ClusterSingletonManager发送消息,而不是发送给位于其下方的实际演员。尝试通过名称(actorFor)查找下面的演员,它应该有效。