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

The following examples show how to use org.eclipse.microprofile.faulttolerance.Fallback. 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
Source Project: quarkus-quickstarts   Source File: CoffeeResource.java    License: 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 2
@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 3
@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 4
@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 5
@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 6
@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 7
Source Project: smallrye-fault-tolerance   Source File: Service.java    License: 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 8
Source Project: smallrye-fault-tolerance   Source File: Service.java    License: 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 Project: smallrye-fault-tolerance   Source File: MyMicroservice.java    License: 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 10
Source Project: smallrye-fault-tolerance   Source File: HelloService.java    License: 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 11
Source Project: smallrye-fault-tolerance   Source File: HelloService.java    License: 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 12
Source Project: smallrye-fault-tolerance   Source File: SuperCoolService.java    License: 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 13
Source Project: smallrye-fault-tolerance   Source File: RetryTestBean.java    License: 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 14
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 15
@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 16
@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 17
@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 18
@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 19
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 20
@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 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
@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 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(String dummy) {
    return CompletableFuture.completedFuture(null);
}
 
Example 24
@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 25
@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 26
@Fallback(fallbackMethod = "fallback")
public String method(int a, Long... b) {
    throw new RuntimeException("test");
}
 
Example 27
@Fallback(applyOn = {E1.class, E2.class}, skipOn = E0.class, fallbackMethod = "myFallback")
public String serviceC(Throwable exception) throws Throwable {
    throw exception;
}
 
Example 28
Source Project: quarkus-in-prod   Source File: LegumeResource.java    License: Apache License 2.0 4 votes vote down vote up
@Fallback(fallbackMethod = "fallback")
@Timeout(500)
public List<Legume> list() {

    return manager.createQuery("SELECT l FROM Legume l").getResultList();
}
 
Example 29
@Fallback(applyOn = {E0.class, E2.class}, skipOn = E1.class, fallbackMethod = "myFallback")
public String serviceA(Throwable exception) throws Throwable {
    throw exception;
}
 
Example 30
Source Project: smallrye-fault-tolerance   Source File: PingService.java    License: Apache License 2.0 4 votes vote down vote up
@Fallback(fallbackMethod = "getFallback")
@CircuitBreaker(requestVolumeThreshold = 5, failOn = { IllegalArgumentException.class })
public String ping() {
    pingCounter.incrementAndGet();
    throw new IllegalStateException();
}