use of io.github.resilience4j.core.functions.CheckedFunction in project resilience4j by resilience4j.
the class DefaultFallbackHandler method decorate.
@Override
public CheckedFunction<Object[], Object> decorate(CheckedFunction<Object[], Object> invocationCall, Method method, Predicate<Exception> filter) {
validateFallback(fallback, method);
Method fallbackMethod = getFallbackMethod(fallback, method);
fallbackMethod.setAccessible(true);
return args -> {
try {
return invocationCall.apply(args);
} catch (Exception exception) {
if (filter.test(exception)) {
return fallbackMethod.invoke(fallback, args);
}
throw exception;
}
};
}
use of io.github.resilience4j.core.functions.CheckedFunction in project resilience4j by resilience4j.
the class DecoratorInvocationHandler method decorateMethodHandlers.
/**
* Applies the specified {@link FeignDecorator} to all specified {@link MethodHandler}s and
* returns the result as a map of {@link CheckedFunction}s. Invoking a {@link CheckedFunction}
* will therefore invoke the decorator which, in turn, may invoke the corresponding {@link
* MethodHandler}.
*
* @param dispatch a map of the methods from the feign interface to the {@link
* MethodHandler}s.
* @param invocationDecorator the {@link FeignDecorator} with which to decorate the {@link
* MethodHandler}s.
* @param target the target feign interface.
* @return a new map where the {@link MethodHandler}s are decorated with the {@link
* FeignDecorator}.
*/
private Map<Method, CheckedFunction<Object[], Object>> decorateMethodHandlers(Map<Method, MethodHandler> dispatch, FeignDecorator invocationDecorator, Target<?> target) {
final Map<Method, CheckedFunction<Object[], Object>> map = new HashMap<>();
for (final Map.Entry<Method, MethodHandler> entry : dispatch.entrySet()) {
final Method method = entry.getKey();
final MethodHandler methodHandler = entry.getValue();
if (methodHandler != null) {
CheckedFunction<Object[], Object> decorated = invocationDecorator.decorate(methodHandler::invoke, method, methodHandler, target);
map.put(method, decorated);
}
}
return map;
}
use of io.github.resilience4j.core.functions.CheckedFunction in project resilience4j by resilience4j.
the class FallbackFactory method decorate.
@Override
public CheckedFunction<Object[], Object> decorate(CheckedFunction<Object[], Object> invocationCall, Method method, Predicate<Exception> filter) {
return args -> {
try {
return invocationCall.apply(args);
} catch (Exception exception) {
if (filter.test(exception)) {
T fallbackInstance = fallbackSupplier.apply(exception);
validateFallback(fallbackInstance, method);
Method fallbackMethod = getFallbackMethod(fallbackInstance, method);
try {
return fallbackMethod.invoke(fallbackInstance, args);
} catch (InvocationTargetException e) {
// Rethrow the exception thrown in the fallback wrapped by InvocationTargetException
throw e.getCause();
}
}
throw exception;
}
};
}
Aggregations