Search in sources :

Example 1 with CheckedFunction

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;
        }
    };
}
Also used : CheckedFunction(io.github.resilience4j.core.functions.CheckedFunction) Predicate(java.util.function.Predicate) Method(java.lang.reflect.Method) Method(java.lang.reflect.Method)

Example 2 with CheckedFunction

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;
}
Also used : MethodHandler(feign.InvocationHandlerFactory.MethodHandler) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) Map(java.util.Map) HashMap(java.util.HashMap) CheckedFunction(io.github.resilience4j.core.functions.CheckedFunction)

Example 3 with CheckedFunction

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;
        }
    };
}
Also used : CheckedFunction(io.github.resilience4j.core.functions.CheckedFunction) Predicate(java.util.function.Predicate) Method(java.lang.reflect.Method) Function(java.util.function.Function) InvocationTargetException(java.lang.reflect.InvocationTargetException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

CheckedFunction (io.github.resilience4j.core.functions.CheckedFunction)3 Method (java.lang.reflect.Method)3 Predicate (java.util.function.Predicate)2 MethodHandler (feign.InvocationHandlerFactory.MethodHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Function (java.util.function.Function)1