如果我禁用 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'")
}
}
本地.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
}