Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inspect a Flux stream by an operator

I try to inspect an existing stream but couldn't find a special operator so far.

Flux.just(1, 2, 3)
    // just inspect every element
    .map(e -> {
        System.out.println(e);
        return e;
    })
    .subscribe();

I'm looking for an operator like peek() of Java Streams or tap() in RxJS.

like image 885
sschmeck Avatar asked Sep 12 '25 10:09

sschmeck


1 Answers

As you comment, doOnNext(System.out::println) will just print every element, which may be enough for simple use cases. (In general, doOnNext() is used for side effects, so equivalent to tap() in that sense.)

However, if debugging is your primary purpose, you may also want to look at log(), which gives you more information that could be useful (such as notifying you of subscriptions, requests, etc. as well as each element, and how they tie together.) For example:

Flux.just(1, 2, 3)
        .log()
        .subscribe();

Prints:

[ INFO] (main) | onSubscribe([Synchronous Fuseable] FluxArray.ArraySubscription)
[ INFO] (main) | request(unbounded)
[ INFO] (main) | onNext(1)
[ INFO] (main) | onNext(2)
[ INFO] (main) | onNext(3)
[ INFO] (main) | onComplete()
like image 175
Michael Berry Avatar answered Sep 15 '25 00:09

Michael Berry