org.eclipse.microprofile.faulttolerance.CircuitBreaker Java Examples

The following examples show how to use org.eclipse.microprofile.faulttolerance.CircuitBreaker. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FaultToleranceExtension.java    From smallrye-fault-tolerance with Apache License 2.0 6 votes vote down vote up
void registerInterceptorBindings(@Observes BeforeBeanDiscovery bbd, BeanManager bm) {
    LOGGER.info("MicroProfile: Fault Tolerance activated");
    bbd.addInterceptorBinding(new FTInterceptorBindingAnnotatedType<>(bm.createAnnotatedType(CircuitBreaker.class)));
    bbd.addInterceptorBinding(new FTInterceptorBindingAnnotatedType<>(bm.createAnnotatedType(Retry.class)));
    bbd.addInterceptorBinding(new FTInterceptorBindingAnnotatedType<>(bm.createAnnotatedType(Timeout.class)));
    bbd.addInterceptorBinding(new FTInterceptorBindingAnnotatedType<>(bm.createAnnotatedType(Asynchronous.class)));
    bbd.addInterceptorBinding(new FTInterceptorBindingAnnotatedType<>(bm.createAnnotatedType(Fallback.class)));
    bbd.addInterceptorBinding(new FTInterceptorBindingAnnotatedType<>(bm.createAnnotatedType(Bulkhead.class)));

    // It seems that fraction deployment module cannot be picked up as a CDI bean archive - see also SWARM-1725
    bbd.addAnnotatedType(bm.createAnnotatedType(FaultToleranceInterceptor.class),
            FaultToleranceInterceptor.class.getName());
    bbd.addAnnotatedType(bm.createAnnotatedType(DefaultFallbackHandlerProvider.class),
            DefaultFallbackHandlerProvider.class.getName());
    bbd.addAnnotatedType(bm.createAnnotatedType(ExecutorProvider.class),
            ExecutorProvider.class.getName());
    bbd.addAnnotatedType(bm.createAnnotatedType(DefaultFaultToleranceOperationProvider.class),
            DefaultFaultToleranceOperationProvider.class.getName());
    bbd.addAnnotatedType(bm.createAnnotatedType(MetricsCollectorFactory.class), MetricsCollectorFactory.class.getName());
    bbd.addAnnotatedType(bm.createAnnotatedType(StrategyCache.class), StrategyCache.class.getName());
}
 
Example #2
Source File: CircuitBreakerClientWithRetryAsync.java    From microprofile-fault-tolerance with Apache License 2.0 6 votes vote down vote up
/**
 * Configured to always time out and Retry until CircuitBreaker is triggered on 4th call. Runs asynchronously.
 */
@CircuitBreaker(successThreshold = 2, requestVolumeThreshold = 4, failureRatio = 0.75, delay = 50000)
@Retry(retryOn = {TestException.class, TimeoutException.class}, maxRetries = 7, maxDuration = 20000)
@Timeout(100) // Scaled via config
@Asynchronous
public Future<Connection> serviceC() {
    Connection conn = null;
    counterForInvokingServiceC++;

    try {
        Thread.sleep(TCKConfig.getConfig().getTimeoutInMillis(5000));
        throw new RuntimeException("Timeout did not interrupt");
    } 
    catch (InterruptedException e) {
        //expected
    }
    return CompletableFuture.completedFuture(conn);
}
 
Example #3
Source File: DisableFTEnableOnMethodTest.java    From microprofile-fault-tolerance with Apache License 2.0 6 votes vote down vote up
@Deployment
public static WebArchive deploy() {
   Asset config = new DisableConfigAsset()
           .enable(DisableAnnotationClient.class, "failAndRetryOnce", Retry.class)
           .enable(DisableAnnotationClient.class, "failWithCircuitBreaker", CircuitBreaker.class)
           .enable(DisableAnnotationClient.class, "failWithTimeout", Timeout.class)
           .enable(DisableAnnotationClient.class, "asyncWaitThenReturn", Asynchronous.class)
           .enable(DisableAnnotationClient.class, "failRetryOnceThenFallback", Fallback.class)
           .enable(DisableAnnotationClient.class, "waitWithBulkhead", Bulkhead.class)
           .disableGlobally();

    JavaArchive testJar = ShrinkWrap
        .create(JavaArchive.class, "ftDisableGloballyEnableMethod.jar")
        .addClasses(DisableAnnotationClient.class)
        .addPackage(Packages.UTILS)
        .addAsManifestResource(config, "microprofile-config.properties")
        .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
        .as(JavaArchive.class);

    WebArchive war = ShrinkWrap
        .create(WebArchive.class, "ftDisableGloballyEnableMethod.war")
        .addAsLibrary(testJar);
    return war;
}
 
