问题描述
我想捕获异常并验证与@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 (将#修改为@)