Search in sources :

Example 6 with Retry

use of org.eclipse.microprofile.faulttolerance.Retry 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)

Example 7 with Retry

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

the class ConfigOverridePriorityTest method methodAndClassLevelAnnotationPresent.

@Test
public void methodAndClassLevelAnnotationPresent() {
    Method annotatedMethod = TestUtils.getAnnotatedMethod();
    FaultToleranceConfig config = configFactory.bindTo(new StaticAnalysisContext(getClass(), annotatedMethod));
    Retry retry = config.getAnnotation(Retry.class);
    assertEquals(ChronoUnit.WEEKS, config.delayUnit(retry));
    overrides.override(Retry.class, "delayUnit", ChronoUnit.ERAS);
    assertEquals("should be: global override effective", ChronoUnit.ERAS, config.delayUnit(retry));
    overrides.override(getClass(), Retry.class, "delayUnit", ChronoUnit.MICROS);
    assertEquals("should be: class level annotation present but override ineffective because method level annotation is present", ChronoUnit.ERAS, config.delayUnit(retry));
    overrides.override(annotatedMethod, Retry.class, "delayUnit", ChronoUnit.YEARS);
    assertEquals("should be: method level override effective", ChronoUnit.YEARS, config.delayUnit(retry));
}
Also used : StaticAnalysisContext(fish.payara.microprofile.faulttolerance.policy.StaticAnalysisContext) FaultToleranceConfig(fish.payara.microprofile.faulttolerance.FaultToleranceConfig) Method(java.lang.reflect.Method) Retry(org.eclipse.microprofile.faulttolerance.Retry) Test(org.junit.Test)

Example 8 with Retry

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

the class FaultToleranceStressTest method occasionallyFailingService_Method.

/**
 * The method under tests fails every 3rd call whereby in a window of 4 there can be 2 failed calls opening the
 * circuit. As delay is just recorded but not enforced there is only a minimal chance another caller does an attempt
 * while the circuit is open but occasionally this happens.
 *
 * Every 5th call returns a failed Future that should be handed to the caller as is.
 *
 * Every 3rd call fails causing a retry. Together with open circuits this might even cause fallback handler to be
 * used which will also fail the result as it only rethrows the error.
 */
@Asynchronous
@Fallback(FaultToleranceStressTest.class)
@Retry(maxRetries = 1)
@Bulkhead(value = 2, waitingTaskQueue = 2)
@CircuitBreaker(successThreshold = 2, delay = 0, requestVolumeThreshold = 4)
public Future<String> occasionallyFailingService_Method() throws IOException {
    int called = methodInvocationCount.incrementAndGet();
    if (called % 3 == 0) {
        // this causes a retry
        methodFailedInvocationCount.incrementAndGet();
        throw new IOException("Failed");
    }
    if (called % 5 == 0) {
        // this does not cause a retry, its simply a Future that completes with a failure
        CompletableFuture<String> failedValue = new CompletableFuture<>();
        failedValue.completeExceptionally(new IOException("Failed"));
        return failedValue;
    }
    methodSuccessfulInvocationCount.incrementAndGet();
    return CompletableFuture.completedFuture("Success");
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Asynchronous(org.eclipse.microprofile.faulttolerance.Asynchronous) CircuitBreaker(org.eclipse.microprofile.faulttolerance.CircuitBreaker) Bulkhead(org.eclipse.microprofile.faulttolerance.Bulkhead) Fallback(org.eclipse.microprofile.faulttolerance.Fallback) Retry(org.eclipse.microprofile.faulttolerance.Retry)

Aggregations

Retry (org.eclipse.microprofile.faulttolerance.Retry)8 FaultToleranceService (fish.payara.microprofile.faulttolerance.FaultToleranceService)4 Config (org.eclipse.microprofile.config.Config)4 Fallback (org.eclipse.microprofile.faulttolerance.Fallback)4 InvocationManager (org.glassfish.api.invocation.InvocationManager)4 FallbackPolicy (fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy)3 AroundInvoke (javax.interceptor.AroundInvoke)3 FaultToleranceConfig (fish.payara.microprofile.faulttolerance.FaultToleranceConfig)2 StaticAnalysisContext (fish.payara.microprofile.faulttolerance.policy.StaticAnalysisContext)2 Method (java.lang.reflect.Method)2 NoSuchElementException (java.util.NoSuchElementException)2 NamingException (javax.naming.NamingException)2 Test (org.junit.Test)2 RequestTraceSpan (fish.payara.notification.requesttracing.RequestTraceSpan)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ChronoUnit (java.time.temporal.ChronoUnit)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1