Example #4
Source File: DisableAnnotationOnMethodsTest.java    From microprofile-fault-tolerance with Apache License 2.0 6 votes vote down vote up
@Deployment
public static WebArchive deploy() {
    Asset config = new DisableConfigAsset()
            .disable(DisableAnnotationClient.class, "failAndRetryOnce", Retry.class)
            .disable(DisableAnnotationClient.class, "failRetryOnceThenFallback", Fallback.class)
            .disable(DisableAnnotationClient.class, "failWithCircuitBreaker", CircuitBreaker.class)
            .disable(DisableAnnotationClient.class, "failWithTimeout", Timeout.class)
            .disable(DisableAnnotationClient.class, "asyncWaitThenReturn", Asynchronous.class)
            .disable(DisableAnnotationClient.class, "waitWithBulkhead", Bulkhead.class);
    
    JavaArchive testJar = ShrinkWrap
        .create(JavaArchive.class, "ftDisableMethods.jar")
        .addClasses(DisableAnnotationClient.class)
        .addPackage(Packages.UTILS)
        .addAsManifestResource(config, "microprofile-config.properties")
        .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
        .as(JavaArchive.class);

    WebArchive war = ShrinkWrap
        .create(WebArchive.class, "ftDisableMethods.war")
        .addAsLibrary(testJar);
    return war;
}
 
Example #5
Source File: DisableFTEnableGloballyTest.java    From microprofile-fault-tolerance with Apache License 2.0 6 votes vote down vote up
@Deployment
public static WebArchive deploy() {
   Asset config = new DisableConfigAsset()
           .enable(Retry.class)
           .enable(CircuitBreaker.class)
           .enable(Timeout.class)
           .enable(Asynchronous.class)
           .enable(Fallback.class)
           .enable(Bulkhead.class)
           .disableGlobally();

    JavaArchive testJar = ShrinkWrap
        .create(JavaArchive.class, "ftDisableGlobalEnableClass.jar")
        .addClasses(DisableAnnotationClient.class)
        .addPackage(Packages.UTILS)
        .addAsManifestResource(config, "microprofile-config.properties")
        .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
        .as(JavaArchive.class);

    WebArchive war = ShrinkWrap
        .create(WebArchive.class, "ftDisableGlobalEnableClass.war")
        .addAsLibrary(testJar);
    return war;
}
 
Example #6
Source File: CircuitBreakerClientWithRetry.java    From microprofile-fault-tolerance with Apache License 2.0 6 votes vote down vote up
/**
 * Configured to always time out and Retry until CircuitBreaker is triggered on 4th call.
 */
@CircuitBreaker(successThreshold = 2, requestVolumeThreshold = 4, failureRatio = 0.75, delay = 50000)
@Retry(retryOn = {RuntimeException.class, TimeoutException.class}, maxRetries = 7, maxDuration = 20000)
@Timeout(100) // Scaled via config
public Connection serviceC() {
    Connection conn = null;
    counterForInvokingServiceC++;

    try {
        Thread.sleep(TCKConfig.getConfig().getTimeoutInMillis(5000));
        throw new RuntimeException("Timeout did not interrupt");
    } 
    catch (InterruptedException e) {
        //expected
    }
    return conn;
}
 
