首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从一个主要的方法终止Akka演员系统?

如何从一个主要的方法终止Akka演员系统?
EN

Stack Overflow用户
提问于 2017-01-13 16:34:14
回答 1查看 1.4K关注 0票数 2

我已经使用了Akka和ReactiveMongo的ReactiveMongo应用程序。没有用户定义的参与者。应用程序是从main方法启动的。

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

代码语言:javascript
复制
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打电话,但这不太优雅。

从日志上看,阿克卡似乎试图关闭,但我看到了一堆蒙戈消息。

代码语言:javascript
复制
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不

EN

回答 1

Stack Overflow用户

发布于 2017-10-26 01:20:36

我认为您想要添加一个关机钩子或调用actorSystem.registerOnTermination(driver.close())

代码语言:javascript
复制
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())    
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41639308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档