* doc: Broadcast Stream operator, #25468 * example intro text
This commit is contained in:
parent
35dbfd7883
commit
6185fbde18
3 changed files with 177 additions and 0 deletions
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package docs.stream.operators
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom
|
||||
|
||||
import scala.concurrent.Future
|
||||
|
||||
import akka.actor.ActorSystem
|
||||
import akka.stream.scaladsl.Broadcast
|
||||
import akka.stream.scaladsl.Keep
|
||||
|
||||
object BroadcastDocExample {
|
||||
|
||||
implicit val system: ActorSystem = ???
|
||||
|
||||
//#broadcast
|
||||
import akka.NotUsed
|
||||
import akka.stream.ClosedShape
|
||||
import akka.stream.scaladsl.Flow
|
||||
import akka.stream.scaladsl.GraphDSL
|
||||
import akka.stream.scaladsl.RunnableGraph
|
||||
import akka.stream.scaladsl.Sink
|
||||
import akka.stream.scaladsl.Source
|
||||
|
||||
val source: Source[Int, NotUsed] =
|
||||
Source.fromIterator(() => Iterator.continually(ThreadLocalRandom.current().nextInt(100))).take(100)
|
||||
|
||||
val countSink: Sink[Int, Future[Int]] = Flow[Int].toMat(Sink.fold(0)((acc, elem) => acc + 1))(Keep.right)
|
||||
val minSink: Sink[Int, Future[Int]] = Flow[Int].toMat(Sink.fold(0)((acc, elem) => math.min(acc, elem)))(Keep.right)
|
||||
val maxSink: Sink[Int, Future[Int]] = Flow[Int].toMat(Sink.fold(0)((acc, elem) => math.max(acc, elem)))(Keep.right)
|
||||
|
||||
val (count: Future[Int], min: Future[Int], max: Future[Int]) =
|
||||
RunnableGraph
|
||||
.fromGraph(GraphDSL.create(countSink, minSink, maxSink)(Tuple3.apply) {
|
||||
implicit builder => (countS, minS, maxS) =>
|
||||
import GraphDSL.Implicits._
|
||||
val broadcast = builder.add(Broadcast[Int](3))
|
||||
source ~> broadcast
|
||||
broadcast.out(0) ~> countS
|
||||
broadcast.out(0) ~> minS
|
||||
broadcast.out(0) ~> maxS
|
||||
ClosedShape
|
||||
})
|
||||
.run()
|
||||
//#broadcast
|
||||
|
||||
//#broadcast-async
|
||||
RunnableGraph.fromGraph(GraphDSL.create(countSink, minSink, maxSink)(Tuple3.apply) {
|
||||
implicit builder => (countS, minS, maxS) =>
|
||||
import GraphDSL.Implicits._
|
||||
val broadcast = builder.add(Broadcast[Int](3))
|
||||
source ~> broadcast
|
||||
broadcast.out(0) ~> Flow[Int].async ~> countS
|
||||
broadcast.out(0) ~> Flow[Int].async ~> minS
|
||||
broadcast.out(0) ~> Flow[Int].async ~> maxS
|
||||
ClosedShape
|
||||
})
|
||||
//#broadcast-async
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue