最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring - Unexpected Behavior When Using .take(1) in Reactor Flux - Stack Overflow

programmeradmin2浏览0评论

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:

  1. Why does adding .take(1) change the behavior?
  2. Why does map element b appear before signal doOnEach_onNext(b) in the output?

I would appreciate any insights or explanations regarding this behavior. Thank you!

发布评论

评论列表(0)

  1. 暂无评论