Spring Retry @Recover传递参数
问题描述:
我找不到任何有关我需要的操作可能性的信息。我正在使用@Retry处理方法使用@Retryable注释。 Smth像这样:Spring Retry @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,只是对齐和@Recover
方法@Retryable
之间的参数:
的回收方法可以任选地包括在 异常抛出的参数,并且还可以任选地传递给自变量 原始重试方法(或其中的部分列表,只要 都省略)。例如:
@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);
}