Spring Retry @Recover传递参数 [英] Spring Retry @Recover passing parameters
问题描述
我找不到任何关于我需要采取行动的可能性的信息。我正在使用带有@Recover处理程序方法的@Retryable注释。 Smth是这样的:
I couldn't find any info about a possibility of action I need. I am using @Retryable annotation with @Recover handler method. Smth like this:
@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);
}
问题是我不知道,如何传递我的参数 idto recover()方法。有任何想法吗?在此先感谢。
The problem is that I don't know, how to pass my parameter "id" to recover() method. Any ideas? Thanks in advance.
推荐答案
根据 Spring Retry文档,只需在 @Retryable
和 @Recover <之间对齐参数/ code>方法:
According to the Spring Retry documentation, just align the parameters between the @Retryable
and the @Recover
methods :
恢复方法的参数可以选择包括抛出的
异常,以及还可选地,传递给
的参数是原始的可重试方法(或者只要
none被省略,它们的部分列表)。示例:
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);
}
这篇关于Spring Retry @Recover传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!