org.eclipse.microprofile.faulttolerance.Fallback Java Examples

The following examples show how to use org.eclipse.microprofile.faulttolerance.Fallback. 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: FallbackConfigTest.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.set(FallbackConfigBean.class, "applyOnMethod", Fallback.class, "applyOn", TestConfigExceptionA.class.getCanonicalName());
    config.set(FallbackConfigBean.class, "skipOnMethod", Fallback.class, "skipOn", TestConfigExceptionA.class.getCanonicalName());
    config.set(FallbackConfigBean.class, "fallbackMethodConfig", Fallback.class, "fallbackMethod", "anotherFallback");
    config.set(FallbackConfigBean.class, "fallbackHandlerConfig", Fallback.class, "value", FallbackHandlerB.class.getName());
    
    JavaArchive jar = ShrinkWrap
            .create(JavaArchive.class, "ftFallbackConfigTest.jar")
            .addPackage(FallbackConfigTest.class.getPackage())
            .addPackage(Packages.UTILS)
            .addAsManifestResource(config, "microprofile-config.properties")
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    
    WebArchive war = ShrinkWrap
            .create(WebArchive.class, "ftFallbackConfigTest.war")
            .addAsLibraries(jar);
    return war;
}
 
Example #2
Source File: FallbackApplyOnConfigTest.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(Fallback.class, "applyOn", TestConfigExceptionA.class.getCanonicalName());
    
    JavaArchive jar = ShrinkWrap
            .create(JavaArchive.class, "ftFallbackApplyOnConfigTest.jar")
            .addPackage(FallbackConfigTest.class.getPackage())
            .addPackage(Packages.UTILS)
            .addAsManifestResource(config, "microprofile-config.properties")
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    
    WebArchive war = ShrinkWrap
            .create(WebArchive.class, "ftFallbackApplyOnConfigTest.war")
            .addAsLibraries(jar);
    return war;
}
 
Example #3
Source File: NotificationRetryBean.java    From sample-acmegifts with Eclipse Public License 1.0 6 votes vote down vote up
@Retry(maxRetries = 2)
@Fallback(NotificationFallbackHandler.class)
public OccasionResponse makeNotificationConnection(
    String message,
    Orchestrator orchestrator,
    String jwtTokenString,
    String notification11ServiceUrl,
    String twitterHandle,
    String notificationServiceUrl)
    throws IOException {

  JsonBuilderFactory factory = Json.createBuilderFactory(null);
  JsonObjectBuilder builder = factory.createObjectBuilder();
  JsonObject notificationRequestPayload = builder.add(JSON_KEY_NOTIFICATION, message).build();
  Response notificationResponse =
      orchestrator.makeConnection(
          "POST", notificationServiceUrl, notificationRequestPayload.toString(), jwtTokenString);
  OccasionResponse occasionResponse =
      new OccasionResponse(notificationResponse, OccasionResponse.NOTIFICATION_TYPE_LOG, null);

  return occasionResponse;
}
 
Example #4
Source File: CoffeeResource.java    From quarkus-quickstarts with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/{id}/recommendations")
@Timeout(250)
@Fallback(fallbackMethod = "fallbackRecommendations")
public List<Coffee> recommendations(@PathParam int id) {
    long started = System.currentTimeMillis();
    final long invocationNumber = counter.getAndIncrement();

    try {
        randomDelay();
        LOGGER.infof("CoffeeResource#recommendations() invocation #%d returning successfully", invocationNumber);
        return coffeeRepository.getRecommendations(id);
    } catch (InterruptedException e) {
        LOGGER.errorf("CoffeeResource#recommendations() invocation #%d timed out after %d ms",
                invocationNumber, System.currentTimeMillis() - started);
        return null;
    }
}
 
Example #5
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 #6
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 #7
Source File: SuperCoolService.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Retry(maxRetries = 2)
@Fallback(fallbackMethod = "getFallback")
@CircuitBreaker(requestVolumeThreshold = 5)
public String ping() {
    counter.incrementAndGet();
    throw new IllegalStateException();
}
 
Example #8
Source File: Service.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Fallback(fallbackMethod = "asyncFallback")
@Timeout(value = 1500L)
@Retry(delay = 100L, maxRetries = 2)
@Asynchronous
public CompletionStage<String> asyncFoo() {
    mockTracer.buildSpan("asyncFoo").start().finish();
    throw new RuntimeException();
}
 
Example #9
Source File: AllMetricsBean.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@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 #10
Source File: HelloService.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Asynchronous
@Fallback(fallbackMethod = "fallbackFuture")
public Future<String> helloFutureFailingAsync(int counter) throws IOException {
    CompletableFuture<String> result = new CompletableFuture<>();
    result.completeExceptionally(new IOException());
    return result;
}
 
