10

我找不到任何有关我需要采取行动的可能性的信息。我将@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() 方法。有任何想法吗?提前致谢。

4

1 回答 1

18

根据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
    }
}

所以你可以写:

@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);
}
于 2017-10-13T13:24:07.383 回答