Spring Retry @Recover传递参数

时间:2017-10-13 13:18:04

标签: java spring recover spring-retry

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

    @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()方法。有任何想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:4)

根据Spring Retry documentation,只需调整@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
    }
}

所以你可以写:

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