0

如果我禁用 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
    }
4

1 回答 1

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
  }
}
于 2016-06-07T08:39:19.423 回答