title: How to terminate Akka actor system from a main method?
body: <p>I've <a href="https://github.com/asarkar/akka/tree/master/movie-database" rel="nofollow noreferrer">this</a> application using Akka Streams and ReactiveMongo. There are no user defined actors. The application is launched from a <code>main</code> method.</p>
  
  <p>Problem is the JVM continues to run forever after the <code>main</code> method has completed. This is what I'm doing now:</p>
  
  <pre><code>val g = (file: String) =&gt; RunnableGraph.fromGraph(GraphDSL.create(Sink.ignore) {
    implicit builder =&gt;
      sink =&gt;
        import GraphDSL.Implicits._
  
        // Source
        val A: Outlet[(String, String)] = builder.add(Source.fromIterator(() =&gt; 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 ~&gt; B ~&gt; C ~&gt; sink.in
  
        ClosedShape
  })
  
  def main(args: Array[String]): Unit = {
    require(args.size &gt;= 1, "Path to file is required.")
  
    g(args(0)).run
      .onComplete(_ =&gt; Await.result(system.terminate(), 5.seconds))
  }
  </code></pre>
  
  <p>I've read <a href="https://stackoverflow.com/questions/12436397/how-to-wait-for-akka-actor-system-to-terminate">this</a> thread, and <a href="https://stackoverflow.com/questions/33481156/how-to-stop-gracefully-the-actor-system-for-an-akka-http-server-that-must-be-dep">this</a>, none of which work. <code>system.shutdown</code> is deprecated and I don't have any explicit actors to watch for. I can call <code>system.exit</code> but that's hardly graceful.</p>
  
  <p>From the logs, it appears that Akka is trying to shut down but then I see a bunch of Mongo messages.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Why won't <em>it.just.die</em>?</p>