Example #11
Source File: FallbackMetricBean.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Fallback(value = FallbackMetricHandler.class, applyOn = TestException.class)
public Void doWorkWithHandler(Action action) {
    if (action == Action.PASS) {
        return null;
    }
    else if (action == Action.FAIL){
        throw new TestException();
    }
    else {
        throw new NonFallbackException();
    }
}
 
Example #12
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(String dummy) {
    return CompletableFuture.completedFuture(null);
}
 
Example #13
Source File: HelloService.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Asynchronous
@Fallback(fallbackMethod = "fallbackCompletionStage")
public CompletionStage<String> helloCompletionStageFailingAsync(int counter) throws IOException {
    CompletableFuture<String> result = new CompletableFuture<>();
    result.completeExceptionally(new IOException());
    return result;
}
 
Example #14
Source File: Service.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Fallback(fallbackMethod = "fallback")
@Timeout(value = 1L)
@Retry(delay = 1L, maxRetries = 2)
@Asynchronous
public CompletionStage<String> hello() {
    tracer.buildSpan("hello").start().finish();
    throw new RuntimeException();
}
 
Example #15
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 #16
Source File: MyMicroservice.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Asynchronous
@Timeout(200)
@Fallback(FutureStringFallbackHandler.class)
public Future<String> sayHelloAsyncTimeoutFallback() {
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    return CompletableFuture.completedFuture(HELLO);
}
 
Example #17
Source File: RetryTestBean.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Retry(retryOn = OutOfMemoryError.class)
@Fallback(fallbackMethod = "fallback")
@Timeout(100L)
public String callWithRetryOnOutOfMemoryError() {
    int attempt = this.attempt.getAndIncrement();
    if (attempt == 0) {
        sleep(5000L);
    }
    return "call" + attempt;
}
 
Example #18
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 #19
Source File: RetryTestBean.java    From smallrye-fault-tolerance with 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 #20
Source File: AsyncHelloService.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Asynchronous
@Fallback(fallbackMethod = "fallback")
public Future<String> hello(Result result) throws IOException {
    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("Hello");
    }
}
 
Example #21
Source File: AsyncHelloService.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@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 #22
Source File: AsyncHelloService.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Asynchronous
@Fallback(fallbackMethod = "fallback")
public CompletionStage<String> hello(Result result) throws IOException {
    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("Hello");
    }
}
 
Example #23
Source File: AsyncHelloService.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Asynchronous
@Retry(retryOn = IOException.class)
@CircuitBreaker(failOn = IOException.class, requestVolumeThreshold = 5, successThreshold = 3, delay = 2, delayUnit = ChronoUnit.SECONDS, failureRatio = 0.75)
@Fallback(fallbackMethod = "fallback", applyOn = { IOException.class, CircuitBreakerOpenException.class })
public CompletionStage<String> helloFailAsync(int counter) {
    // 3/4 requests trigger IOException
    if (counter % 4 != 0) {
        CompletableFuture<String> result = new CompletableFuture<>();
        result.completeExceptionally(new IOException("Simulated IOException"));
        return result;
    }

    return completedFuture("Hello" + counter);
}
 
Example #24
Source File: DisableAnnotationGloballyEnableOnClassDisableOnMethod.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@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 #25
Source File: FaultToleranceOperation.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
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 #26
Source File: FallbackMethodGenericComplexBeanA.java    From microprofile-fault-tolerance with Apache License 2.0 4 votes vote down vote up
@Fallback(fallbackMethod = "fallback")
public String method(List<Set<String>> a) {
    throw new RuntimeException("test");
}
 
Example #27
Source File: AsyncFallbackClient.java    From microprofile-fault-tolerance with Apache License 2.0 4 votes vote down vote up
/**
 * Always throws an exception directly, so should fallback.
 */
@Asynchronous
@Fallback(fallbackMethod = "fallback")
public Future<String> service2() throws IOException {
    throw new IOException("Simulated error");
}
 
Example #28
Source File: AsyncFallbackClient.java    From microprofile-fault-tolerance with Apache License 2.0 4 votes vote down vote up
/**
 * Always throws an exception directly, so should fallback.
 */
@Asynchronous
@Fallback(fallbackMethod = "fallbackCS")
public CompletionStage<String> serviceCS2() throws IOException {
    throw new IOException("Simulated error");
}
 
Example #29
Source File: AsyncFallbackClient.java    From microprofile-fault-tolerance with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a CompletionStage which always completes successfully, so should NOT fallback.
 */
@Asynchronous
@Fallback(fallbackMethod = "fallbackCS")
public CompletionStage<String> serviceCS1() {
    return CompletableFuture.completedFuture("Success");
}
 
Example #30
Source File: FallbackClientWithBothFallbacks.java    From microprofile-fault-tolerance with Apache License 2.0 4 votes vote down vote up
/**
 * Retry 5 times and then fallback
 * @return a dummy number
 */
@Retry(maxRetries = 4)
@Fallback(value = IncompatibleFallbackHandler.class, fallbackMethod="serviceBFallback")
public int serviceB() {
    return 42;
}