仅通过回复回复远程客户端?
根据 akka actor 文档,可以使用 回复自频道!消息
,以便代码可以在本地运行。我想对远程演员做同样的事情。
我有:
class ServerActor extends Actor {
def receive = {
case "Hello" =>
self.channel ! "World"
}
}
并且
class ClientActor extends Actor {
val remote = ...
def receive = {
case "Start" =>
remote ! "Hello"
case "World" =>
println("World received")
}
}
只要 ServerActor
接收“Hello”并向 ClientActor
发送“World”消息即可。不幸的是,接收消息的 ClientActor 似乎是在服务器 VM 中创建的,而不是实际发送消息的(在客户端 VM 中)。
有办法让这项工作发挥作用吗?
PS:当我进行自我回复“World”
和remote时,它会起作用吗? “你好”
,但是,我宁愿发送消息而不是回复。
编辑:谢谢大家。在两端启动远程处理是问题所在。其他发现这个问题的人要小心: 当使用让客户端以非阻塞方式接收响应时(例如不使用远程?请求),在收到关闭消息后立即关闭它们,会导致一些奇怪的行为(在我的评论中提到)以下);可能是由于设计原因(akka 的让它失败的容错能力?)。由于客户端不等待响应,因此在收到关闭消息后立即关闭它们将导致以下结果(在 akka-1.2 上):由于“原始客户端”不再存在(但往返“仍在进行中”) )它们在客户端和服务器上都重新启动(奇怪的是)。
According to the akka actor documentation one can reply using self.channel ! Message
so the code will work locally. I would like to do the same with remote actors.
I have:
class ServerActor extends Actor {
def receive = {
case "Hello" =>
self.channel ! "World"
}
}
and
class ClientActor extends Actor {
val remote = ...
def receive = {
case "Start" =>
remote ! "Hello"
case "World" =>
println("World received")
}
}
This works in so far as the ServerActor
receives the "Hello" and sends a "World" message to a ClientActor
. Unfortunately, it seems that the ClientActor
receiving the message is one that is created in the servers VM, not the one that actually sent it (in the client VM).
Is there a way to make this work?
PS: It works when I do a self reply "World"
and remote ? "Hello"
, however, I would rather send a message than replying.
EDIT: Thanks to everyone. Starting remoting on both ends was the problem. Others finding this question beware:
When using letting clients receive their responses in a non-blocking manner (as in not using remote ? request
), shutting them down immediately on receiving a shutdown message, will cause some strange behavior (mentioned in my comments below); possibly by design due (to akka's let-it-fail fault-tolerance?). As clients are not waiting for a response shutting them down immediately on receiving a shutdown message will result in the following (on akka-1.2): Since the "original clients" no longer exist (but the round-trip "is still in progress") they are restarted --- strangely --- both on the client and the server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这与我遇到的问题相同。当您想要从服务器接收消息时,您还需要在客户端上启动服务器实例。
例外情况是当您明确使用问号运算符询问结果时。
I think it is the same problem I had. You need to start up a server instance on the client aswell when you want receive messages from the server.
The exception is when you're explicitly are asking for a result with question-mark operator.