【问题标题】:Unexpected behaviour while using then with Mono<Void>与 Mono<Void> 一起使用 then 时出现意外行为
【发布时间】:2019-06-21 14:40:22
【问题描述】:

当使用 .then(Mono&lt;Void&gt;) 组合许多 Mono&lt;Void&gt;s 时,它们不会按预期顺序运行。

有人可以解释下面的工作代码和非工作代码之间的区别吗?

工作代码

StepVerifier.create(
        repository.incrementCounter(bucket, timeStamp)
                .then(repository.incrementCounter(bucket, timeStamp))
                .then(Mono.just(1).flatMap(t -> repository.resetCounter(bucket, timeStamp)))
                .then(Mono.just(1).flatMap(t -> repository.getCounter(bucket, timeStamp))))
        .expectNext(0L)
        .verifyComplete();

不工作

StepVerifier.create(
        repository.incrementCounter(bucket, timeStamp)
                .then(repository.incrementCounter(bucket, timeStamp))
                .then(repository.resetCounter(bucket, timeStamp))
                .then(repository.getCounter(bucket, timeStamp)))
        .expectNext(0L)
        .verifyComplete();

【问题讨论】:

    标签: java project-reactor reactive-streams


    【解决方案1】:

    根据Gitter channel,您的存储库应该返回冷的Monos(又名“懒惰”),但您很可能在返回之前就开始调用了。

    您可以使用Mono.defer 使其变冷。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-23
      • 2021-04-20
      • 2021-01-30
      • 2017-04-29
      • 1970-01-01
      • 2017-10-24
      • 2014-07-12
      • 1970-01-01
      相关资源
      最近更新 更多