在没有 future/ask 的情况下使用 RemoteActors
我想从客户端的 Actor 异步发送一条消息(!),并从服务器向 Actor 返回一条消息。关键是我不想使用(?)并获得Future。
我有以下示例代码:
import akka.actor.Actor._
import akka.event.EventHandler
import akka.actor.{ActorRef, Actor}
case class FromUser(s: String)
case class FromServer(s: String)
class ServerActor extends Actor {
protected def receive = {
case FromUser(msg) => self.sender ! FromServer(msg)
}
}
class ClientActor(val remoteServer: ActorRef) extends Actor {
protected def receive = {
case FromUser(msg) => {
EventHandler.info(this, "I got '" + msg + "' from the user.")
remoteServer ! FromUser(msg)
}
case FromServer(msg) => EventHandler.info(this, "I got '" + msg + "' from the server.")
}
}
object Client {
def main(args: Array[String]) {
val s = Actor.remote.actorFor("ServerActor", "NYCWD2328", 4552)
val c = Actor.actorOf(new ClientActor(s)).start
c ! FromUser("Hello")
}
}
object Server {
def main(args: Array[String]) {
Actor.remote.start("NYCWD2328", 4552)
Actor.remote.register("ServerActor", actorOf(new ServerActor))
}
}
此代码失败并显示以下跟踪:
[GENERIC] [11/9/11 12:32 PM] [RemoteClientWriteFailed(uuid {
high: 15711794799146701281
low: 10512246108465469656
}
actorInfo {
....
timeout: 5000
}
,java.nio.channels.ClosedChannelException,akka.remote.netty.NettyRemoteSupport@a1d1f4,/127.0.0.1:2552)]
[GENERIC] [11/9/11 12:32 PM] [RemoteClientError(java.nio.channels.ClosedChannelException,akka.remote.netty.NettyRemoteSupport@a1d1f4,/127.0.0.1:2552)]
[ERROR] [11/9/11 12:32 PM] [akka:event-driven:dispatcher:global-3] [LocalActorRef] null
java.nio.channels.ClosedChannelException
at org.jboss.netty.channel.socket.nio.NioWorker.cleanUpWriteBuffer(NioWorker.java:643)
这似乎应该可以做到。是吗?
I'd like to send a message from an Actor on the client side asynchronously (!) and return a message to the Actor from the server. The key is that I do not want to use (?) and get a Future.
I've got the following example code:
import akka.actor.Actor._
import akka.event.EventHandler
import akka.actor.{ActorRef, Actor}
case class FromUser(s: String)
case class FromServer(s: String)
class ServerActor extends Actor {
protected def receive = {
case FromUser(msg) => self.sender ! FromServer(msg)
}
}
class ClientActor(val remoteServer: ActorRef) extends Actor {
protected def receive = {
case FromUser(msg) => {
EventHandler.info(this, "I got '" + msg + "' from the user.")
remoteServer ! FromUser(msg)
}
case FromServer(msg) => EventHandler.info(this, "I got '" + msg + "' from the server.")
}
}
object Client {
def main(args: Array[String]) {
val s = Actor.remote.actorFor("ServerActor", "NYCWD2328", 4552)
val c = Actor.actorOf(new ClientActor(s)).start
c ! FromUser("Hello")
}
}
object Server {
def main(args: Array[String]) {
Actor.remote.start("NYCWD2328", 4552)
Actor.remote.register("ServerActor", actorOf(new ServerActor))
}
}
This code fails with the following trace:
[GENERIC] [11/9/11 12:32 PM] [RemoteClientWriteFailed(uuid {
high: 15711794799146701281
low: 10512246108465469656
}
actorInfo {
....
timeout: 5000
}
,java.nio.channels.ClosedChannelException,akka.remote.netty.NettyRemoteSupport@a1d1f4,/127.0.0.1:2552)]
[GENERIC] [11/9/11 12:32 PM] [RemoteClientError(java.nio.channels.ClosedChannelException,akka.remote.netty.NettyRemoteSupport@a1d1f4,/127.0.0.1:2552)]
[ERROR] [11/9/11 12:32 PM] [akka:event-driven:dispatcher:global-3] [LocalActorRef] null
java.nio.channels.ClosedChannelException
at org.jboss.netty.channel.socket.nio.NioWorker.cleanUpWriteBuffer(NioWorker.java:643)
This seems like it should be possible to do. Is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个老问题,Viktor 已经回答了它,但我不喜欢“无答案”状态,因此为了完整性:应将 main 方法更改为
Thestartedremoteservice(主机和端口)必须可从远程访问机,此处为“NYCWD2328”,因为回复将通过该远程主机发起的新连接发送。
It’s an old question, and Viktor already answered it, but I don’t like the “no answer” state, so for completeness: the main method should be changed into
The started remote service (host and port) must be reachable from the remote machine, here "NYCWD2328", because replies will be sent through a new connection initiated by that remote host.