如何使用重试?我想在出错时重试调用方法,直到成功。 我的测试代码
@Test
public void fun1() throws InterruptedException {
this.generate()
.retryWhen(Retry.fixedDelay(5, Duration.ofMillis(1))
.filter(e -> e instanceof Exception)
.doBeforeRetry(res -> System.out.println("retry begin"))
.doAfterRetry(res -> System.out.println("try finished")))
.onErrorContinue((throwable, o) -> System.out.println(throwable))
.subscribe(System.out::println);
}
private Mono<String> generate() throws InterruptedException {
if (retryTime.get() == 3) {
return Mono.just("Hello");
}
System.out.println("i am called" + retryTime.getAndAdd(1));
return Mono.error(new IllegalArgumentException("exception test"));
}
Run Code Online (Sandbox Code Playgroud)
** 但得到这个结果**
i am called1
10:02:12.104 [main] DEBUG reactor.util.Loggers - Using Slf4j logging framework
retry begin
try finished
retry begin
try finished
retry begin
try …Run Code Online (Sandbox Code Playgroud)