2014-04-04 17 views
0

我有一個服務器 - 客戶端架構,其中有許多客戶端和一臺服務器。我想在每個客戶端創建一個actor並將它們放在服務器actor系統上。Akka無法動態創建遠程角色:Scala

我試圖動態地做到這一點。在遠程創建演員後(我不確定是否成功),我無法選擇它。這裏是我的服務器:

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://[email protected]: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'. 

我也嘗試通過我的配置文件創建遠程參與者具有類似的結果。任何想法讚賞!我不確定這是否重要,但我通過IDE運行此代碼。

+0

看到這個職位:http://stackoverflow.com/q/18838775/3248346 –

回答

3

您應該使用actorOf創建演員而不是actorSelection

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

Programmatic Remote Deployment