topic: Spring Webflux에서 Mono/Flux를 처리할 때 Exception 등의 에러를 처리하는 방법
(onError 시그널의 처리)
예시)
/*
* error를 발생시키는 함수 errorFunc
* return type : Mono<Int>
*/
errorFunc()
.onErrorReturn(0)
예시)
errorFunc()
.onErrorResume { throwable ->
// Do on error
Mono.just(0)
}
/*
* 특정 Exception만 처리 가능
*/
errorFunc()
.onErrorResume(SomeException::class.java) {
// DO SOMETHING
Mono.just(0)
}
예시)
errorFunc()
.onErrorMap { throwable ->
OtherException("Transfoem any error")
}
예시)
/*
* 실패시 3번 재시도
*/
errorFunc()
.retry(3)
public final Flux<T> retryWhen(Retry retrySpec) {
return onAssembly(new FluxRetryWhen<>(this, retrySpec));
}
예시)
errorFunc()
.retryWhen(
Retry.max(2)
)
errorFunc()
.retryWhen(
Retry.backoff(2, Duration.ofSeconds(5)) // 5초 간격, 2번 retry
)
/*
* 특정 exception 처리
*/
errorFunc()
.retryWhen(
Retry.max(2)
.filter(Predicate.isEqual(SomeException::class.java))
.doAfterRetry {
// Do something after retry
}
)
+) 참고
public abstract class Retry
: Base abstract class for a strategy to decide when to retry given a companion Flux of Retry.RetrySignal, for use with Flux.retryWhen(Retry) and reactor.core.publisher.Mono.retryWhen(Retry). Also provides access to configurable built-in strategies via static factory methods:
Users are encouraged to provide either concrete custom Retry strategies or builders that produce such concrete Retry. The RetrySpec returned by e.g. max(long) is a good inspiration for a fluent approach that generates a Retry at each step and uses immutability/copy-on-write to enable sharing of intermediate steps (that can thus be considered templates).