3
object RemoteEchoServer extends App {
  remote.start("localhost", 1111)
  remote.register("hello-service", actorOf[HelloWorldActor])
}

object RemoteEchoClient extends App {
  val actor = remote.actorFor("hello-service", "localhost", 1111)
  val reply = actor !! "Hello"
  println(reply)
  actor ! "Stop"
  actor ! PoisonPill
}

/**
 * a remote actor servers for message "Hello" and response with a message "World"
 * it is silly
 */
class HelloWorldActor extends Actor {
  def receive = {
    case "Hello" =>
      println("receiving a Hello message,and a World message will rply")
      self.reply("World")
    case "Stop" =>
      println("stopping...")
      remote.shutdown()
  }
}

クライアントはPoisonPillと「停止」信号を送信しますが、リモートはそれ自体を終了しません。remote.shutdown() を呼び出して、オブジェクト RemoteEchoServer 内のリモート アクターを強制終了する必要があります。「停止」メッセージを受信して​​リモート アクターをシャットダウンする方法は?

exit() はおそらくサーバーアプリを直接終了することを知っていますが、まだ処理が必要なリクエストがある場合はどうでしょうか。

重要なポイントは、remote.shutdown() を呼び出してリモート サービス (サーバー アプリ) をシャットダウンしないことです。アクターのサーバー アプリを停止したい場合はどうすればよいですか?

4

1 に答える 1

4

アクターは、 を呼び出すことで自分自身を殺すことができself.stopます。したがって、PoisonPill を忘れて、次のStopメッセージを使用してください。

case "Stop" => {
  println("stopping...")
  self.stop
  remote.shutdown()
}
于 2011-06-30T10:39:15.290 に答える