Merge pull request #29356 from ignasi35/monitor-operator-sample-code

This commit is contained in:
Ignasi Marimon-Clos 2020-07-13 19:33:36 +02:00 committed by GitHub
commit 68fa87c58c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 152 additions and 5 deletions

View file

@ -0,0 +1,51 @@
/*
* Copyright (C) 2020 Lightbend Inc. <https://www.lightbend.com>
*/
package docs.stream.operators.sourceorflow
import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.FlowMonitor
import akka.stream.FlowMonitorState
import akka.stream.scaladsl.Keep
import akka.stream.scaladsl.Sink
import akka.stream.scaladsl.Source
import scala.concurrent.ExecutionContextExecutor
import scala.concurrent.duration._
class Monitor {
implicit val system = ActorSystem("monitor-sample-sys2")
implicit val executionContext: ExecutionContextExecutor = system.dispatcher
// #monitor
val source: Source[Int, NotUsed] =
Source.fromIterator(() => Iterator.from(0))
def printMonitorState(flowMonitor: FlowMonitor[Int]) =
flowMonitor.state match {
case FlowMonitorState.Initialized =>
println("Stream is initialized but hasn't processed any element")
case FlowMonitorState.Received(msg) =>
println(s"Last element processed: $msg")
case FlowMonitorState.Failed(cause) =>
println(s"Stream failed with cause $cause")
case FlowMonitorState.Finished => println(s"Stream completed already")
}
val monitoredSource: Source[Int, FlowMonitor[Int]] = source.take(6).throttle(5, 1.second).monitorMat(Keep.right)
val (flowMonitor, futureDone) =
monitoredSource.toMat(Sink.foreach(println))(Keep.both).run()
// If we peek in the monitor too early, it's possible it was not initialized yet.
printMonitorState(flowMonitor)
// Periodically check the monitor
Source.tick(200.millis, 400.millis, "").runForeach(_ => printMonitorState(flowMonitor))
// #monitor
futureDone.onComplete(_ => system.terminate())
}