2017-01-13 33 views
1

我有this应用程序使用Akka Streams和ReactiveMongo。没有用户定义的演员。该应用程序从main方法启动。如何从主要方法中终止Akka actor系统?

问题是在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

+2

你应该正确地关闭了'MongoDriver',因为它管理自己的底层演员系统,你不应该(也不能)访问 – cchantep

+0

@cchantep你能更具体还是展示代码示例?关闭Mongo连接并没有帮助。 –

+0

如果你仔细阅读我提到的'MongoDriver',而不是'MongoConnection' – cchantep

回答

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