在没有 future/ask 的情况下使用 RemoteActors

发布于 2024-12-14 15:55:36 字数 1816 浏览 0 评论 0原文

我想从客户端的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知你几分 2024-12-21 15:55:36

这是一个老问题,Viktor 已经回答了它,但我不喜欢“无答案”状态,因此为了完整性:应将 main 方法更改为

object Client {
  def main(args: Array[String]) {
    // choose available port on local machine
    Actor.remote.start("YourHostnameHere", 4553)
    val s = Actor.remote.actorFor("ServerActor", "NYCWD2328", 4552)
    val c = Actor.actorOf(new ClientActor(s)).start
    c ! FromUser("Hello")
  }
}

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

object Client {
  def main(args: Array[String]) {
    // choose available port on local machine
    Actor.remote.start("YourHostnameHere", 4553)
    val s = Actor.remote.actorFor("ServerActor", "NYCWD2328", 4552)
    val c = Actor.actorOf(new ClientActor(s)).start
    c ! FromUser("Hello")
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文