Search in sources :

Example 1 with ExecutionContext

use of org.eclipse.microprofile.faulttolerance.ExecutionContext 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)1 RequestTraceSpan (fish.payara.notification.requesttracing.RequestTraceSpan)1 ExecutionContext (org.eclipse.microprofile.faulttolerance.ExecutionContext)1 InvocationManager (org.glassfish.api.invocation.InvocationManager)1