completable-future

How to create async stacktraces?

爷,独闯天下 提交于 2019-12-18 13:13:36
问题 UPDATE : The latest version of Intellij IDEA implements exactly what I'm looking for. The question is how to implement this outside of the IDE (so I can to dump async stack traces to log files), ideally without the use of an instrumenting agent. Ever since I converted my application from a synchronous to asynchronous model I am having problems debugging failures. When I use synchronous APIs, I always find my classes in exception stacktraces so I know where to begin looking if something goes

CompletableFuture allof(..).join() vs CompletableFuture.join()

最后都变了- 提交于 2019-12-18 07:19:27
问题 I am currently using CompletableFuture supplyAsync() method for submitting some tasks to common thread pool. Here is what code snippet looks like: final List<CompletableFuture<List<Test>>> completableFutures = resolvers.stream() .map(resolver -> supplyAsync(() -> task.doWork())) .collect(toList()); CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()])).join(); final List<Test> tests = new ArrayList<>(); completableFutures.stream() .map

How to implement CompletableFuture.allOf() that completes exceptionally once any of the futures fail?

心不动则不痛 提交于 2019-12-18 07:16:09
问题 I want to implement a hybrid of CompletableFuture.allOf() and CompletableFuture.anyOf() where the returned future completes successfully as soon as all of the elements complete successfully, or it completes exceptionally (with the same exception) as soon as any of the elements complete exceptionally. In the case of multiple elements failing, returning the exception of any of them is sufficient. Use-case I have a task that needs to aggregate sub-results returned by a list of CompletableFuture

CompletableFuture is not getting executed. If I use the ExecutorService pool its work as expected but not with the default forkJoin common pool

拜拜、爱过 提交于 2019-12-18 05:06:16
问题 I am trying to run the following class its getting terminated without executing the CompletableFuture. public class ThenApplyExample { public static void main(String[] args) throws Exception { //ExecutorService es = Executors.newCachedThreadPool(); CompletableFuture<Student> studentCompletableFuture = CompletableFuture.supplyAsync(() -> { try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } return 3; })// If I put executorservice created n commented above

Spring @Async with CompletableFuture

怎甘沉沦 提交于 2019-12-17 22:35:37
问题 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

unreported exception when throwing from a lambda in a Completable Future

做~自己de王妃 提交于 2019-12-17 21:24:09
问题 When I compile the code below, I get the following error: /home/prakashs/composite_indexes/src/main/java/com/spakai/composite/TwoKeyLookup.java:22: error: unreported exception NoMatchException; must be caught or declared to be thrown CompletableFuture<Set<V>> result = calling.thenCombine(called, (s1, s2) -> findCommonMatch(s1, s2)); The code: public CompletableFuture<Set<V>> lookup(K callingNumber, K calledNumber) throws NoMatchException { CompletableFuture<Set<V>> calling =

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

巧了我就是萌 提交于 2019-12-17 16:41:28
问题 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

Does CompletionStage always wrap exceptions in CompletionException?

≯℡__Kan透↙ 提交于 2019-12-17 16:16:54
问题 The CompletionStage Javadoc states: [...] if a stage's computation terminates abruptly with an (unchecked) exception or error, then all dependent stages requiring its completion complete exceptionally as well, with a CompletionException holding the exception as its cause. Seeing as exceptional completions always wrap exceptions in CompletionException why do exceptionally() , whenComplete() and handle() represent the exception as Throwable instead of CompletionException ? This matters because

MDC Logger with CompletableFuture

不羁岁月 提交于 2019-12-13 17:14:55
问题 I am using MDC Logger, which is perfectly working for me except in one case. Wherever in the code we have used CompletableFuture, for the created thread the MDC data is not getting passed to next thread and due to which Logs are failing. For example in the code I have used below snippet for creating new Thread. CompletableFuture.runAsync(() -> getAcountDetails(user)); And the result of logs as below 2019-04-29 11:44:13,690 INFO | /app/rest/controller/userdetails | f80fdc1f-8123-3932-a405

How to use CompletableFuture in java 8 to start an async task and let main thread finish and exit

醉酒当歌 提交于 2019-12-13 00:36:28
问题 I have the following code (more or less): ExecutorService executor = Executors.newFixedThreadPool(10); CompletableFuture .supplyAsync(()->{ return longRunningMethodThatReturnsBoolean(); }, executor) .thenAcceptAsync(taskResult -> { logResult(); executor.shutdown(); }, executor); This allows the code in the main thread to continue, however I was expecting the main thread to die when it finished and the future to keep working in it's own thread, but the main thread stays alive until the