Example #7
Source File: DisableAnnotationGloballyTest.java    From microprofile-fault-tolerance with Apache License 2.0 6 votes vote down vote up
@Deployment
public static WebArchive deploy() {
    
    Asset config = new DisableConfigAsset()
            .disable(Retry.class)
            .disable(CircuitBreaker.class)
            .disable(Timeout.class)
            .disable(Asynchronous.class)
            .disable(Fallback.class)
            .disable(Bulkhead.class);
    
    JavaArchive testJar = ShrinkWrap
        .create(JavaArchive.class, "ftDisableGlobally.jar")
        .addClasses(DisableAnnotationClient.class)
        .addPackage(Packages.UTILS)
        .addAsManifestResource(config, "microprofile-config.properties")
        .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
        .as(JavaArchive.class);

    WebArchive war = ShrinkWrap
        .create(WebArchive.class, "ftDisableGlobally.war")
        .addAsLibrary(testJar);
    return war;
}
 
Example #8
Source File: CircuitBreakerSkipOnConfigTest.java    From microprofile-fault-tolerance with Apache License 2.0 6 votes vote down vote up
@Deployment
public static WebArchive create() {
    ConfigAnnotationAsset config = new ConfigAnnotationAsset();
    config.setGlobally(CircuitBreaker.class, "skipOn", TestConfigExceptionA.class.getCanonicalName());
    
    JavaArchive jar = ShrinkWrap
            .create(JavaArchive.class, "ftCircuitBreakerSkipOnConfig.jar")
            .addPackage(CircuitBreakerConfigTest.class.getPackage())
            .addPackage(Packages.UTILS)
            .addAsManifestResource(config, "microprofile-config.properties")
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    
    WebArchive war = ShrinkWrap
            .create(WebArchive.class, "ftCircuitBreakerSkipOnConfig.war")
            .addAsLibraries(jar);
    
    return war;
}
 
Example #9
Source File: CircuitBreakerClassLevelClientWithDelay.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@CircuitBreaker(successThreshold = 2, requestVolumeThreshold = 4, failureRatio = 0.75, delay = 1)
public Connection serviceD() {
    Connection conn = null;

    counterForInvokingService++;
    conn = connectionService();

    return conn;
}
 
Example #10
Source File: CircuitBreakerConfigBean.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
/**
 * This method's circuit breaker moves from closed to open after 10 consecutive failures,
 * unless failureRatio is configured differently.
 */
@CircuitBreaker(requestVolumeThreshold = 10, failureRatio = 1.0)
public void failureRatioMethod(boolean fail) {
    if (fail) {
        throw new TestConfigExceptionA();
    }
}
 
Example #11
Source File: DisableClient.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
/**
 * Invokes connection service and increases the counter for invocations of serviceC and connection service
 * @return Always throws exception
 */
@CircuitBreaker(successThreshold = 2, requestVolumeThreshold = 4, failureRatio = 0.75, delay = 50000)
public Connection serviceC() {
    Connection conn = null;
    counterForInvokingServiceC++;
    conn = connectionService();

    return conn;
}
 
Example #12
Source File: DisableAnnotationGloballyEnableOnClassTest.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Deployment
public static WebArchive deploy() {
   Asset config = new DisableConfigAsset()
           .disable(Retry.class)
           .disable(CircuitBreaker.class)
           .disable(Timeout.class)
           .disable(Asynchronous.class)
           .disable(Fallback.class)
           .disable(Bulkhead.class)
           .enable(DisableAnnotationClient.class, Retry.class)
           .enable(DisableAnnotationClient.class, CircuitBreaker.class)
           .enable(DisableAnnotationClient.class, Timeout.class)
           .enable(DisableAnnotationClient.class, Asynchronous.class)
           .enable(DisableAnnotationClient.class, Fallback.class)
           .enable(DisableAnnotationClient.class, Bulkhead.class);
    
    JavaArchive testJar = ShrinkWrap
        .create(JavaArchive.class, "ftDisableGlobalEnableClass.jar")
        .addClasses(DisableAnnotationClient.class)
        .addPackage(Packages.UTILS)
        .addAsManifestResource(config, "microprofile-config.properties")
        .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
        .as(JavaArchive.class);

    WebArchive war = ShrinkWrap
        .create(WebArchive.class, "ftDisableGlobalEnableClass.war")
        .addAsLibrary(testJar);
    return war;
}
 
