我想捕获一个异常并验证是否与@retyable中包含的集合值匹配,但无法获取值。我知道一个选择,我可以使用java反射utils并获取值,但我认为这不是一个好主意,因为它是垃圾。
我捕获ioexception.class,而不是foundexception.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);
以前有人经历过吗?
谢谢
暂无答案!
目前还没有任何答案,快来回答吧!