Skip to content

Instantly share code, notes, and snippets.

@rocketpages
Last active December 12, 2017 22:16
Show Gist options
  • Save rocketpages/ed514d309da87711c821 to your computer and use it in GitHub Desktop.
Save rocketpages/ed514d309da87711c821 to your computer and use it in GitHub Desktop.
object GraphExample {
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl._
import FlowGraph.Implicits._
import scala.util.{ Failure, Success }
import scala.concurrent.ExecutionContext.Implicits._
implicit val system = ActorSystem("Sys")
implicit val materializer = ActorMaterializer()
def main(args: Array[String]): Unit = {
val out = Sink.foreach(println)
val g = FlowGraph.closed(out) { implicit builder =>
sink =>
val in = Source(1 to 10)
val bcast = builder.add(Broadcast[Int](2))
val merge = builder.add(Merge[Int](2))
val f1, f2, f3, f4 = Flow[Int].map(_ + 10)
in ~> f1 ~> bcast ~> f2 ~> merge ~> f3 ~> sink.inlet
bcast ~> f4 ~> merge
}.run()
// ensure the output file is closed and the system shutdown upon completion
g.onComplete {
case Success(_) =>
system.shutdown()
case Failure(e) =>
println(s"Failure: ${e.getMessage}")
system.shutdown()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment