PR comments
This commit is contained in:
parent
f187abddd1
commit
6c12ae117a
3 changed files with 13 additions and 27 deletions
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
package docs.stream.operators.sourceorflow
|
||||
|
||||
import akka.Done
|
||||
import akka.NotUsed
|
||||
import akka.actor.ActorSystem
|
||||
import akka.stream.FlowMonitor
|
||||
|
|
@ -14,12 +13,8 @@ import akka.stream.scaladsl.Sink
|
|||
import akka.stream.scaladsl.Source
|
||||
|
||||
import scala.concurrent.ExecutionContextExecutor
|
||||
import scala.concurrent.Future
|
||||
import scala.concurrent.duration._
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Monitor {
|
||||
|
||||
implicit val system = ActorSystem("monitor-sample-sys2")
|
||||
|
|
@ -29,7 +24,7 @@ class Monitor {
|
|||
val source: Source[Int, NotUsed] =
|
||||
Source.fromIterator(() => Iterator.from(0))
|
||||
|
||||
def printMonitorState(flowMonitor: FlowMonitor[Int]) = {
|
||||
def printMonitorState(flowMonitor: FlowMonitor[Int]) =
|
||||
flowMonitor.state match {
|
||||
case FlowMonitorState.Initialized =>
|
||||
println("Stream is initialized but hasn't processed any element")
|
||||
|
|
@ -39,21 +34,18 @@ class Monitor {
|
|||
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 monitoredStream: (FlowMonitor[Int], Future[Done]) =
|
||||
val (flowMonitor, futureDone) =
|
||||
monitoredSource.toMat(Sink.foreach(println))(Keep.both).run()
|
||||
|
||||
val flowMonitor = monitoredStream._1
|
||||
|
||||
// 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, "").map(_ => printMonitorState(flowMonitor)).to(Sink.ignore).run
|
||||
Source.tick(200.millis, 400.millis, "").runForeach(_ => printMonitorState(flowMonitor))
|
||||
|
||||
// #monitor
|
||||
monitoredStream._2.onComplete(_ => system.terminate())
|
||||
futureDone.onComplete(_ => system.terminate())
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue