Materializing and dematerializing signals

Sometimes, it is useful to process a stream not in terms of data, but in terms of signals. To convert a stream of data into a stream of signals and back again, Flux and Mono provide the materialize and dematerialize methods. An example of this is as follows:

Flux.range(1, 3)
    .doOnNext(e -> log.info("data  : {}", e))
    .materialize()
    .doOnNext(e -> log.info("signal: {}", e))
    .dematerialize()
    .collectList()
    .subscribe(r-> log.info("result: {}", r));

The preceding code produces the following output:

data  : 1
signal: onNext(1)
data  : 2
signal: onNext(2)
data  : 3
signal: onNext(3)
signal: onComplete()
result: [1, 2, 3]

Here, when processing the signal stream, the doOnNext method receives not only onNext events with data but also an onComplete event wrapped in the Signal class. This approach allows treating the onNext, onError, and onCompete events within one type hierarchy.

If we only want log signals without modifying them, Rector provides the log method, which uses the available logger to log all processed signals.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.93.175