I encountered an unexpected behavior when using .take(1)
in my Reactor Flux
pipeline. Below is the code snippet I used:
Flux.just("", "b")
.doOnEach(signal -> {
System.out.println("signal " + signal);
})
.filter(StringUtils::isNotBlank)
.map(e -> {
System.out.println("map element " + e);
return e;
})
.subscribe();
Expected Behavior
Without .take(1)
, the output is as follows:
signal doOnEach_onNext()
signal doOnEach_onNext(b)
map element b
signal onComplete()
This behavior aligns with my expectations.
Unexpected Behavior
However, when I add .take(1)
, like this:
Flux.just("", "b")
.doOnEach(signal -> {
System.out.println("signal " + signal);
})
.filter(StringUtils::isNotBlank)
.map(e -> {
System.out.println("map element " + e);
return e;
})
.take(1)
.subscribe();
The output changes to:
map element b
signal doOnEach_onNext(b)
This is different from the previous case, and I have two questions:
- Why does adding
.take(1)
change the behavior? - Why does
map element b
appear beforesignal doOnEach_onNext(b)
in the output?
I would appreciate any insights or explanations regarding this behavior. Thank you!