monitor
Materializes to a FlowMonitor
that monitors messages flowing through or completion of the operators.
Signature
Description
Materializes to a FlowMonitor
that monitors messages flowing through or completion of the stream. Elements pass through unchanged. Note that the FlowMonitor
inserts a memory barrier every time it processes an event, and may therefore affect performance. The provided FlowMonitor
contains a state
field you can use to peek and get information about the stream.
Example
The example below uses the monitorMat
variant of monitor
. The only difference between the two operators is that monitorMat
has a combine
argument so we can decide which materialization value to keep. In the sample below be Keep.right
so only the FlowMonitor[Int]
is returned.
- Scala
-
source
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))
- Java
When run, the sample code will produce something similar to:
Stream is initialized but hasn't processed any element
0
1
2
Last element processed: 2
3
4
5
Stream completed already
Reactive Streams semantics
emits when upstream emits an element
backpressures when downstream backpressures
completes when upstream completes