Java源码示例:org.eclipse.microprofile.faulttolerance.Fallback

示例1
@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;
    }
}
 
示例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;
}
 
示例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;
}
 
示例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;
}
 
示例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;
}
 
示例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;
}
 
示例7
@Fallback(fallbackMethod = "fallback")
@Timeout(value = 1L)
@Retry(delay = 1L, maxRetries = 2)
@Asynchronous
public CompletionStage<String> hello() {
    tracer.buildSpan("hello").start().finish();
    throw new RuntimeException();
}
 
示例8
@Fallback(fallbackMethod = "asyncFallback")
@Timeout(value = 1500L)
@Retry(delay = 100L, maxRetries = 2)
@Asynchronous
public CompletionStage<String> asyncFoo() {
    mockTracer.buildSpan("asyncFoo").start().finish();
    throw new RuntimeException();
}
 
示例9
@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);
}
 
示例10
@Asynchronous
@Fallback(fallbackMethod = "fallbackCompletionStage")
public CompletionStage<String> helloCompletionStageFailingAsync(int counter) throws IOException {
    CompletableFuture<String> result = new CompletableFuture<>();
    result.completeExceptionally(new IOException());
    return result;
}
 
示例11
@Asynchronous
@Fallback(fallbackMethod = "fallbackFuture")
public Future<String> helloFutureFailingAsync(int counter) throws IOException {
    CompletableFuture<String> result = new CompletableFuture<>();
    result.completeExceptionally(new IOException());
    return result;
}
 
示例12
@Retry(maxRetries = 2)
@Fallback(fallbackMethod = "getFallback")
@CircuitBreaker(requestVolumeThreshold = 5)
public String ping() {
    counter.incrementAndGet();
    throw new IllegalStateException();
}
 
示例13
@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;
}
 
示例14
@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;
}
 
示例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");
    }
}
 
示例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");
}
 
示例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");
    }
}
 
示例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);
}
 
示例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));
}
 
示例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;
}
 
示例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;
}
 
示例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);
}
 
示例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);
}
 
示例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);
}
 
示例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();
    }
}
 
示例26
@Fallback(fallbackMethod = "fallback")
public String method(int a, Long... b) {
    throw new RuntimeException("test");
}
 
示例27
@Fallback(applyOn = {E1.class, E2.class}, skipOn = E0.class, fallbackMethod = "myFallback")
public String serviceC(Throwable exception) throws Throwable {
    throw exception;
}
 
示例28
@Fallback(fallbackMethod = "fallback")
@Timeout(500)
public List<Legume> list() {

    return manager.createQuery("SELECT l FROM Legume l").getResultList();
}
 
示例29
@Fallback(applyOn = {E0.class, E2.class}, skipOn = E1.class, fallbackMethod = "myFallback")
public String serviceA(Throwable exception) throws Throwable {
    throw exception;
}
 
示例30
@Fallback(fallbackMethod = "getFallback")
@CircuitBreaker(requestVolumeThreshold = 5, failOn = { IllegalArgumentException.class })
public String ping() {
    pingCounter.incrementAndGet();
    throw new IllegalStateException();
}