【问题标题】:Why can't I use concatWith after Flux initiation为什么我不能在 Flux 启动后使用 concatWith
【发布时间】:2021-08-03 14:09:21
【问题描述】:

我是新手,正在学习 Spring Flux。我无法理解以下内容:此处正在打印西瓜:

Flux<String> fruitFlux = Flux.just("Apple", "Banana", "Orange").concatWith(Flux.just("Watermelon"));
fruitFlux.subscribe( System.out::println);

但是当使用下面的代码时,它并没有被打印出来:

Flux<String> fruitFlux = Flux.just("Apple", "Banana", "Orange");
fruitFlux.concatWith(Flux.just("Watermelon"));
fruitFlux.subscribe( System.out::println);

这两个代码块不应该完全一样吗?

【问题讨论】:

    标签: spring-boot spring-webflux project-reactor


    【解决方案1】:

    否 - Flux 是不可变的,像 concatWith 这样的运算符会返回相应修改的 Flux 的新实例。它们不会改变原始发布者。

    在你的第二个例子中,你忽略了那个新实例,所以它没有任何效果。

    如果您改为使用fruitFlux = fruitFlux.concatWith(Flux.just("Watermelon"));,那么您会看到预期的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多