【发布时间】:2019-08-28 23:32:01
【问题描述】:
如何有条件地执行then(Mono<T>)操作符?
我有一个返回Mono<Void> 的方法。它还可以返回错误信号。我想使用then 运算符(或任何其他运算符),只有在前一个操作完成且没有错误信号时。
谁能帮我找到合适的供应商运营商?
public static void main(String[] args) {
Mono.just("GOOD_SIGNAL")//It can also be a BAD_SIGNAL
.flatMap(s -> firstMethod(s))
.then(secondMethod())
.subscribe()
;
}
private static Mono<String> secondMethod() {
//This method call is only valid when the firstMethod is success
return Mono.just("SOME_SIGNAL");
}
private static Mono<Void> firstMethod(String s) {
if ("BAD_SIGNAL".equals(s)) {
Mono.error(new Exception("Some Error occurred"));
}
return Mono
.empty()//Just to illustrate that this function return Mono<Void>
.then();
}
-谢谢
【问题讨论】:
标签: java spring-webflux project-reactor