Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this simple Akka Streams program never terminate?

Tags:

akka-stream

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
  )
}
like image 569
Matthew Adams Avatar asked Feb 23 '16 17:02

Matthew Adams


People also ask

How does Akka stream work?

By default, Akka Streams will fuse the stream operators. This means that the processing steps of a flow or stream can be executed within the same Actor and has two consequences: passing elements from one operator to the next is a lot faster between fused operators due to avoiding the asynchronous messaging overhead.

Why Akka is reactive?

Akka is a powerful actor / reactive framework for the JVM. Akka is an extremely high-performance library — you can do Up to 50 million msg/sec on a single machine. Small memory footprint; ~2.5 million actors per GB of the heap. Akka is also resilient by Design and follows the principles of the Reactive Manifesto.

Is Akka single threaded?

In Akka, actors are guaranteed to be run in a single-threaded illusion, which means that the Akka framework takes care of threading issues while allowing us to focus on the behavior that needs to be implemented. Actors may only communicate with each other and the outside world by through messages.

What is materialized value in Akka stream?

The Akka Streams library calls them materialized values. That's because, when you plug components together, you have an inert graph, but when you call the run method, the graph comes alive, or is materialized. The Jedi value returned by materializing a graph is called a materialized value.


1 Answers

Because Akka uses non-daemonic threads, so it'll keep the app running until you shut-down the ActorSystem. That's because the typical use case is to start the system in your main method and then all computation happens on threads that the ActorSystem manages (i.e. the main() thread is left to complete and go away - if the threads would be daemonic the app would shut down, which is not what we usually want).

You can do so via:

import system.dispatcher
s.runForeach(println).onComplete { _ =>  system.terminate() }
like image 163
Konrad 'ktoso' Malawski Avatar answered Jun 24 '23 23:06

Konrad 'ktoso' Malawski