I am new to Akka Stream
and have only started reading its docs as of last week. I am able to understand most of the concepts but I am finding it hard to understand what Materialized Value
means in Akka stream and what its significance is?.
If somebody could explain this to me with a real world example that would help a lot in co-relating it and its use cases in Akka Stream.
Update
I was going through below example and wanted to understand the two different kind of output that we get for the same graph. I know it has direct relation with the Materialized Value
and that's why I asked the above question.
implicit val system = ActorSystem("PlainSinkProducerMain")
implicit val materializer = ActorMaterializer()
val source = Source(1 to 10)
val sink = Sink.fold[Int, Int](0)(_ + _)
val runnable: RunnableGraph[Future[Int]] =
source.toMat(sink)(Keep.right)
val sum1: Future[Int] = runnable.run()
val sum2: Future[Int] = runnable.run()
println(sum1.value)
println(sum2.value)
When I run this I get below output :
None
Some(Success(55))
The above example is from the docs and below is the explanation of it.
Since a stream can be materialized multiple times, the materialized value will also be calculated anew for each such materialization, usually leading to different values being returned each time. In the example below we create two running materialized instance of the stream that we described in the runnable variable, and both materializations give us a different Future from the map even though we used the same sink to refer to the future.
I have 2 questions here: 1) Why the 2 different Materialized Instance of same graph have 2 different output values? Although the materialized value is calculated twice but under the hood both the calculation are for same graph.
2) Why the return type is Some(Success(55))
instead of a plain 55
for successful execution and Failure Exception
for failed cases? (This might be dumb questions but I want to understand what problem/advantage does this special return value solves/gives.)