Java Code Examples for java.util.concurrent.CompletableFuture#acceptEitherAsync()
The following examples show how to use
java.util.concurrent.CompletableFuture#acceptEitherAsync() .
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: ManagedExecutorTest.java From microprofile-context-propagation with Apache License 2.0 | 4 votes |
/** * When an already-contextualized Consumer or BiFunction is specified as the action/task, * the action/task runs with its already-captured context rather than * capturing and applying context per the configuration of the managed executor. * * @throws ExecutionException indicates test failure * @throws InterruptedException indicates test failure * @throws TimeoutException indicates test failure */ @Test public void contextOfContextualConsumerAndBiFunctionOverrideContextOfManagedExecutor() throws ExecutionException, InterruptedException, TimeoutException { ThreadContext labelContext = ThreadContext.builder() .propagated(Label.CONTEXT_NAME) .unchanged() .cleared(ThreadContext.ALL_REMAINING) .build(); ManagedExecutor executor = ManagedExecutor.builder() .propagated(Buffer.CONTEXT_NAME) .cleared(ThreadContext.ALL_REMAINING) .build(); try { Buffer.set(new StringBuffer("contextualBiFunctionOverride-buffer-1")); Label.set("contextualBiFunctionOverride-label-1"); BiFunction<Integer, Throwable, Integer> precontextualizedFunction1 = labelContext.contextualFunction((result, failure) -> { Assert.assertEquals(Label.get(), "contextualBiFunctionOverride-label-1", "Previously captured context type not found on thread."); Assert.assertEquals(Buffer.get().toString(), "", "Context type not cleared from thread."); return failure == null ? result : 100; }); Buffer.set(new StringBuffer("contextualBiFunctionOverride-buffer-2")); Label.set("contextualBiFunctionOverride-label-2"); BiFunction<Integer, Integer, Integer> precontextualizedFunction2 = labelContext.contextualFunction((i, j) -> { Assert.assertEquals(Label.get(), "contextualBiFunctionOverride-label-2", "Previously captured context type not found on thread."); Assert.assertEquals(Buffer.get().toString(), "", "Context type not cleared from thread."); return i - j; }); Buffer.set(new StringBuffer("contextualConsumerOverride-buffer-3")); Label.set("contextualConsumerOverride-label-3"); Consumer<Integer> precontextualizedConsumer3 = labelContext.contextualConsumer(i -> { Assert.assertEquals(Label.get(), "contextualConsumerOverride-label-3", "Previously captured context type not found on thread."); Assert.assertEquals(Buffer.get().toString(), "", "Context type not cleared from thread."); }); Buffer.set(new StringBuffer("contextualConsuemrOverride-buffer-4")); Label.set("contextualConsumerOverride-label-4"); Consumer<Integer> precontextualizedConsumer4 = labelContext.contextualConsumer(i -> { Assert.assertEquals(Label.get(), "contextualConsumerOverride-label-4", "Previously captured context type not found on thread."); Assert.assertEquals(Buffer.get().toString(), "", "Context type not cleared from thread."); }); BiFunction<Void, Void, String> normalFunction5 = (unused1, unused2) -> { Assert.assertEquals(Buffer.get().toString(), "contextualConsumerAndBiFunctionOverride-buffer-5", "Previously captured context type not found on thread."); Assert.assertEquals(Label.get(), "", "Context type not cleared from thread."); return "done"; }; Buffer.set(new StringBuffer("contextualConsumerAndBiFunctionOverride-buffer-5")); Label.set("contextualConsumerAndBiFunctionOverride-label-5"); CompletableFuture<Integer> stage0 = executor.failedFuture(new ArrayIndexOutOfBoundsException("Expected error.")); CompletableFuture<Integer> stage1 = stage0.handleAsync(precontextualizedFunction1); CompletableFuture<Integer> stage2 = executor.completedFuture(200).thenCombineAsync(stage1, precontextualizedFunction2); CompletableFuture<Void> stage3 = stage2.thenAccept(precontextualizedConsumer3); CompletableFuture<Void> stage4 = stage2.acceptEitherAsync(stage1, precontextualizedConsumer4); CompletableFuture<String> stage5 = stage4.thenCombine(stage3, normalFunction5); Assert.assertEquals(stage5.join(), "done", "Unexpected result for completion stage."); } finally { executor.shutdownNow(); // Restore original values Buffer.set(null); Label.set(null); } }
Example 2
Source File: CompletableFutureTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public <T> CompletableFuture<Void> acceptEither (CompletableFuture<T> f, CompletionStage<? extends T> g, Consumer<? super T> a) { return f.acceptEitherAsync(g, a); }
Example 3
Source File: CompletableFutureTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public <T> CompletableFuture<Void> acceptEither (CompletableFuture<T> f, CompletionStage<? extends T> g, Consumer<? super T> a) { return f.acceptEitherAsync(g, a, new ThreadExecutor()); }
Example 4
Source File: CompletableFutureTest.java From j2objc with Apache License 2.0 | 4 votes |
public <T> CompletableFuture<Void> acceptEither (CompletableFuture<T> f, CompletionStage<? extends T> g, Consumer<? super T> a) { return f.acceptEitherAsync(g, a); }
Example 5
Source File: CompletableFutureTest.java From j2objc with Apache License 2.0 | 4 votes |
public <T> CompletableFuture<Void> acceptEither (CompletableFuture<T> f, CompletionStage<? extends T> g, Consumer<? super T> a) { return f.acceptEitherAsync(g, a, new ThreadExecutor()); }