Using RemoteActors without future/ask

442 views Asked by At

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?

1

There are 1 answers

0
Roland Kuhn On

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.