Reply is not transmitted back to the 'client'-actor

丶灬走出姿态 提交于 2020-01-02 10:02:58

问题


I've an unexpected behavior when using remote actors. I've a server and a 'client'. The client sends a message to the server actor and the server replies. When I use the '?' operator everything works as expected. I get the answer back from the server.

The server:

class HelloWorldActor extends Actor {
  def receive = {
    case msg => self reply (msg + " World")
  }
}

object Server extends App{
  Actor.remote.start("localhost",2552);
  Actor.remote.register("hello-service",Actor.actorOf[HelloWorldActor])
}

The client:

object Client extends App {
  // client code
  val actor = remote.actorFor(
    "hello-service", "localhost", 2552)
  val result = (actor ? "Hello").as[String]
  println(result)
}

Now I changed the code so that the client is an actor and just reacts to the answer. However not the reply isn't sent back to the client. Instead a 'ClientActor'-instance is created one the server and the reply is sent there?

The modified client:

class ClientActor extends Actor {
  def receive = {
    case "Ask" =>{
      val actor = remote.actorFor(
        "hello-service", "localhost", 2552)
      actor ! "Hello"
    }
    case response:String => println(response) // This executed on the server! That's not what I expect?
  }
}

object Client extends App {
  // client code
  val client = actorOf[ClientActor].start();
  client ! "Ask"
}

What am I missing? Is that the expected behavior of Akka? And how can I force it to send the answer back to the client?

Thanks for any input.


回答1:


You haven't started the remoting on the client.



来源:https://stackoverflow.com/questions/7674468/reply-is-not-transmitted-back-to-the-client-actor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!