回复不会传输回“客户端”参与者
使用远程演员时,我出现了意外的行为。我有一个服务器和一个“客户端”。客户端向服务器参与者发送消息,服务器回复。当我使用“?”时操作员一切都按预期进行。我从服务器得到答案。
服务器:
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])
}
客户端:
object Client extends App {
// client code
val actor = remote.actorFor(
"hello-service", "localhost", 2552)
val result = (actor ? "Hello").as[String]
println(result)
}
现在我更改了代码,以便客户端成为参与者并且只对答案做出反应。但是,回复不会发送回客户端。相反,在服务器上创建一个“ClientActor”实例并将回复发送到那里?
修改后的客户端:
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"
}
我缺少什么?这是 Akka 的预期行为吗?我怎样才能强迫它把答案发回给客户端呢?
感谢您的任何意见。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尚未在客户端上启动远程处理。
You haven't started the remoting on the client.