Why does this simple Akka Streams program never end?

There should be a simple question. I am using Akka 2.4.2 (contains Akka and HTTP streams). I expected this Source to end, and the program will end, because Source course, but it never will be. Why does this program not end?

 import scala.concurrent._ import scala.collection.immutable._ import akka._ import akka.actor._ import akka.stream._ import akka.stream.scaladsl._ import akka.util._ object Test extends App { implicit val system = ActorSystem("TestSystem") implicit val materializer = ActorMaterializer() val s = Source.single(1) s.runForeach(println) } 

Output:

 $ sbt run ... [info] Running Test [DEBUG] [02/23/2016 10:59:19.904] [run-main-0] [EventStream(akka://TestSystem)] logger log1-Logging$DefaultLogger started [DEBUG] [02/23/2016 10:59:19.904] [run-main-0] [EventStream(akka://TestSystem)] Default Loggers started 1 

The corresponding part of my build.sbt file build.sbt :

 scalaVersion := "2.11.7" scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8") libraryDependencies ++= { val akkaVersion = "2.4.2" Seq( "com.typesafe.akka" %% "akka-stream" % akkaVersion ) } 
+7
akka-stream
source share
2 answers

Since Akka uses non-demonic threads , so the application will work until you disable ActorSystem . This is because a typical use case is to simply start the system in your main method, and then all the calculations take place in the threads controlled by the ActorSystem (i.e., the main() thread remains finished and leaves) if the threads are closed by a demonic application, which we usually don’t want).

You can do this via:

 import system.dispatcher s.runForeach(println).onComplete { _ => system.terminate() } 
+9
source share

Reply to https://groups.google.com/d/msg/akka-user/u3MXlfVpm40/LHyYruS0HgAJ https://stackoverflow.com/users/111024/konrad-ktoso-malawski , copied here for convenience; credit to him:

Because Akka uses non-demonic threads, so the application will work until you disable ActorSystem.

You can do this via:

 import system.dispatcher s.runForeach(println).onComplete { _ => system.terminate() } 
0
source share

All Articles