如何从主方法终止Akka actor系统?

时间:2017-01-13 16:34:14

标签: scala akka akka-stream akka-http reactivemongo

我使用Akka Streams和ReactiveMongo进行this申请。没有用户定义的actor。该应用程序是从main方法启动的。

问题是main方法完成后JVM会继续运行。这就是我现在正在做的事情:

val g = (file: String) => RunnableGraph.fromGraph(GraphDSL.create(Sink.ignore) {
  implicit builder =>
    sink =>
      import GraphDSL.Implicits._

      // Source
      val A: Outlet[(String, String)] = builder.add(Source.fromIterator(() => parseMovies(file).iterator)).out
      // Flow
      val B: FlowShape[(String, String), Either[String, Movie]] = builder.add(findMovie)
      // Flow
      val C: FlowShape[Either[String, Movie], Option[String]] = builder.add(persistMovie)

      A ~> B ~> C ~> sink.in

      ClosedShape
})

def main(args: Array[String]): Unit = {
  require(args.size >= 1, "Path to file is required.")

  g(args(0)).run
    .onComplete(_ => Await.result(system.terminate(), 5.seconds))
}

我已阅读this帖子和this,其中没有一个可以使用。 system.shutdown已弃用,我没有任何明确的演员值得关注。我可以致电system.exit,但这并不优雅。

从日志中可以看出,Akka正试图关闭,但后来我看到了一堆Mongo消息。

2017-01-13 11:35:57.320 [DEBUG] a.e.EventStream.$anonfun$applyOrElse$4 - shutting down: StandardOutLogger started
2017-01-13 11:36:05.397 [DEBUG] r.c.a.MongoDBSystem.debug - [Supervisor-1/Connection-2] ConnectAll Job running... Status: {{NodeSet None Node[localhost:27017: Primary (10/10 available connections), latency=6], auth=Set() }}
2017-01-13 11:36:05.420 [DEBUG] r.c.a.MongoDBSystem.debug - [Supervisor-1/Connection-2] RefreshAll Job running... Status: {{NodeSet None Node[localhost:27017: Primary (10/10 available connections), latency=6], auth=Set() }}
// more of MongoDBSystem.debug messages

为什么赢得 it.just.die

1 个答案:

答案 0 :(得分:0)

我想你想添加一个关闭钩子或者调用actorSystem.registerOnTermination(driver.close())

def main(args: Array[String]): Unit = {
  import akka.actor.CoordinatedShutdown
  require(args.size >= 1, "Path to file is required.")

  CoordinatedShutdown(system).addTask(CooordinatedShutdown.PhaseBeforeActorSystemTerminate, "shutdownMongoDriver") { () => driver.close(5.seconds); Future.successful(Done) }

  g(args(0)).run.onComplete(_ => CoordinatedShutdown(system).run())    
}