Spring Retry @Recover传递参数

Svi*_*ana 5 java spring recover spring-retry

我找不到任何有关需要采取行动的信息。我正在使用@Retryable注释和@Recover处理程序方法。像这样:

    @Retryable(value = {Exception.class}, maxAttempts = 5, backoff = @Backoff(delay = 10000))
    public void update(Integer id)
    {
        execute(id);
    }

    @Recover
    public void recover(Exception ex)
    {
        logger.error("Error when updating object with id {}", id);
    }
Run Code Online (Sandbox Code Playgroud)

问题是我不知道如何将我的参数“ id”传递给recovery()方法。有任何想法吗?提前致谢。

dav*_*xxx 7

根据Spring Retry文档,只需在@Retryable@Recover方法之间对齐参数 :

恢复方法的参数可以选择包括引发的异常,还可以选择将参数传递给原始的可重试方法(或部分列表,只要省略了一部分)。例:

@Service
class Service {
    @Retryable(RemoteAccessException.class)
    public void service(String str1, String str2) {
        // ... do something
    }
    @Recover
    public void recover(RemoteAccessException e, String str1, String str2) {
       // ... error handling making use of original args if required
    }
}
Run Code Online (Sandbox Code Playgroud)

所以你可以这样写:

@Retryable(value = {Exception.class}, maxAttempts = 5, backoff = @Backoff(delay = 10000))
public void update(Integer id) {
    execute(id);
}

@Recover
public void recover(Exception ex, Integer id){
    logger.error("Error when updating object with id {}", id);
}
Run Code Online (Sandbox Code Playgroud)