org.eclipse.microprofile.faulttolerance.Asynchronous Java Examples
The following examples show how to use
org.eclipse.microprofile.faulttolerance.Asynchronous.
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: AsyncRetryClient.java From microprofile-fault-tolerance with Apache License 2.0 | 6 votes |
/** * Service will retry a method returning a parallel execution of 2 CompletionStages. One of them configured to * always fail. * * @return a {@link CompletionStage} */ @Asynchronous @Retry(maxRetries = 3) public CompletionStage<String> serviceF() { countInvocationsServF++; if (countInvocationsServF < 3) { // fail 2 first invocations return CompletableFuture.supplyAsync(doTask(null), executor) .thenCombine(CompletableFuture.supplyAsync(doTask("Simulated error"), executor), (s, s2) -> s + " then " + s2); } else { return CompletableFuture.supplyAsync(doTask(null), executor) .thenCombine(CompletableFuture.supplyAsync(doTask(null), executor), (s, s2) -> s + " then " + s2); } }
Example #2
Source File: AsyncRetryClient.java From microprofile-fault-tolerance with Apache License 2.0 | 6 votes |
/** * Service will retry a method returning a CompletionStage and configured to completeExceptionally twice. * * @return a {@link CompletionStage} */ @Asynchronous @Retry(maxRetries = 3) public CompletionStage<String> serviceC() { countInvocationsServC++; CompletableFuture<String> future = new CompletableFuture<>(); if (countInvocationsServC < 3) { // fail 2 first invocations future.completeExceptionally(new IOException("Simulated error")); } else { future.complete("Success"); } return future; }
Example #3
Source File: AsyncTimeoutClient.java From microprofile-fault-tolerance with Apache License 2.0 | 6 votes |
/** * serviceA is a slow running service that will take 5 seconds in normal operation. Here it is * configured to time out after 2 seconds. * @return the result as a Future * @throws InterruptedException the interrupted exception */ @Timeout(2000) @Asynchronous public Future<Connection> serviceA() throws InterruptedException { Connection conn = new Connection() { { Thread.sleep(TCKConfig.getConfig().getTimeoutInMillis(5000)); } @Override public String getData() { return "serviceA DATA"; } }; return CompletableFuture.completedFuture(conn); }
Example #4
Source File: AsyncTimeoutClient.java From microprofile-fault-tolerance with Apache License 2.0 | 6 votes |
/** * serviceB is a fast running service that will take 0.5 seconds in normal operation. That is considerably quicker than * its configured time out of 2 seconds. * @return the result as a Future * @throws InterruptedException the interrupted exception */ @Timeout(2000) @Asynchronous public Future<Connection> serviceB() throws InterruptedException { Connection conn = new Connection() { { Thread.sleep(TCKConfig.getConfig().getTimeoutInMillis(500)); } @Override public String getData() { return "serviceB DATA"; } }; return CompletableFuture.completedFuture(conn); }
Example #5
Source File: CircuitBreakerClientWithRetryAsync.java From microprofile-fault-tolerance with Apache License 2.0 | 6 votes |
/** * 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 #6
Source File: AsyncRetryClient.java From microprofile-fault-tolerance with Apache License 2.0 | 6 votes |
/** * Service will retry a method returning a chained, running sequentially, CompletionStage configured to completeExceptionally twice. * * @return a {@link CompletionStage} */ @Asynchronous @Retry(maxRetries = 3) public CompletionStage<String> serviceD() { countInvocationsServD++; if (countInvocationsServD < 3) { // fail 2 first invocations return CompletableFuture.supplyAsync(doTask(null), executor) .thenCompose(s -> CompletableFuture.supplyAsync(doTask("Simulated error"), executor)); } else { return CompletableFuture.supplyAsync(doTask(null), executor) .thenCompose(s -> CompletableFuture.supplyAsync(doTask(null), executor)); } }
Example #7
Source File: FaultToleranceExtension.java From smallrye-fault-tolerance with Apache License 2.0 | 6 votes |
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 #8
Source File: DisableAnnotationOnClassTest.java From microprofile-fault-tolerance with Apache License 2.0 | 6 votes |
@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 #9
Source File: DisableAnnotationGloballyTest.java From microprofile-fault-tolerance with Apache License 2.0 | 6 votes |
@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 #10
Source File: DisableFTEnableOnClassTest.java From microprofile-fault-tolerance with Apache License 2.0 | 6 votes |
@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 #11
Source File: DisableAnnotationOnMethodsTest.java From microprofile-fault-tolerance with Apache License 2.0 | 6 votes |
@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 #12
Source File: DisableFTEnableOnMethodTest.java From microprofile-fault-tolerance with Apache License 2.0 | 6 votes |
@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 #13
Source File: DisableFTEnableGloballyTest.java From microprofile-fault-tolerance with Apache License 2.0 | 6 votes |
@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 #14
Source File: CircuitBreakerClientWithRetryAsync.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
@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 #15
Source File: AllMetricsBean.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
@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 #16
Source File: CircuitBreakerClientWithRetryAsync.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
@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 #17
Source File: BulkheadMetricBean.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
/** * 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 #18
Source File: CircuitBreakerClientWithRetryAsync.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
/** * 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"); } }
Example #19
Source File: CircuitBreakerClientWithRetryAsync.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
/** * 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 #20
Source File: DisableAnnotationGloballyEnableOnClassTest.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
@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 #21
Source File: DisableAnnotationGloballyEnableOnMethodTest.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
@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
Source File: BulkheadMethodAsynchronousDefaultBean.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
@Override @Bulkhead @Asynchronous public Future test(BackendTestDelegate action) throws InterruptedException { Utils.log("in business method of bean " + this.getClass().getName() ); return action.perform(); }
Example #23
Source File: Bulkhead55RapidRetry10MethodAsynchBean.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
@Override @Bulkhead(waitingTaskQueue = 5, value = 5) @Asynchronous @Retry(retryOn = { BulkheadException.class }, delay = 1, delayUnit = ChronoUnit.MICROS, 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 #24
Source File: Bulkhead3MethodAsynchronousBean.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
@Override @Bulkhead(3) @Asynchronous public Future test(BackendTestDelegate action) throws InterruptedException { Utils.log("in business method of bean " + this.getClass().getName() ); return action.perform(); }
Example #25
Source File: AsyncRetryClient.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
/** * Service will retry a method returning a CompletionStage and configured to always completeExceptionally. * * @return a {@link CompletionStage} */ @Asynchronous @Retry(maxRetries = 2) public CompletionStage<String> serviceA() { countInvocationsServA++; // always fail CompletableFuture<String> future = new CompletableFuture<>(); future.completeExceptionally(new IOException("Simulated error")); return future; }
Example #26
Source File: Bulkhead10MethodAsynchronousBean.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
@Override @Bulkhead(10) @Asynchronous public Future test(BackendTestDelegate action) throws InterruptedException { Utils.log("in business method of bean " + this.getClass().getName() ); return action.perform(); }
Example #27
Source File: DisableAnnotationGloballyEnableOnClassDisableOnMethod.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
@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 #28
Source File: DisableAnnotationClient.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
/** * Returns a future which will be complete on method return if Asynchronous is disabled, * or incomplete if Asynchronous is enabled. * * @return Completed future */ @Asynchronous public Future<String> asyncWaitThenReturn() { try { Thread.sleep(2000); return CompletableFuture.completedFuture("OK"); } catch (InterruptedException e) { throw new RuntimeException(e); } }
Example #29
Source File: Service.java From smallrye-fault-tolerance with Apache License 2.0 | 5 votes |
@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 #30
Source File: InterceptorComponent.java From microprofile-fault-tolerance with Apache License 2.0 | 5 votes |
@InterceptEarly @InterceptLate @Asynchronous public Future<String> asyncGetString() { orderFactory.getOrderQueue().add("asyncGetString"); return CompletableFuture.completedFuture("OK"); }