组合多个 Mono<Void>
时使用.then(Mono<Void>)
它们没有按预期顺序运行。
有人可以解释一下下面的工作代码和非工作代码之间的区别吗?
工作代码
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();
最佳答案
根据 Gitter channel ,您的存储库应该返回冷 Mono
(又名“惰性”),但您很可能在返回之前就开始调用。
您可以使用Mono.defer
来使其变冷。
关于java - 将 then 与 Mono<Void> 一起使用时出现意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56705592/