Why does this simple Akka Streams program never terminate?

201 views
Skip to first unread message

Matthew Adams

unread,
Feb 23, 2016, 12:11:31 PM2/23/16
to Akka User List

Should be a simple question. I'm using Akka 2.4.2 (contains Akka Streams & HTTP). I expected this Source to complete & the program to terminate because the Source is finite, but it never does. Why doesn't this program terminate?

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

Relevant portion of my build.sbt file:

scalaVersion  := "2.11.7"

scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8")

libraryDependencies ++= {
  val akkaVersion = "2.4.2"
  Seq(
    "com.typesafe.akka" %%  "akka-stream" % akkaVersion
  )
}

-matthew

Konrad Malawski

unread,
Feb 23, 2016, 12:13:37 PM2/23/16
to akka...@googlegroups.com, Matthew Adams
Because Akka uses non-daemonic threads, so it'll keep the app running until you shut-down the ActorSystem.

You can do so via:

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

-- 
Cheers,
Konrad 'ktoso’ Malawski
Akka @ Lightbend
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Matthew Adams

unread,
Feb 23, 2016, 12:18:14 PM2/23/16
to Akka User List, mat...@matthewadams.me
That was it.  Good to know.  Thanks.

-matthew

Endre Varga

unread,
Feb 23, 2016, 12:48:23 PM2/23/16
to akka...@googlegroups.com, mat...@matthewadams.me
Also, the stream itself runs in a separate thread than main(), so runForeach returns before the stream has been processed.

-Endre
Reply all
Reply to author
Forward
0 new messages