Java Code Examples for org.eclipse.microprofile.faulttolerance.Bulkhead

The following examples show how to use org.eclipse.microprofile.faulttolerance.Bulkhead. These examples are extracted from open source projects. 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
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
@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 3
@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
@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
@Deployment
public static WebArchive deploy() {
   Asset config = new DisableConfigAsset()
           .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)
           .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
@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 7
@Deployment
public static WebArchive deploy() {
   Asset config = new DisableConfigAsset()
           .disable(DisableAnnotationClient.class, Retry.class)
           .disable(DisableAnnotationClient.class, CircuitBreaker.class)
           .disable(DisableAnnotationClient.class, Timeout.class)
           .disable(DisableAnnotationClient.class, Asynchronous.class)
           .disable(DisableAnnotationClient.class, Fallback.class)
           .disable(DisableAnnotationClient.class, Bulkhead.class);
    
    JavaArchive testJar = ShrinkWrap
        .create(JavaArchive.class, "ftDisableClass.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, "ftDisableClass.war")
        .addAsLibrary(testJar);
    return war;
}
 
Example 8
@Deployment
public static WebArchive create() {
    ConfigAnnotationAsset config = new ConfigAnnotationAsset()
            .set(BulkheadConfigBean.class, "serviceValue", Bulkhead.class, "value", "1")
            .set(BulkheadConfigBean.class, "serviceWaitingTaskQueue", Bulkhead.class, "waitingTaskQueue", "1");
    
    JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "ftBulkheadConfig.jar")
                                .addClass(BulkheadConfigBean.class)
                                .addPackage(Packages.UTILS)
                                .addAsManifestResource(config, "microprofile-config.properties")
                                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

    WebArchive war = ShrinkWrap.create(WebArchive.class, "ftBulkheadConfig.war")
                               .addAsLibrary(jar);
    
    return war;
}
 
Example 9
@Asynchronous
@Bulkhead(value = 5, waitingTaskQueue = 10)
private Future writeSomeLogging(String item) {
        LOGGER.info("New Customer order at: "+new java.util.Date());
        LOGGER.info("Item: {}", item);
        return CompletableFuture.completedFuture("ok");
}
 
Example 10
Source Project: smallrye-fault-tolerance   Source File: PingService.java    License: Apache License 2.0 5 votes vote down vote up
@Asynchronous
@Bulkhead(value = BulkheadTest.QUEUE_SIZE, waitingTaskQueue = BulkheadTest.QUEUE_SIZE)
public Future<String> ping(CountDownLatch startLatch, CountDownLatch endLatch) throws InterruptedException {
    if (startLatch != null) {
        startLatch.countDown();
    }
    if (endLatch != null) {
        if (endLatch.await(1500, TimeUnit.MILLISECONDS)) {
            return CompletableFuture.completedFuture("pong");
        } else {
            return CompletableFuture.completedFuture("timeout");
        }
    }
    return CompletableFuture.completedFuture("null");
}
 
Example 11
Source Project: smallrye-fault-tolerance   Source File: RetryTestBean.java    License: Apache License 2.0 5 votes vote down vote up
@Retry(retryOn = BulkheadException.class, delay = 500)
@Bulkhead(2)
@Fallback(fallbackMethod = "fallback")
public String callWithRetryOnBulkhead() {
    int attempt = this.attempt.getAndIncrement();
    // both first attempts should take long time
    // without @Asynchronous, the third attempt should fail right away with BulkheadException and should be retried
    // the third attempt should be retried in 500 ms, after the first two calls were processed and should be successful
    // no fallback should be called
    if (attempt < 2) {
        sleep(300L);
    }
    return "call" + attempt;
}
 
Example 12
Source Project: smallrye-fault-tolerance   Source File: RetryTestBean.java    License: Apache License 2.0 5 votes vote down vote up
@Retry(retryOn = TimeoutException.class, delay = 500)
@Bulkhead(2)
public String callWithNoRetryOnBulkhead() {
    int attempt = this.attempt.getAndIncrement();
    if (attempt < 2) {
        sleep(300L);
    }
    return "call" + attempt;
}
 
Example 13
Source Project: smallrye-fault-tolerance   Source File: RetryTestBean.java    License: Apache License 2.0 5 votes vote down vote up
@Retry(retryOn = TimeoutException.class, delay = 500)
@Bulkhead(2)
@Fallback(fallbackMethod = "fallback")
public String callWithFallbackAndNoRetryOnBulkhead() {
    int attempt = this.attempt.getAndIncrement();
    if (attempt < 2) {
        sleep(300L);
    }
    return "call" + attempt;
}
 
Example 14
@Asynchronous
@Bulkhead(value = 15, waitingTaskQueue = 15)
@Timeout(value = 1, unit = ChronoUnit.SECONDS)
@Fallback(fallbackMethod = "fallback")
public CompletionStage<String> bulkheadTimeout(boolean fail) throws InterruptedException {
    if (fail) {
        Thread.sleep(2000);
    }
    return completedFuture("Hello from @Bulkhead @Timeout method");
}
 
Example 15
public static FaultToleranceOperation of(AnnotatedMethod<?> annotatedMethod) {
    return new FaultToleranceOperation(annotatedMethod.getDeclaringType().getJavaClass(), annotatedMethod.getJavaMember(),
            isAsync(annotatedMethod),
            returnsCompletionStage(annotatedMethod),
            getConfig(Bulkhead.class, annotatedMethod, BulkheadConfig::new),
            getConfig(CircuitBreaker.class, annotatedMethod, CircuitBreakerConfig::new),
            getConfig(Fallback.class, annotatedMethod, FallbackConfig::new),
            getConfig(Retry.class, annotatedMethod, RetryConfig::new),
            getConfig(Timeout.class, annotatedMethod, TimeoutConfig::new));
}
 
