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.
来源:https://stackoverflow.com/questions/7674468/reply-is-not-transmitted-back-to-the-client-actor