completable-future

Spring @Async with CompletableFuture

落爺英雄遲暮 提交于 2019-11-28 18:25:54
I have a doubt about this code: @Async public CompletableFuture<String> doFoo() { CompletableFuture<String> fooFuture = new CompletableFuture<>(); try { String fooResult = longOp(); fooFuture.complete(fooResult); } catch (Exception e) { fooFuture.completeExceptionally(e); } return fooFuture; } The question is: does doFoo return fooFuture only after longOp has finished (either correctly or exceptionally) and is therefore returning already completed futures or is Spring doing some magic and returning before executing the body? If the code is blocking on longOp(), how would you express that the

CompletableFuture | thenApply vs thenCompose

谁都会走 提交于 2019-11-28 16:36:41
I can't get my head around the difference between thenApply( ) and thenCompose() . So, could someone provide a valid use case? From the Java docs: thenApply(Function<? super T,? extends U> fn) Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied function. thenCompose(Function<? super T,? extends CompletionStage<U>> fn) Returns a new CompletionStage that, when this stage completes normally, is executed with this stage as the argument to the supplied function. I get that the 2nd argument of thenCompose

CompletableFuture: Waiting for first one normally return?

跟風遠走 提交于 2019-11-28 11:55:00
I have some CompletableFuture s and I want to run them in parallel, waiting for the first that returns normally . I know I can use CompletableFuture.anyOf to wait for the first to return, but this will return normally or exceptionally . I want to ignore exceptions. List<CompletableFuture<?>> futures = names.stream().map( (String name) -> CompletableFuture.supplyAsync( () -> // this calling may throw exceptions. new Task(name).run() ) ).collect(Collectors.toList()); //FIXME Can not ignore exceptionally returned takes. Future any = CompletableFuture.anyOf(futures.toArray(new CompletableFuture<?>

Surprising behavior of Java 8 CompletableFuture exceptionally method

烈酒焚心 提交于 2019-11-28 08:03:46
I have encountered strange behavior of Java 8 CompletableFuture.exceptionally method. If I execute this code, it works fine and prints java.lang.RuntimeException CompletableFuture<String> future = new CompletableFuture<>(); future.completeExceptionally(new RuntimeException()); future.exceptionally(e -> { System.out.println(e.getClass()); return null; }); But if I add another step in the future processing like thenApply , the exception type changes to java.util.concurrent.CompletionException with the original exception wrapped inside. CompletableFuture<String> future = new CompletableFuture<>()

Java8 CompletableFuture recoverWith equivalent? eg exceptionally but return CompletableFuture<U>

孤街浪徒 提交于 2019-11-28 00:11:04
I don't see an obvious way to handle an exception with an asynchronous result. For example, if I want to retry an async operation. I would expect something like this, however handleAsync doesn't do what you think it does - it runs the callbacks on another thread asynchronously. Returning a CompletionStage here is not correct. Jeopardy question of the day: thenApply is to thenCompose as exceptionally is to what? CompletionStage<String> cf = askPong("cause error").handleAsync((x, t) -> { if (t != null) { return askPong("Ping"); } else { return x; } }); Where askPong asks an actor: public

Java 8 Supplier Exception handling with CompletableFuture

☆樱花仙子☆ 提交于 2019-11-27 14:51:12
Consider the following code public class TestCompletableFuture { BiConsumer<Integer, Throwable> biConsumer = (x,y) -> { System.out.println(x); System.out.println(y); }; public static void main(String args[]) { TestCompletableFuture testF = new TestCompletableFuture(); testF.start(); } public void start() { Supplier<Integer> numberSupplier = new Supplier<Integer>() { @Override public Integer get() { return SupplyNumbers.sendNumbers(); } }; CompletableFuture<Integer> testFuture = CompletableFuture.supplyAsync(numberSupplier).whenComplete(biConsumer); } } class SupplyNumbers { public static

CompletableFuture | thenApply vs thenCompose

若如初见. 提交于 2019-11-27 09:34:00
问题 I can't get my head around the difference between thenApply( ) and thenCompose() . So, could someone provide a valid use case? From the Java docs: thenApply(Function<? super T,? extends U> fn) Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied function. thenCompose(Function<? super T,? extends CompletionStage<U>> fn) Returns a new CompletionStage that, when this stage completes normally, is executed with

CompletableFuture already completed with an exception

孤人 提交于 2019-11-27 09:15:46
CompletableFuture.completedFuture() returns a CompletedFuture that is already completed with the given value. How do we construct a CompletableFuture that is already completed exceptionally? Meaning, instead of returning a value I want the future to throw an exception. I just found this: CompletableFuture<T> future = new CompletableFuture<>(); future.completeExceptionally(t); There is no static factory method, but the default constructor seems to do the job. Java 9 provides CompletableFuture.failedFuture​(Throwable ex) that does exactly that. 来源: https://stackoverflow.com/questions/49116855

CompletableFuture: Waiting for first one normally return?

我们两清 提交于 2019-11-27 06:35:43
问题 I have some CompletableFuture s and I want to run them in parallel, waiting for the first that returns normally . I know I can use CompletableFuture.anyOf to wait for the first to return, but this will return normally or exceptionally . I want to ignore exceptions. List<CompletableFuture<?>> futures = names.stream().map( (String name) -> CompletableFuture.supplyAsync( () -> // this calling may throw exceptions. new Task(name).run() ) ).collect(Collectors.toList()); //FIXME Can not ignore

Throwing checked exceptions with CompletableFuture

空扰寡人 提交于 2019-11-27 04:52:03
问题 Stackoverflow contains multiple questions about mixing checked exceptions with CompletableFuture . Here are a few examples: Checked exception with CompletableFuture Java 8 Supplier Exception handling JDK8 CompletableFuture.supplyAsync how to deal with interruptedException While some of answers hint at the use of CompletableFuture.completeExceptionally() their approach results in user code that is difficult to read. I will use this space to provide an alternate solution that results in