开发者

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

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.


You haven't started the remoting on the client.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