Example 16
public static FaultToleranceOperation of(Class<?> beanClass, Method method) {
    return new FaultToleranceOperation(beanClass, method,
            isAsync(method, beanClass),
            returnsCompletionStage(method),
            getConfig(Bulkhead.class, beanClass, method, BulkheadConfig::new),
            getConfig(CircuitBreaker.class, beanClass, method, CircuitBreakerConfig::new),
            getConfig(Fallback.class, beanClass, method, FallbackConfig::new),
            getConfig(Retry.class, beanClass, method, RetryConfig::new),
            getConfig(Timeout.class, beanClass, method, TimeoutConfig::new));
}
 
Example 17
Source Project: quarkus   Source File: BulkheadBean.java    License: Apache License 2.0 5 votes vote down vote up
@Bulkhead(5)
public int bulkhead() {
    int i = integer.incrementAndGet();
    try {
        Thread.sleep(10);//artificially generates contention
        return i;
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } finally {
        integer.decrementAndGet();
    }
}
 
Example 18
/**
 * Blocks waiting for {@code waitingFuture} to complete
 * <p>
 * If passed an already completed {@link Future}, this method will return immediately.
 * <p>
 * Should permit two simultaneous calls if bulkhead enabled, or more if bulkhead disabled.
 * 
 * @param waitingFuture the future to wait for
 */
@Bulkhead(2)
public void waitWithBulkhead(Future<?> waitingFuture) {
    try {
        tracker.executionStarted();
        waitingFuture.get();
    }
    catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }
    finally {
        tracker.executionEnded();
    }
}
 
Example 19
@Deployment
public static WebArchive deploy() {
    
    final 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)
            .disable(DisableAnnotationClient.class, "failAndRetryOnce", Retry.class)
            .disable(DisableAnnotationClient.class, "failWithCircuitBreaker", CircuitBreaker.class)
            .disable(DisableAnnotationClient.class, "failWithTimeout", Timeout.class)
            .disable(DisableAnnotationClient.class, "asyncWaitThenReturn", Asynchronous.class)
            .disable(DisableAnnotationClient.class, "failRetryOnceThenFallback", Fallback.class)
            .disable(DisableAnnotationClient.class, "waitWithBulkhead", Bulkhead.class);

    final ConfigAnnotationAsset mpAnnotationConfig = new ConfigAnnotationAsset()
        .setValue(DisableAnnotationClient.class,"failWithTimeout",Timeout.class,getConfig().getTimeoutInStr(500))
        .mergeProperties(((DisableConfigAsset) config).getProps());

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

    WebArchive war = ShrinkWrap
        .create(WebArchive.class, "ftDisableGloballyEnableClassDisableMethod.war")
        .addAsLibrary(testJar);
    return war;
}
 
Example 20
@Deployment
public static WebArchive deploy() {
   Asset config = new DisableConfigAsset()
           .disable(DisableAnnotationClient.class, Retry.class)
           .disable(DisableAnnotationClient.class, CircuitBreaker.class)
           .disable(DisableAnnotationClient.class, Timeout.class)
           .disable(DisableAnnotationClient.class, Asynchronous.class)
           .disable(DisableAnnotationClient.class, Fallback.class)
           .disable(DisableAnnotationClient.class, 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, "ftDisableClassEnableMethod.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, "ftDisableClassEnableMethod.war")
        .addAsLibrary(testJar);
    return war;
}
 
Example 21
@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 22
@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 23
@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 24
@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(String dummy) {
    return CompletableFuture.completedFuture(null);
}
 
Example 25
@Retry(maxRetries = 5)
@Bulkhead(3)
@Timeout(value = 1, unit = ChronoUnit.MINUTES)
@CircuitBreaker(failureRatio = 1.0, requestVolumeThreshold = 20)
@Fallback(fallbackMethod = "doFallback")
@Asynchronous
public Future<Void> doWork() {
    return CompletableFuture.completedFuture(null);
}
 
Example 26
/**
 * WaitFor method for testing async calls
 *
 * @param future to complete
 * @return a completed future set to null
 */
@Asynchronous
@Bulkhead(value = 2, waitingTaskQueue = 2)
public Future<Void> waitForAsync(Future<?> future) {
    doWaitFor(future);
    return CompletableFuture.completedFuture(null);
}
 
Example 27
@Override
@Bulkhead(value = 10, waitingTaskQueue = 10)
@Asynchronous
public Future test(BackendTestDelegate action) throws InterruptedException {
    Utils.log("in business method of bean " + this.getClass().getName() );
    return action.perform();
}
 
Example 28
@Override
@Bulkhead(value = 5)
@Retry(retryOn =
{ BulkheadException.class }, delay = 1, delayUnit = ChronoUnit.SECONDS, maxRetries = 20, maxDuration=999999)
public Future test(BackendTestDelegate action) throws InterruptedException {
    Utils.log("in business method of bean " + this.getClass().getName());
    return action.perform();
}
 
Example 29
@Override
@Bulkhead(waitingTaskQueue = 5, value = 5)
@Asynchronous
@Retry(retryOn =
{ BulkheadException.class }, delay = 1, delayUnit = ChronoUnit.SECONDS, maxRetries = 10, maxDuration=999999)
public Future test(BackendTestDelegate action) throws InterruptedException {
    Utils.log("in business method of bean " + this.getClass().getName());
    return action.perform();
}
 
Example 30
@Override
@Bulkhead
@Asynchronous
public Future test(BackendTestDelegate action) throws InterruptedException {
    Utils.log("in business method of bean " + this.getClass().getName() );
    return action.perform();
}