Example #13
Source File: DisableAnnotationGloballyEnableOnMethodTest.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Deployment
public static WebArchive deploy() {
   Asset config = new DisableConfigAsset()
           .disable(Retry.class)
           .disable(CircuitBreaker.class)
           .disable(Timeout.class)
           .disable(Asynchronous.class)
           .disable(Fallback.class)
           .disable(Bulkhead.class)
           .enable(DisableAnnotationClient.class, "failAndRetryOnce", Retry.class)
           .enable(DisableAnnotationClient.class, "failWithCircuitBreaker", CircuitBreaker.class)
           .enable(DisableAnnotationClient.class, "failWithTimeout", Timeout.class)
           .enable(DisableAnnotationClient.class, "asyncWaitThenReturn", Asynchronous.class)
           .enable(DisableAnnotationClient.class, "failRetryOnceThenFallback", Fallback.class)
           .enable(DisableAnnotationClient.class, "waitWithBulkhead", Bulkhead.class);
    
    JavaArchive testJar = ShrinkWrap
        .create(JavaArchive.class, "ftDisableGloballyEnableMethod.jar")
        .addClasses(DisableAnnotationClient.class)
        .addPackage(Packages.UTILS)
        .addAsManifestResource(config, "microprofile-config.properties")
        .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
        .as(JavaArchive.class);

    WebArchive war = ShrinkWrap
        .create(WebArchive.class, "ftDisableGloballyEnableMethod.war")
        .addAsLibrary(testJar);
    return war;
}
 
Example #14
Source File: CircuitBreakerClientWithRetry.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
/**
 * Has a CircuitBreaker and Retries on CircuitBreakerOpenException
 * 
 * @param throwException whether this method should throw a TestException to simulate an application failure
 * @return string "OK"
 */
@CircuitBreaker(requestVolumeThreshold = 4, failureRatio = 0.75, delay = 1000)
@Retry(retryOn = CircuitBreakerOpenException.class, maxRetries = 20, delay = 100, jitter = 0)
// Scaled via config
public String serviceWithRetryOnCbOpen(boolean throwException) {
    if (throwException) {
        throw new TestException();
    }
    else {
        return "OK";
    }
}
 
Example #15
Source File: AsyncHelloService.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Asynchronous
@CircuitBreaker(requestVolumeThreshold = THRESHOLD, failureRatio = 0.5, delay = DELAY, successThreshold = 1)
public Future<String> hello(Result result) throws IOException {
    COUNTER.incrementAndGet();
    switch (result) {
        case FAILURE:
            throw new IOException("Simulated IO error");
        case COMPLETE_EXCEPTIONALLY:
            CompletableFuture<String> future = new CompletableFuture<>();
            future.completeExceptionally(new IOException("Simulated IO error"));
            return future;
        default:
            return completedFuture(OK);
    }
}
 
Example #16
Source File: CircuitBreakerClientWithRetry.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
/**
 * Has a CircuitBreaker and Retries on all exceptions except TestException and CircuitBreakerOpenException
 * 
 * @param throwException whether this method should throw a TestException to simulate an application failure
 * @return string "OK"
 */
@CircuitBreaker(requestVolumeThreshold = 4, failureRatio = 0.75, delay = 1000)
@Retry(abortOn = { TestException.class, CircuitBreakerOpenException.class }, maxRetries = 20, delay = 200)
// Scaled via config
public String serviceWithRetryFailOnCbOpen(boolean throwException) {
    if (throwException) {
        throw new TestException();
    }
    else {
        return "OK";
    }
}
 
Example #17
Source File: MyMicroservice.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@CircuitBreaker(successThreshold = 3, requestVolumeThreshold = 4, failureRatio = 0.75, delay = 200)
public String sayHelloBreakerHighThreshold() {
    sayHelloBreakerCount5++;
    System.err.printf("sayHelloBreakerHighThreshold, %d\n", sayHelloBreakerCount5);
    // Only two execution succeeds
    if (sayHelloBreakerCount5 < 5 || sayHelloBreakerCount5 > 6) {
        throw new RuntimeException("Connection failed");
    }
    return "sayHelloBreaker#" + sayHelloBreakerCount5;
}
 
