我有兩個測試函數,我期望返回相同的結果。第一個(monoTest3)在'.then()'方法中使用lambda表達式。第二個不是。有人可以解釋Reactor Mono流中的.then()方法的注意事項嗎?
爲什麼我會得到結果?
使用ReactiveX和lambda表達式時,有什麼做什麼和不該做什麼?
在代碼下面進一步monoTest3產生
monoVoid called
Success
----
monoVoid2 called
Success
和monoTest4產生(所希望的結果):
monoVoid called
monoVoid2 called
Success
----
monoVoid2 called
monoVoid called
Success
最後的代碼。
private static Mono<Void> monoVoid() {
System.out.println("monoVoid called");
return Mono.empty();
}
private static Mono<Void> monoVoid2() {
System.out.println("monoVoid2 called");
return Mono.just("Hello").then();
}
@Test
public void monoTest3() throws Exception {
monoVoid()
.then(v -> monoVoid2())
.doOnSuccess(v -> System.out.println("Success"))
.block();
System.out.println("----");
monoVoid2()
.then(v -> monoVoid())
.doOnSuccess(v -> System.out.println("Success"))
.block();
}
@Test
public void monoTest4() throws Exception {
monoVoid()
.then(MonoTest::monoVoid2)
.doOnSuccess(v -> System.out.println("Success"))
.block();
System.out.println("----");
monoVoid2()
.then(MonoTest::monoVoid)
.doOnSuccess(v -> System.out.println("Success"))
.block();
}
非常感謝您的解釋。 –