【问题标题】:correctly terminate akka actors in scala正确终止 scala 中的 akka 演员
【发布时间】:2012-09-08 03:32:30
【问题描述】:

我编写了启动一个actor、杀死它并完成执行的示例代码。

object PureAkka {
  def main(argv : Array[String]) = {
    val actorSystem : ActorSystem = ActorSystem("main")
    val actor : ActorRef = actorSystem.actorOf(Props( new Actor {
      override def receive = {
        case x => println(x)
      }
      override def preStart() = println("prestart")
      override def postStop() = println("poststop")
    } ) )
    Thread.sleep(15000)
    actor ! PoisonPill
  }
}

此代码打印:

[info] prestart
[info] poststop

但它拒绝停止,直到我用 Ctrl-C 终止进程

应用程序等待什么?我怎样才能以适当的方式阻止它?

【问题讨论】:

标签: scala actor akka


【解决方案1】:

也许调用 ActorSystem.shutdown() 就可以了。

根据akka docs:

抽象定义shutdown(): Unit

停止这个演员系统。这将停止监护 Actor,后者将递归地停止其所有子 Actor,然后是系统监护(记录 Actor 驻留在其下方)并执行所有已注册的终止处理程序(请参阅 ActorSystem.registerOnTermination)。

【讨论】:

  • 请注意:as of Akka 2.4 你应该使用ActorSystem.terminate()。这会返回一个 Future[Terminated] 你可能会等待。
猜你喜欢
  • 2013-06-16
  • 2019-01-01
  • 2014-07-11
  • 2019-04-23
  • 2014-06-13
  • 2018-12-15
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多