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;
}
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));
}
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");
}
Aggregations