Example #18
Source File: ClashingNameBean.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Retry(maxRetries = 5)
@Bulkhead(3)
@Timeout(value = 1000, unit = ChronoUnit.MILLIS)
@CircuitBreaker(failureRatio = 1.0, requestVolumeThreshold = 20)
@Fallback(fallbackMethod = "doFallback")
@Asynchronous
public Future<Void> doWork() {
    return CompletableFuture.completedFuture(null);
}
 
Example #19
Source File: PingService.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@CircuitBreaker(failOn = { IllegalArgumentException.class })
public String ping() {
    int count = counter.incrementAndGet();

    if (count % 2 == 0) {
        throw new IllegalArgumentException();
    } else {
        throw new IllegalStateException();
    }
}
 
Example #20
Source File: CircuitBreakerConfigTest.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Deployment
public static WebArchive create() {
    ConfigAnnotationAsset config = new ConfigAnnotationAsset()
        .set(CircuitBreakerConfigBean.class, "skipOnMethod", CircuitBreaker.class,
            "skipOn", TestConfigExceptionA.class.getName())
        .set(CircuitBreakerConfigBean.class, "failOnMethod", CircuitBreaker.class,
            "failOn", TestConfigExceptionA.class.getName())
        .set(CircuitBreakerConfigBean.class, "delayMethod", CircuitBreaker.class,
            "delay", TCKConfig.getConfig().getTimeoutInStr(1000))
        .set(CircuitBreakerConfigBean.class, "delayMethod", CircuitBreaker.class,
            "delayUnit", "MILLIS")
        .set(CircuitBreakerConfigBean.class, "requestVolumeThresholdMethod", CircuitBreaker.class,
            "requestVolumeThreshold", "4")
        .set(CircuitBreakerConfigBean.class, "failureRatioMethod", CircuitBreaker.class,
            "failureRatio","0.8")
        .set(CircuitBreakerConfigBean.class, "successThresholdMethod", CircuitBreaker.class,
            "successThreshold", "2")
        // only changing value here to scale the original, not for the purpose of this test
        .set(CircuitBreakerConfigBean.class, "successThresholdMethod", CircuitBreaker.class,
            "delay", TCKConfig.getConfig().getTimeoutInStr(1000));

    JavaArchive jar = ShrinkWrap
            .create(JavaArchive.class, "ftCircuitBreakerConfig.jar")
            .addClasses(CircuitBreakerConfigBean.class, TestConfigExceptionA.class, TestConfigExceptionB.class)
            .addPackage(Packages.UTILS)
            .addAsManifestResource(config, "microprofile-config.properties")
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

    return ShrinkWrap
            .create(WebArchive.class, "ftCircuitBreakerConfig.war")
            .addAsLibraries(jar);
}
 
Example #21
Source File: CircuitBreakerClientNoDelay.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@CircuitBreaker(successThreshold = 2, requestVolumeThreshold = 4, failureRatio=0.75, delay = 1)
public Connection serviceA() {
    Connection conn = null;
    counterForInvokingServiceA++;
    conn = connectionService();
    
    return conn;
}
 
Example #22
Source File: CircuitBreakerClientWithDelay.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@CircuitBreaker(successThreshold = 2, requestVolumeThreshold = 4, failureRatio=0.75, delay = 50000)
public Connection serviceA() {      
    Connection conn = null;
    counterForInvokingServiceA++;
    conn = connectionService();
    
    return conn;
}
 
Example #23
Source File: CircuitBreakerClassLevelClientWithDelay.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@CircuitBreaker(successThreshold = 2, requestVolumeThreshold = 2, failureRatio = 1, delay = 50000)
public Connection serviceC() {
    Connection conn = null;

    counterForInvokingService++;
    conn = connectionService();

    return conn;
}
 
Example #24
Source File: CircuitBreakerClientWithTimeout.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
/**
 * Sleeps for 1000ms, times out after 500ms
 * <p>
 * CircuitBreaker opens after two failed requests
 * 
 * @return should always throw TimeoutException, unless CircuitBreaker prevents execution
 */
