2014-09-03 31 views
7

我在一个JVM中启动了带有HelloActor的akka​​系统,并试图从另一个JVM中的客户端向它发送消息。没有任何工作。我应该如何正确地做到这一点? 下面是代码:如何发送消息到邻居jvm中的akka​​系统?

简单的服务器

package akkaSample.severalSystems 

    import akka.actor.{Props, Actor, ActorSystem} 
    import com.typesafe.config.ConfigFactory 

    class HelloActor extends Actor { 
     override def preStart(): Unit = { 
      println("Hello actor started") 
     } 

     def receive = { 
      case "mew" => println("I said mew") 
      case "hello" => println("hello back at you") 
      case "shutdown" => context.stop(self) 
      case _  => println("huh?") 
     } 
    } 

    object Server extends App { 
     val root = ConfigFactory.load() 
     val one = root.getConfig("systemOne") 
     val system = ActorSystem("HelloSystem", one) 
     val helloActor = system.actorOf(Props[HelloActor], "HelloActor") 
     println (system) 
     println("Remote application started.") 
    } 

简单的客户端

package akkaSample.severalSystems.client 

import akka.actor.ActorSystem 
import com.typesafe.config.ConfigFactory 
import scala.util.control.Breaks._ 

class Client { 

} 

object Client extends App { 
    println("started") 

    val root = ConfigFactory.load() 
    val two = root.getConfig("systemTwo") 
    val system = ActorSystem("mySystem", two) 
    //What should be there to access HelloActor? 
    val selection = ... 
    selection ! "mew" 
    println(selection.anchorPath) 
} 

配置文件

systemOne { 
    akka { 
    remote { 
     enabled-transports = ["akka.remote.netty.tcp"] 
     netty.tcp { 
     port = 2552 
     } 
    } 
    } 
} 

systemTwo { 
    akka { 
    remote { 
     enabled-transports = ["akka.remote.netty.tcp"] 
     netty.tcp { 
     port = 2553 
     } 
    } 
    } 
} 

它假设服务器开始之前客户端。现在,当我尝试获取像这样的演员时收到“akka:// mySystem/deadLetters”(println(selection.anchorPath)system.actorSelection("akka://[email protected]:2552/HelloActor")

那么如何在另一个JVM中从ActorSystem中检索actor?或者直到我创建一个群集,指定种子,领导者等等,这是不可能的?

回答

5

看来你错过了在配置中加入 RemoteActorRefProvider。你的演员配置应该像如下:

systemOne { 
    akka { 
    actor { 
     provider = "akka.remote.RemoteActorRefProvider" 
    } 
    remote { 
     enabled-transports = ["akka.remote.netty.tcp"] 
     netty.tcp { 
     port = 2552 
     } 
    } 
    } 
} 

systemTwo { 
    akka { 
    actor { 
     provider = "akka.remote.RemoteActorRefProvider" 
    } 
    remote { 
     enabled-transports = ["akka.remote.netty.tcp"] 
     netty.tcp { 
     port = 2553 
     } 
    } 
    } 
} 

演员的选择是使用

context.actorSelection("akka.tcp://[email protected]:2552/user/actorName") 

你并不需要创建一个阿卡集群,使远程处理能力来完成。

此配置更改RemoteActorRefProvider要求akka-remote作为依赖项。

相关问题