如何解决使用 RetryListenerSupport 时,无法从 @Retryable 获取可抛出的集合
我想捕获异常并验证与@Retyable 中包含的集合值的匹配,但我无法获取这些值。我知道一个选项,我可以使用 java 反射实用程序并获取值,但我认为这不是一个好主意,因为它很垃圾。
我捕获了 IOException.class、NotFoundException.class
@Retryable(maxAttempts = 3,value = { IOException.class,NotFoundException.class },backoff = @Backoff(delay = 1000,multiplier = 2))
public void createBook(final int id,final Book book){
//any code
}
并且我支持在 SupportRetryListenerSupport 中重试
@Component
public class SupportRetryLoggingListener extends RetryListenerSupport {
private final static Logger LOG = LoggerFactory.getLogger(SupportRetryLoggingListener.class);
@Override
public <T,E extends Throwable> void close(final RetryContext context,final RetryCallback<T,E> callback,final Throwable throwable) {
if (throwable != null) {
log.error("Processing operation Error: {},exception: {}",context,throwable);
}
}
}
我的选择:
Field labelField = ReflectionUtils.findField(callback.getClass(),"this$0");
ReflectionUtils.makeAccessible(Objects.requireNonNull(labelField));
final var field = (RetryOperationsInterceptor)ReflectionUtils.getField(labelField,callback);
Field retryOperations = ReflectionUtils.findField(field.getClass(),"retryOperations");
ReflectionUtils.makeAccessible(retryOperations);
final var retryTemplate = (RetryTemplate)ReflectionUtils.getField(retryOperations,field);
...
Field classified = ReflectionUtils.findField(binaryExceptionClassifier.getClass(),"classified");
ReflectionUtils.makeAccessible(classified);
有人遇到过这种情况吗?
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。