小编tan*_*aya的帖子

Spring Reactor Retry.retryWhen() 不起作用

如何使用重试?我想在出错时重试调用方法,直到成功。 我的测试代码


@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)

spring project-reactor spring-webflux

3
推荐指数
1
解决办法
5370
查看次数

标签 统计

project-reactor ×1

spring ×1

spring-webflux ×1