如果我禁用A行,HelloRemote应用程序无法接收消息" hello world",如果我们关闭本地actor系统太快,该消息将不会被发送到远程?
当然,本地actor可以等待来自远程的消息来确认已经收到的消息,但是如果我们添加这样的代码,那么进程将是同步的,我不希望这个东西变成类似RPC的东西。
我只想让它异步,也不想浪费时间" Thread.sleep(3000)",我希望客户端尽快退出,有什么建议吗?
HelloRemote.scala
package remote
import akka.actor._
object HelloRemote extends App {
val system = ActorSystem("HelloRemoteSystem")
val remoteActor = system.actorOf(Props[RemoteActor], name = "RemoteActor")
}
class RemoteActor extends Actor {
def receive = {
case msg: String =>
println(s"received message '$msg'")
}
}
Local.scala
package local
import akka.actor._
object Local extends App {
val system = ActorSystem("LocalSystem")
val localActor = system.actorOf(Props[LocalActor], name = "LocalActor")
localActor ! "START"
}
class LocalActor extends Actor {
val remote = context.actorSelection(
"akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor")
def receive = {
case "START" =>
remote ! "hello world"
Thread.sleep(3000) // Line A
context.stop(self)
context.system.shutdown
}
}
另外,粘贴application.conf以便于尝试。
远程部分:
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
netty.tcp {
hostname = "127.0.0.1"
port = 5150
}
log-remote-lifecycle-events = off
}
log-dead-letters = off
log-dead-letters-during-shutdown = off
}
当地部分:
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
log-remote-lifecycle-events = off
}
log-dead-letters = off
log-dead-letters-during-shutdown = off
}
答案 0 :(得分:0)
作为最简单的事情,远程演员可以将毒丸送回当地客户。
像这样:
sender ! Kill
你可以在这里阅读http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Killing_an_Actor
或者你可以选择一些"秘密"告诉你的当地演员在停止之前做某事的消息。
简短的例子。远程部分:
class RemoteActor extends Actor {
def receive = {
case msg: String =>
println(s"received message '$msg'")
sender ! "secretWord"
}
}
本地部分:
class LocalActor extends Actor {
val remote = context.actorSelection(
"akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor")
def receive = {
case "START" =>
remote ! "hello world"
case "secretWord" =>
context.stop()
}
def postStop(): Unit = {
//do something here
}
}