2017-01-13 59 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())  
}