Search in sources :

Example 11 with FaultToleranceService

use of fish.payara.microprofile.faulttolerance.FaultToleranceService in project Payara by payara.

the class FallbackPolicy method fallback.

/**
 * Performs the fallback operation defined by the @Fallback annotation.
 * @param invocationContext The failing invocation context
 * @return The result of the executed fallback method
 * @throws Exception If the fallback method itself fails.
 */
public Object fallback(InvocationContext invocationContext) throws Exception {
    Object fallbackInvocationContext = null;
    FaultToleranceService faultToleranceService = Globals.getDefaultBaseServiceLocator().getService(FaultToleranceService.class);
    InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
    faultToleranceService.startFaultToleranceSpan(new RequestTraceSpan("executeFallbackMethod"), invocationManager, invocationContext);
    try {
        if (fallbackMethod != null && !fallbackMethod.isEmpty()) {
            logger.log(Level.FINE, "Using fallback method: {0}", fallbackMethod);
            fallbackInvocationContext = invocationContext.getMethod().getDeclaringClass().getDeclaredMethod(fallbackMethod, invocationContext.getMethod().getParameterTypes()).invoke(invocationContext.getTarget(), invocationContext.getParameters());
        } else {
            logger.log(Level.FINE, "Using fallback class: {0}", fallbackClass.getName());
            ExecutionContext executionContext = new FaultToleranceExecutionContext(invocationContext.getMethod(), invocationContext.getParameters());
            fallbackInvocationContext = fallbackClass.getDeclaredMethod(FALLBACK_HANDLER_METHOD_NAME, ExecutionContext.class).invoke(CDI.current().select(fallbackClass).get(), executionContext);
        }
    } finally {
        faultToleranceService.endFaultToleranceSpan();
    }
    return fallbackInvocationContext;
}
Also used : ExecutionContext(org.eclipse.microprofile.faulttolerance.ExecutionContext) InvocationManager(org.glassfish.api.invocation.InvocationManager) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) FaultToleranceService(fish.payara.microprofile.faulttolerance.FaultToleranceService)

Aggregations

FaultToleranceService (fish.payara.microprofile.faulttolerance.FaultToleranceService)11 InvocationManager (org.glassfish.api.invocation.InvocationManager)9 Config (org.eclipse.microprofile.config.Config)8 FallbackPolicy (fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy)5 AroundInvoke (javax.interceptor.AroundInvoke)5 Fallback (org.eclipse.microprofile.faulttolerance.Fallback)5 Retry (org.eclipse.microprofile.faulttolerance.Retry)5 NoSuchElementException (java.util.NoSuchElementException)4 RequestTraceSpan (fish.payara.notification.requesttracing.RequestTraceSpan)3 NamingException (javax.naming.NamingException)3 ChronoUnit (java.time.temporal.ChronoUnit)2 ArrayList (java.util.ArrayList)2 ManagedScheduledExecutorService (javax.enterprise.concurrent.ManagedScheduledExecutorService)2 BulkheadException (org.eclipse.microprofile.faulttolerance.exceptions.BulkheadException)2 CircuitBreakerOpenException (org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException)2 CircuitBreakerState (fish.payara.microprofile.faulttolerance.state.CircuitBreakerState)1 List (java.util.List)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 Semaphore (java.util.concurrent.Semaphore)1