@CircuitBreaker(successThreshold = 2, requestVolumeThreshold = 2, failureRatio = 0.75, delay = 50000)
@Timeout(500) // Adjusted by config
public String serviceWithTimeout() {
    try {
        Thread.sleep(TCKConfig.getConfig().getTimeoutInMillis(1000));
        fail("Thread not interrupted by timeout");
    }
    catch (InterruptedException e) {
        // Expected
    }
    return "OK";
}
 
Example #25
Source File: CircuitBreakerClientHigherSuccessThreshold.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@CircuitBreaker(successThreshold = 3, requestVolumeThreshold = 4, failureRatio=0.75, delay = 1000)
public Connection serviceA() {
    Connection conn = null;
    counterForInvokingServiceA++;
    conn = connectionService();
    
    return conn;
}
 
Example #26
Source File: CircuitBreakerClientWithRetryAsync.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@CircuitBreaker(successThreshold = 2, requestVolumeThreshold = 4, failureRatio = 0.75, delay = 50000)
@Retry(retryOn = {TestException.class}, maxRetries = 7)
@Asynchronous
public Future<Connection> serviceA() {
    Connection conn = null;
    counterForInvokingServiceA++;
    conn = connectionService();
    return CompletableFuture.completedFuture(conn);
}
 
Example #27
Source File: CircuitBreakerClientWithRetryAsync.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@CircuitBreaker(successThreshold = 2, requestVolumeThreshold = 4, failureRatio = 0.75, delay = 50000)
@Retry(retryOn = {TestException.class}, maxRetries = 2)
@Asynchronous
public Future<Connection> serviceB() {
    Connection conn = null;
    counterForInvokingServiceB++;
    conn = connectionService();
    return CompletableFuture.completedFuture(conn);
}
 
Example #28
Source File: CircuitBreakerClientWithRetryAsync.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
/**
 * Has a CircuitBreaker and Retries on CircuitBreakerOpenException
 * 
 * @param throwException whether this method should throw a runtime exception to simulate an application failure
 * @return string "OK"
 */
@CircuitBreaker(requestVolumeThreshold = 4, failureRatio = 0.75, delay = 1000)
@Retry(retryOn = CircuitBreakerOpenException.class, maxRetries = 20, delay = 100, jitter = 0)
@Asynchronous
// Scaled via config
public Future<String> serviceWithRetryOnCbOpen(boolean throwException) {
    if (throwException) {
        throw new TestException("Test Exception");
    }
    else {
        return CompletableFuture.completedFuture("OK");
    }
}
 
Example #29
Source File: CircuitBreakerClientWithRetryAsync.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
/**
 * Has a CircuitBreaker and Retries on TimeoutException
 * <p>
 * The method should never throw a TimeoutException so the retry should have no effect
 * 
 * @param throwException whether this method should throw a TestException to simulate an application failure
 * @return string "OK"
 */
@CircuitBreaker(requestVolumeThreshold = 4, failureRatio = 0.75, delay = 1000)
@Retry(retryOn = TimeoutException.class, maxRetries = 20, delay = 200)
@Asynchronous
// Delays scaled via config
public Future<String> serviceWithRetryOnTimeout(boolean throwException) {
    if (throwException) {
        throw new TestException("Test Exception");
    }
    else {
        return CompletableFuture.completedFuture("OK");
    }
}
 
Example #30
Source File: CircuitBreakerClientWithRetryAsync.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
/**
 * Has a CircuitBreaker and Retries on all exceptions except TestException and CircuitBreakerOpenException
 * 
 * @param throwException whether this method should throw a TestException to simulate an application failure
 * @return string "OK"
 */
@CircuitBreaker(requestVolumeThreshold = 4, failureRatio = 0.75, delay = 1000)
@Retry(abortOn = { TestException.class, CircuitBreakerOpenException.class }, maxRetries = 20, delay = 200)
@Asynchronous
// Delays scaled via config
public Future<String> serviceWithRetryFailOnCbOpen(boolean throwException) {
    if (throwException) {
        throw new TestException("Test Exception");
    }
    else {
        return CompletableFuture.completedFuture("OK");
    }
}