Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. package sample.stream
  2.  
  3. import akka.actor.ActorSystem
  4. import akka.stream.ActorMaterializer
  5. import akka.stream.scaladsl._
  6.  
  7. import scala.concurrent.Future
  8.  
  9. object SimpleStreamExample {
  10.  
  11. def main(args: Array[String]): Unit = {
  12. implicit val system = ActorSystem("Sys")
  13. import system.dispatcher
  14.  
  15. implicit val materializer = ActorMaterializer()
  16.  
  17. val numbers = 1 to 1000
  18.  
  19. //We create a Source that will iterate over the number sequence
  20. val numbersSource: Source[Int, Unit] = Source.fromIterator(() => numbers.iterator)
  21.  
  22. //Only let pass even numbers through the Flow
  23. val isEven: Flow[Int, Int, Unit] = Flow[Int].filter((num) => num % 2 == 0)
  24.  
  25. //Create a Source of even random numbers by combining the random number Source with the even number filter Flow
  26. val evenNumbers: Source[Int, Unit] = numbersSource.via(isEven)
  27.  
  28. //A Sink that will print out the data to the console output
  29. val consoleSink: Sink[Int, Future[Unit]] = Sink.foreach[Int](println)
  30.  
  31. //Attach the evenNumbers Source with the console Sink. When the Sink is done shutdown the Actor System
  32. evenNumbers.runWith(consoleSink).onComplete(_ => system.shutdown())
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement