completable-future

How to invoke CompletableFuture callback while propagating result or error?

时光毁灭记忆、已成空白 提交于 2021-02-20 15:38:04
问题 I was trying .exceptionally and .handle but those don't seem to work. In scala, you can call a method on the future with a closure that is just like a finally block(it runs on exception AND on success) AND it propogates the exception or success up the chain as-is. I tried this... CompletableFuture<Object> future = newFuture.handle((r, e) -> { if(r != null) return r; else if(e != null) return e; else return new RuntimeException("Asdf"); }); Assert.assertTrue(future.isCompletedExceptionally());

How to invoke CompletableFuture callback while propagating result or error?

北慕城南 提交于 2021-02-20 15:34:11
问题 I was trying .exceptionally and .handle but those don't seem to work. In scala, you can call a method on the future with a closure that is just like a finally block(it runs on exception AND on success) AND it propogates the exception or success up the chain as-is. I tried this... CompletableFuture<Object> future = newFuture.handle((r, e) -> { if(r != null) return r; else if(e != null) return e; else return new RuntimeException("Asdf"); }); Assert.assertTrue(future.isCompletedExceptionally());

How to invoke CompletableFuture callback while propagating result or error?

拈花ヽ惹草 提交于 2021-02-20 15:34:08
问题 I was trying .exceptionally and .handle but those don't seem to work. In scala, you can call a method on the future with a closure that is just like a finally block(it runs on exception AND on success) AND it propogates the exception or success up the chain as-is. I tried this... CompletableFuture<Object> future = newFuture.handle((r, e) -> { if(r != null) return r; else if(e != null) return e; else return new RuntimeException("Asdf"); }); Assert.assertTrue(future.isCompletedExceptionally());

How to invoke CompletableFuture callback while propagating result or error?

妖精的绣舞 提交于 2021-02-20 15:32:10
问题 I was trying .exceptionally and .handle but those don't seem to work. In scala, you can call a method on the future with a closure that is just like a finally block(it runs on exception AND on success) AND it propogates the exception or success up the chain as-is. I tried this... CompletableFuture<Object> future = newFuture.handle((r, e) -> { if(r != null) return r; else if(e != null) return e; else return new RuntimeException("Asdf"); }); Assert.assertTrue(future.isCompletedExceptionally());

Stopping a thread in java CompletableFuture after timeout

给你一囗甜甜゛ 提交于 2021-02-19 08:40:09
问题 I have an async chain in my java code that i want to stop after a certain timeout so i created a threadPool with some threads and called the CompletableFuture like this ExecutorService pool = Executors.newFixedThreadPool(10); than i have a cyclic method that loads data from the db and executes some task on it, once all the CompletableFutures are completed its doing it again CompletableFuture<MyObject> futureTask = CompletableFuture.supplyAsync(() -> candidate, pool) .thenApply(Task1::doWork)

How does Spring get the result from an endpoint that returns CompletableFuture object?

强颜欢笑 提交于 2021-02-07 20:25:23
问题 In the code below, when the endpoint getPerson gets hit, the response will be a JSON of type Person. How does Spring convert CompletableFuture<Person> to Person ? @RestController public class PersonController { @Autowired private PersonService personService; @GetMapping("/persons/{personId}" ) public CompletableFuture<Person> getPerson(@PathVariable("personId") Integer personId) { return CompletableFuture.supplyAsync(() -> personService.getPerson(personId)); } } 回答1: When the

How to avoid invoking CompletableFuture.thenCompose(x -> x)?

只愿长相守 提交于 2021-02-07 10:45:31
问题 I get the feeling that I am misusing the CompletableFuture API. When invoking CompletableFuture.exceptionally() I routinely find myself needing to invoke another asynchronous process, which means that exceptionally() returns CompletableFuture<CompletableFuture<T>> instead of CompletableFuture<T> . I then cast the result back using thenCompose(x -> x) . Here is a concrete example: CompletableFuture<Void> listenersNotified = CompletableFuture.supplyAsync(() -> { int result = expensiveOperation(

Return CompletableFuture<Void> or CompletableFuture<?>?

拜拜、爱过 提交于 2021-02-05 12:54:51
问题 I want to write an asynchronous method that returns a CompletableFuture. The only purpose of the future is to track when the method is complete, not its result. Would it be better to return CompletableFuture<Void> or CompletableFuture<?> ? Is there a reason to prefer one or the other, or are they interchangeable? CompletableFuture itself returns CompletableFuture<Void> from many of its methods. java.nio has a Future<Void> in AsynchronousSocketChannel: Future<Void> connect(SocketAddress remote

CompletableFuture, main never exits

余生长醉 提交于 2021-01-29 21:40:25
问题 I'm learning Java 8 and more in detail the "CompletableFuture". Following this interesting tutorial: https://www.callicoder.com/java-8-completablefuture-tutorial/ I wrote the following Java class : package parallels; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.stream.Collectors; import javax.ws.rs.client.ClientRequestFilter; import javax.ws.rs

CompletableFuture how to return first FALSE or wait until all are completed to return TRUE

青春壹個敷衍的年華 提交于 2021-01-29 18:52:07
问题 I have a bit of an odd situation that doesn’t seem to allow this peg to fit into any of the widely established CompletableFuture holes. Within a primary method that is evaluating returned booleans, I am wanting to allow calls to three different methods to complete asynchronously. Each of these three methods can return either a TRUE or a FALSE. If any return a FALSE, I want the evaluation to drop the remainder and simply return that FALSE value. The point being, it can be any of the three, not