Spring 重试@Recover 传递参数

Spring Retry @Recover passing parameters

我找不到任何关于我需要采取的行动的信息。我正在使用 @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);
    }

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

根据Spring Retry documentation,只需对齐@Retryable@Recover方法之间的参数即可:

The arguments for the recovery method can optionally include the exception that was thrown, and also optionally the arguments passed to the orginal retryable method (or a partial list of them as long as none are omitted). Example:

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

所以你可以这样写:

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