私はProject Reactorに関する3つの質問があります。私が持っているコードから始めてください(問題を分かりやすくするために簡略化されています)。 プロジェクトの原子炉のタイムアウト処理
Mono<Integer> doWithSession(Function<String, Mono<Integer>> callback, long timeout) {
return Mono.just("hello")
.compose(monostr -> monostr
.doOnSuccess(str -> System.out.println("Suppose I want to release session here after all")) //(1)
.doOnCancel(() -> System.out.println("cancelled")) //(2)
.then(callback::apply)
.timeoutMillis(timeout, Mono.error(new TimeoutException("Timeout after " + timeout)))
);
}
とテスト:だから
@Test
public void testDoWithSession2() throws Exception {
Function<String, Mono<Integer>> fun1 = str -> Mono.fromCallable(() -> {
System.out.println("do some long timed work");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("work has completed");
return str.length();
});
StepVerifier.create(doWithSession(fun1,1000))
.verifyError(TimeoutException.class);
}
と質問:
fun1
の呼び出しを中断し、すぐにエラーを返すためにどのように? (多分私は何か間違っていますが、タイムアウト後ではなくコールバックの呼び出し後にエラーが返されます)- なぜ
doOnSuccess
とdoOnCancel
が同時に呼び出されますか? (私が期待される(1)または(2)が呼び出されますが、両方ではない) - そして、どのように次のような場合に対処するために:
- コードで
Mono.just("hello")
が接続を取得していることを想像。 - (
callback
)私は接続して何らかの結果を得ています(私の場合はMono<Integer>
)。 - (成功時または失敗時)セッションを解放したい((1)でこれを実行しようとしています)。答えはスケジューラを使用することであるように第一の質問には
- コードで