加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

正确终止scala中的akka?? actor

发布时间:2020-12-16 09:59:36 所属栏目:安全 来源:网络整理
导读:我编写了启动actor的示例代码,杀死它并完成执行. object PureAkka { def main(argv : Array[String]) = { val actorSystem : ActorSystem = ActorSystem("main") val actor : ActorRef = actorSystem.actorOf(Props( new Actor { override def receive = { c
我编写了启动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杀死进程

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

解决方法

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

根据akka docs:

abstract def shutdown(): Unit

Stop this actor system. This will stop the guardian actor,which in turn will recursively stop all its child actors,then the system guardian (below which the logging actors reside) and the execute all registered termination handlers (see ActorSystem.registerOnTermination).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读