Akka无法动态创建远程Actor:Scala

时间:2014-04-04 13:34:12

标签: scala akka

我有一个服务器 - 客户端架构,其中有许多客户端和一台服务器。我想在每个客户端创建一个actor并将它们放在服务器actor系统上。

我试图动态地这样做。远程创建演员后(我不确定是否成功)我无法选择它。这是我的服务器:

   class TestActorSystem {
    val system = ActorSystem("testSystem", ConfigFactory.load("server.conf"))

    def shutdown = system.shutdown()

服务器配置:

akka {
  #loglevel = "DEBUG"
  actor {
     provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
     enabled-transports = ["akka.remote.netty.tcp"]
     netty.tcp {
         hostname = "127.0.0.1"
         port = 2552
     }
     log-sent-messages = on
     log-received-messages = on
  }
}

这是我的客户:

  class Client(id: String, remoteAddress: Address) {

    def this(id: String) = {
      this(id, Address("akka.tcp", "testSystem", "127.0.0.1", 2552))
    }

    implicit val timeout = Timeout(600.seconds)

    val conf = ConfigFactory.load()
    val system = ActorSystem("client", ConfigFactory.load("client.conf"))

    val remote = remoteAddress.toString

    private val broadcaster = system.actorSelection(Props[MyActor].withDeploy(Deploy(scope = RemoteScope(remoteAddress))), name = "joe")

    def shutdown() = system.shutdown

    def send = {
      val c = system.actorSelection("akka.tcp://testSystem@127.0.0.1:2552/user/joe")
      c ! "simple message"
    }

  }

客户端配置:

   akka {
 #log-config-on-start = on
 stdout-loglevel = "DEBUG"
 loglevel = "DEBUG"
 actor {
     provider = "akka.remote.RemoteActorRefProvider"
 }
 remote {
   enabled-transports = ["akka.remote.netty.tcp"]
   log-sent-messages = on
   log-received-messages = on
   netty.tcp {
         hostname = "127.0.0.1"
         port = 0
   }
 }
}

我启动服务器然后运行客户端,这是我认为的相关错误消息:

[INFO] [04/04/2014 09:32:09.631] [testSystem-akka.actor.default-dispatcher-17] [akka://testSystem/user/joe] Message [java.lang.String] from Actor[akka://testSystem/deadLetters] to Actor[akka://testSystem/user/joe] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.

我还尝试通过配置文件创建具有类似结果的远程actor。任何想法赞赏!我不确定这是否重要但我通过IDE运行此代码。

1 个答案:

答案 0 :(得分:3)

您应该使用actorOf来创建演员而不是actorSelection

val ref = system.actorOf(
            Props[SampleActor].withDeploy(
                Deploy(scope = RemoteScope(address))))

请参阅Programmatic Remote Deployment