Skip to content

Instantly share code, notes, and snippets.

@DavidRdgz
Created February 22, 2016 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidRdgz/5fa73fb3ea0d5b9c4850 to your computer and use it in GitHub Desktop.
Save DavidRdgz/5fa73fb3ea0d5b9c4850 to your computer and use it in GitHub Desktop.
A simple example of two akka streams defined with the same source returning two different Futures.
package com.dvidr
import scala.concurrent.ExecutionContext.Implicits.global
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{RunnableGraph, Sink, Source, Keep}
import scala.concurrent.Future
import scala.util.{Success, Failure}
class TwoAkkaStreams
object TwoAkkaStreams {
def main(args: Array[String]) {
implicit val actorSystem = ActorSystem("TwoSimpleStreams")
implicit val materializer = ActorMaterializer()
val sink = Sink.fold[Int, Int](0)(_ + _)
val runnable: RunnableGraph[Future[Int]] =
Source(1 to 10).toMat(sink)(Keep.right)
val sum1: Future[Int] = runnable.run()
val sum2: Future[Int] = runnable.run()
sum1.onComplete{
case Success(n) => println(n)
case Failure(n) => println("Akka Actor 1 failure.")
}
sum2.onComplete{
case Success(n) => println(n)
case Failure(n) => println("Akka Actor 2 failure.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment