zip and flatMap not take effect

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/fu_huo_1993/article/details/100549370

The most recently used in the project Project Reactor, but found that it read, read, in some places do not achieve the desired effect, as some methods zip, flatMapsuch as not implemented, then this may be what causes it?

1, a simple example code is as follows:

public class FluxDemo {

    public static Mono<Void> doSomethind01() {
        return Flux.just(1, 2, 3, 5, 6).then();
    }

    public static Flux<Integer> doSomethind02() {
        return Flux.just(1, 2, 3, 5, 6);
    }

    public static void main(String[] args) throws InterruptedException {

        // 会有数据打印出来吗 ?
        doSomethind01()
                .zipWith(Mono.just(10))
                .flatMap(tuple -> Mono.just(tuple.getT2() + 10))
                .subscribe(i -> System.out.println("doSomethind01返回的是Mono<Void>所以不会执行到这一步"));

        // 会有数据打印出来吗 ?
        doSomethind02()
                .zipWith(Mono.just(10))
                .flatMap(tuple -> Mono.just(tuple.getT2() + 10))
                .subscribe(i -> System.out.println("result => " + i));

        TimeUnit.SECONDS.sleep(10);
    }
}

2, an example of operation results

In the above example, a first 1⃣️ 不会outputting content, but the first one has 2⃣️ 会输出content.

3, conclusion

When we return Mono.empty(), or Mono<Void>when some combination of operations, such as zip, zipWith, flatMapand other operations will not be executed. This is because each element requires a source to generate their output, while Mono.empty () it does not contain elements or Mono, so will not be executed.

Guess you like

Origin blog.csdn.net/fu_huo_1993/article/details/100549370