org.eclipse.microprofile.faulttolerance.FallbackHandler Java Examples

The following examples show how to use org.eclipse.microprofile.faulttolerance.FallbackHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultFallbackHandlerProvider.java    From smallrye-fault-tolerance with Apache License 2.0 6 votes vote down vote up
@Override
public <T> FallbackHandler<T> get(FaultToleranceOperation operation) {
    if (operation.hasFallback()) {
        //noinspection Convert2Lambda
        return new FallbackHandler<T>() {
            @Override
            public T handle(ExecutionContext context) {
                Unmanaged<FallbackHandler<T>> unmanaged = new Unmanaged<>(beanManager,
                        operation.getFallback().get(FallbackConfig.VALUE));
                Unmanaged.UnmanagedInstance<FallbackHandler<T>> unmanagedInstance = unmanaged.newInstance();
                FallbackHandler<T> handler = unmanagedInstance.produce().inject().postConstruct().get();
                try {
                    return handler.handle(context);
                } finally {
                    // The instance exists to service a single invocation only
                    unmanagedInstance.preDestroy().dispose();
                }
            }
        };
    }
    return null;
}
 
Example #2
Source File: QuarkusFallbackHandlerProvider.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public <T> FallbackHandler<T> get(FaultToleranceOperation operation) {
    if (operation.hasFallback()) {
        return new FallbackHandler<T>() {
            @SuppressWarnings("unchecked")
            @Override
            public T handle(ExecutionContext context) {
                Class<? extends FallbackHandler<?>> clazz = operation.getFallback().get(FallbackConfig.VALUE);
                FallbackHandler<T> fallbackHandlerInstance = (FallbackHandler<T>) instance.select(clazz).get();
                try {
                    return fallbackHandlerInstance.handle(context);
                } finally {
                    // The instance exists to service a single invocation only
                    instance.destroy(fallbackHandlerInstance);
                }
            }
        };
    }
    return null;
}
 
Example #3
Source File: FallbackConfig.java    From smallrye-fault-tolerance with Apache License 2.0 4 votes vote down vote up
@Override
public void validate() {
    if (!"".equals(get(FALLBACK_METHOD))) {
        if (!Fallback.DEFAULT.class.equals(get(VALUE))) {
            throw new FaultToleranceDefinitionException(
                    "Fallback configuration can't contain an handler class and method at the same time");
        }
        Method fallbackMethod;
        try {
            fallbackMethod = SecurityActions.getDeclaredMethod(
                    beanClass,
                    method.getDeclaringClass(),
                    get(FALLBACK_METHOD),
                    method.getGenericParameterTypes());
        } catch (PrivilegedActionException e) {
            throw new FaultToleranceDefinitionException("Fallback method " + get(FALLBACK_METHOD)
                    + " with same parameters as " + method.getName() + " not found", e);
        }
        if (fallbackMethod == null) {
            throw new FaultToleranceDefinitionException(
                    "Fallback method " + get(FALLBACK_METHOD) + " with same parameters as " + method.getName()
                            + " not found");
        }
        if (!method.getReturnType().equals(void.class)
                && !isAssignableFrom(method.getGenericReturnType(), fallbackMethod.getGenericReturnType())) {
            throw new FaultToleranceDefinitionException(
                    "Fallback method " + get(FALLBACK_METHOD) + " must have a return type assignable to "
                            + method.getName());
        }
    }
    if (!Fallback.DEFAULT.class.equals(get(VALUE))) {
        Class<?> fbhc = get(VALUE);
        Type fallbackType = null;
        for (Type genericInterface : fbhc.getGenericInterfaces()) {
            if (genericInterface instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
                if (parameterizedType.getRawType().equals(FallbackHandler.class)) {
                    fallbackType = parameterizedType.getActualTypeArguments()[0];
                    break;
                }
            }
        }
        if (fallbackType == null || !method.getGenericReturnType().equals(fallbackType)) {
            throw new FaultToleranceDefinitionException(
                    "Fallback handler type [" + fallbackType + "] is not the same as the method return type: " + method);
        }
    }
}
 
Example #4
Source File: FallbackHandlerProvider.java    From smallrye-fault-tolerance with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param operation Fault tolerance operation
 * @return a new fallback handler or {@code null}
 */
<T> FallbackHandler<T> get(FaultToleranceOperation operation);