Created
February 22, 2016 19:16
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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