Search in sources :

Example 1 with Fallback

use of org.eclipse.microprofile.faulttolerance.Fallback in project Payara by payara.

the class AsynchronousInterceptor method intercept.

@AroundInvoke
public Object intercept(InvocationContext invocationContext) throws Exception {
    Object proceededInvocationContext = null;
    // Get the configured ManagedExecutorService from the Fault Tolerance Service
    FaultToleranceService faultToleranceService = Globals.getDefaultBaseServiceLocator().getService(FaultToleranceService.class);
    ManagedExecutorService managedExecutorService = faultToleranceService.getManagedExecutorService();
    InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
    Config config = null;
    try {
        config = ConfigProvider.getConfig();
    } catch (IllegalArgumentException ex) {
        logger.log(Level.INFO, "No config could be found", ex);
    }
    try {
        // Attempt to proceed the InvocationContext with Asynchronous semantics if Fault Tolerance is enabled
        if (faultToleranceService.isFaultToleranceEnabled(faultToleranceService.getApplicationName(invocationManager, invocationContext), config)) {
            Callable callable = () -> {
                return invocationContext.proceed();
            };
            logger.log(Level.FINER, "Proceeding invocation asynchronously");
            proceededInvocationContext = new FutureDelegator(managedExecutorService.submit(callable));
        } else {
            // If fault tolerance isn't enabled, just proceed as normal
            logger.log(Level.FINE, "Fault Tolerance not enabled for {0}, proceeding normally without asynchronous.", faultToleranceService.getApplicationName(invocationManager, invocationContext));
            proceededInvocationContext = invocationContext.proceed();
        }
    } catch (Exception ex) {
        // If an exception was thrown, check if the method is annotated with @Fallback
        // We should only get here if executing synchronously, as the exception wouldn't get thrown in this thread
        Fallback fallback = FaultToleranceCdiUtils.getAnnotation(beanManager, Fallback.class, invocationContext);
        // If the method was annotated with Fallback, attempt it, otherwise just propagate the exception upwards
        if (fallback != null) {
            logger.log(Level.FINE, "Fallback annotation found on method - falling back from Asynchronous");
            FallbackPolicy fallbackPolicy = new FallbackPolicy(fallback, config, invocationContext);
            proceededInvocationContext = fallbackPolicy.fallback(invocationContext);
        } else {
            throw ex;
        }
    }
    return proceededInvocationContext;
}
Also used : FallbackPolicy(fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy) ManagedExecutorService(javax.enterprise.concurrent.ManagedExecutorService) Config(org.eclipse.microprofile.config.Config) InvocationManager(org.glassfish.api.invocation.InvocationManager) Fallback(org.eclipse.microprofile.faulttolerance.Fallback) FaultToleranceService(fish.payara.microprofile.faulttolerance.FaultToleranceService) Callable(java.util.concurrent.Callable) FaultToleranceException(org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 2 with Fallback

use of org.eclipse.microprofile.faulttolerance.Fallback in project Payara by payara.

the class CircuitBreakerInterceptor method intercept.

@AroundInvoke
public Object intercept(InvocationContext invocationContext) throws Exception {
    Object proceededInvocationContext = null;
    FaultToleranceService faultToleranceService = Globals.getDefaultBaseServiceLocator().getService(FaultToleranceService.class);
    InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
    Config config = null;
    try {
        config = ConfigProvider.getConfig();
    } catch (IllegalArgumentException ex) {
        logger.log(Level.INFO, "No config could be found", ex);
    }
    // Attempt to proceed the invocation with CircuitBreaker semantics if Fault Tolerance is enabled
    try {
        if (faultToleranceService.isFaultToleranceEnabled(faultToleranceService.getApplicationName(invocationManager, invocationContext), config)) {
            logger.log(Level.FINER, "Proceeding invocation with circuitbreaker semantics");
            proceededInvocationContext = circuitBreak(invocationContext);
        } else {
            // If fault tolerance isn't enabled, just proceed as normal
            logger.log(Level.FINE, "Fault Tolerance not enabled for {0}, proceeding normally without " + "circuitbreaker.", faultToleranceService.getApplicationName(invocationManager, invocationContext));
            proceededInvocationContext = invocationContext.proceed();
        }
    } catch (Exception ex) {
        Retry retry = FaultToleranceCdiUtils.getAnnotation(beanManager, Retry.class, invocationContext);
        if (retry != null) {
            logger.log(Level.FINE, "Retry annotation found on method, propagating error upwards.");
            throw ex;
        } else {
            Fallback fallback = FaultToleranceCdiUtils.getAnnotation(beanManager, Fallback.class, invocationContext);
            if (fallback != null) {
                logger.log(Level.FINE, "Fallback annotation found on method, and no Retry annotation - " + "falling back from CircuitBreaker");
                FallbackPolicy fallbackPolicy = new FallbackPolicy(fallback, config, invocationContext);
                proceededInvocationContext = fallbackPolicy.fallback(invocationContext);
            } else {
                throw ex;
            }
        }
    }
    return proceededInvocationContext;
}
Also used : FallbackPolicy(fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy) Config(org.eclipse.microprofile.config.Config) InvocationManager(org.glassfish.api.invocation.InvocationManager) Fallback(org.eclipse.microprofile.faulttolerance.Fallback) Retry(org.eclipse.microprofile.faulttolerance.Retry) FaultToleranceService(fish.payara.microprofile.faulttolerance.FaultToleranceService) NamingException(javax.naming.NamingException) NoSuchElementException(java.util.NoSuchElementException) CircuitBreakerOpenException(org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 3 with Fallback

use of org.eclipse.microprofile.faulttolerance.Fallback in project Payara by payara.

the class RetryInterceptor method intercept.

@AroundInvoke
public Object intercept(InvocationContext invocationContext) throws Exception {
    Object proceededInvocationContext = null;
    FaultToleranceService faultToleranceService = Globals.getDefaultBaseServiceLocator().getService(FaultToleranceService.class);
    InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
    Config config = null;
    try {
        config = ConfigProvider.getConfig();
    } catch (IllegalArgumentException ex) {
        logger.log(Level.INFO, "No config could be found", ex);
    }
    try {
        if (faultToleranceService.isFaultToleranceEnabled(faultToleranceService.getApplicationName(invocationManager, invocationContext), config)) {
            logger.log(Level.FINER, "Proceeding invocation with retry semantics");
            proceededInvocationContext = retry(invocationContext);
        } else {
            // If fault tolerance isn't enabled, just proceed as normal
            logger.log(Level.FINE, "Fault Tolerance not enabled for {0}, proceeding normally without retry.", faultToleranceService.getApplicationName(invocationManager, invocationContext));
            proceededInvocationContext = invocationContext.proceed();
        }
    } catch (Exception ex) {
        Fallback fallback = FaultToleranceCdiUtils.getAnnotation(beanManager, Fallback.class, invocationContext);
        if (fallback != null) {
            logger.log(Level.FINE, "Fallback annotation found on method - falling back from Retry");
            FallbackPolicy fallbackPolicy = new FallbackPolicy(fallback, config, invocationContext);
            proceededInvocationContext = fallbackPolicy.fallback(invocationContext);
        } else {
            throw ex;
        }
    }
    return proceededInvocationContext;
}
Also used : FallbackPolicy(fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy) Config(org.eclipse.microprofile.config.Config) InvocationManager(org.glassfish.api.invocation.InvocationManager) Fallback(org.eclipse.microprofile.faulttolerance.Fallback) FaultToleranceService(fish.payara.microprofile.faulttolerance.FaultToleranceService) NoSuchElementException(java.util.NoSuchElementException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 4 with Fallback

use of org.eclipse.microprofile.faulttolerance.Fallback in project Payara by payara.

the class BulkheadInterceptor method intercept.

@AroundInvoke
public Object intercept(InvocationContext invocationContext) throws Exception {
    Object proceededInvocationContext = null;
    FaultToleranceService faultToleranceService = Globals.getDefaultBaseServiceLocator().getService(FaultToleranceService.class);
    InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
    Config config = null;
    try {
        config = ConfigProvider.getConfig();
    } catch (IllegalArgumentException ex) {
        logger.log(Level.INFO, "No config could be found", ex);
    }
    try {
        String appName = faultToleranceService.getApplicationName(invocationManager, invocationContext);
        // Attempt to proceed the InvocationContext with Bulkhead semantics if Fault Tolerance is enabled
        if (faultToleranceService.isFaultToleranceEnabled(appName, config)) {
            logger.log(Level.FINER, "Proceeding invocation with bulkhead semantics");
            proceededInvocationContext = bulkhead(invocationContext);
        } else {
            // If fault tolerance isn't enabled, just proceed as normal
            logger.log(Level.FINE, "Fault Tolerance not enabled for {0}, proceeding normally without bulkhead.", faultToleranceService.getApplicationName(invocationManager, invocationContext));
            proceededInvocationContext = invocationContext.proceed();
        }
    } catch (Exception ex) {
        Retry retry = FaultToleranceCdiUtils.getAnnotation(beanManager, Retry.class, invocationContext);
        if (retry != null) {
            logger.log(Level.FINE, "Retry annotation found on method, propagating error upwards.");
            throw ex;
        } else {
            // If an exception was thrown, check if the method is annotated with @Fallback
            Fallback fallback = FaultToleranceCdiUtils.getAnnotation(beanManager, Fallback.class, invocationContext);
            // If the method was annotated with Fallback, attempt it, otherwise just propagate the exception upwards
            if (fallback != null) {
                logger.log(Level.FINE, "Fallback annotation found on method, and no Retry annotation - " + "falling back from Bulkhead");
                FallbackPolicy fallbackPolicy = new FallbackPolicy(fallback, config, invocationContext);
                proceededInvocationContext = fallbackPolicy.fallback(invocationContext);
            } else {
                logger.log(Level.FINE, "Fallback annotation not found on method, propagating error upwards.", ex);
                throw ex;
            }
        }
    }
    return proceededInvocationContext;
}
Also used : FallbackPolicy(fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy) Config(org.eclipse.microprofile.config.Config) InvocationManager(org.glassfish.api.invocation.InvocationManager) Fallback(org.eclipse.microprofile.faulttolerance.Fallback) Retry(org.eclipse.microprofile.faulttolerance.Retry) FaultToleranceService(fish.payara.microprofile.faulttolerance.FaultToleranceService) BulkheadException(org.eclipse.microprofile.faulttolerance.exceptions.BulkheadException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 5 with Fallback

use of org.eclipse.microprofile.faulttolerance.Fallback in project Payara by payara.

the class TimeoutInterceptor method intercept.

@AroundInvoke
public Object intercept(InvocationContext invocationContext) throws Exception {
    Object proceededInvocationContext = null;
    FaultToleranceService faultToleranceService = Globals.getDefaultBaseServiceLocator().getService(FaultToleranceService.class);
    InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
    Config config = null;
    try {
        config = ConfigProvider.getConfig();
    } catch (IllegalArgumentException ex) {
        logger.log(Level.INFO, "No config could be found", ex);
    }
    try {
        if (faultToleranceService.isFaultToleranceEnabled(faultToleranceService.getApplicationName(invocationManager, invocationContext), config)) {
            logger.log(Level.FINER, "Proceeding invocation with timeout semantics");
            proceededInvocationContext = timeout(invocationContext);
        } else {
            // If fault tolerance isn't enabled, just proceed as normal
            logger.log(Level.FINE, "Fault Tolerance not enabled for {0}, proceeding normally without timeout.", faultToleranceService.getApplicationName(invocationManager, invocationContext));
            proceededInvocationContext = invocationContext.proceed();
        }
    } catch (Exception ex) {
        Retry retry = FaultToleranceCdiUtils.getAnnotation(beanManager, Retry.class, invocationContext);
        if (retry != null) {
            logger.log(Level.FINE, "Retry annotation found on method, propagating error upwards.");
            throw ex;
        } else {
            Fallback fallback = FaultToleranceCdiUtils.getAnnotation(beanManager, Fallback.class, invocationContext);
            if (fallback != null) {
                logger.log(Level.FINE, "Fallback annotation found on method, and no Retry annotation - " + "falling back from Timeout");
                FallbackPolicy fallbackPolicy = new FallbackPolicy(fallback, config, invocationContext);
                proceededInvocationContext = fallbackPolicy.fallback(invocationContext);
            } else {
                throw ex;
            }
        }
    }
    return proceededInvocationContext;
}
Also used : FallbackPolicy(fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy) Config(org.eclipse.microprofile.config.Config) InvocationManager(org.glassfish.api.invocation.InvocationManager) Fallback(org.eclipse.microprofile.faulttolerance.Fallback) Retry(org.eclipse.microprofile.faulttolerance.Retry) FaultToleranceService(fish.payara.microprofile.faulttolerance.FaultToleranceService) NamingException(javax.naming.NamingException) TimeoutException(org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException) AroundInvoke(javax.interceptor.AroundInvoke)

Aggregations

Fallback (org.eclipse.microprofile.faulttolerance.Fallback)6 FaultToleranceService (fish.payara.microprofile.faulttolerance.FaultToleranceService)5 FallbackPolicy (fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy)5 AroundInvoke (javax.interceptor.AroundInvoke)5 Config (org.eclipse.microprofile.config.Config)5 InvocationManager (org.glassfish.api.invocation.InvocationManager)5 Retry (org.eclipse.microprofile.faulttolerance.Retry)3 NoSuchElementException (java.util.NoSuchElementException)2 NamingException (javax.naming.NamingException)2 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 ManagedExecutorService (javax.enterprise.concurrent.ManagedExecutorService)1 ExecutionContext (org.eclipse.microprofile.faulttolerance.ExecutionContext)1 FallbackHandler (org.eclipse.microprofile.faulttolerance.FallbackHandler)1 BulkheadException (org.eclipse.microprofile.faulttolerance.exceptions.BulkheadException)1 CircuitBreakerOpenException (org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException)1 FaultToleranceDefinitionException (org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException)1 FaultToleranceException (org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceException)1 TimeoutException (org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException)1