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中的远程actor。如何通过接收“停止”消息来关闭远程actor?
我知道exit()可能会直接退出服务器应用程序,但如果仍有请求需要处理,该怎么办呢。
关键是调用remote.shutdown()永远不会关闭远程服务(服务器应用程序),所以如果我想为演员停止服务器应用程序该怎么办
答案 0 :(得分:4)
演员可以通过调用self.stop
来自杀。所以忘记PoisonPill并只使用Stop
消息:
case "Stop" => {
println("stopping...")
self.stop
remote.shutdown()
}