如何从主方法终止 Akka 演员系统



我有这个使用Akka Streams和ReactiveMongo的应用程序。没有用户定义的参与者。应用程序从main方法启动。

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

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))
}

我已经阅读了这个线程,这个,都不起作用。 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

为什么它不会死

我认为您想添加一个关机钩子或调用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())    
}

最新更新