org.springframework.retry.ExhaustedRetryException: Cannot locate recovery method; 异常解决办法

 今天,涉及到一个重试的功能,开始写了一个demo,可以正常运行,之后把代码移到项目,测试了一下,却发现@Recover 注解的方法,无法执行,报以下错误:

org.springframework.retry.ExhaustedRetryException: Cannot locate recovery method; 异常解决办法_第1张图片排查了好久,最终,找到了原因,原来是:

    @Retryable(value= {RemoteAccessException.class},maxAttempts = 3,backoff =@Backoff(delay = 5000L,multiplier = 1))
    public RestUtils call() throws Exception {
        System.out.println("do something...");
        throw new RemoteAccessException("RPC调用异常");
    }
    @Recover
    public void recover(RemoteAccessException e) {
        System.out.println(e.getMessage());
        System.out.println("1233143212");
    }

@Retryable注解的方法,当有返回值(RestUtils)时,是不会触发到@Recover注解的方法。改成下边的函数就可以了:

    @Retryable(value= {RemoteAccessException.class},maxAttempts = 3,backoff = @Backoff(delay = 5000L,multiplier = 1))
    public void call() throws Exception {
        System.out.println("do something...");
        throw new RemoteAccessException("RPC调用异常");
    }
    @Recover
    public void recover(RemoteAccessException e) {
        System.out.println(e.getMessage());
        System.out.println("1233143212");
    }

 

你可能感兴趣的:(java问题总结,JAVA)