completable-future

CompletableFuture — Aggregate Future to Fail Fast

北城余情 提交于 2019-12-04 09:19:59
I have been using the CompletableFuture.allOf(...) helper to create aggregate futures that will only become "done" when their composite futures are marked as complete, i.e: CompletableFuture<?> future1 = new CompletableFuture<>(); CompletableFuture<?> future2 = new CompletableFuture<>(); CompletableFuture<?> future3 = new CompletableFuture<>(); CompletableFuture<?> future = CompletableFuture.allOf(future1, future2, future3); I would like a slight variation on this functionality, where the aggregate future is market as complete when: All futures have completed successfully OR Any one future has

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-04 09:13:32
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, programme work as expected. .thenApply(i -> { for (int j = 0; j <= i; j++) { System.out.println(

Spring promoting request scoped bean to child threads (HttpServletRequest)

妖精的绣舞 提交于 2019-12-04 03:17:55
I tried a lot of things now but i seem to miss a piece of the puzzle. Here is the story: I have a request scoped bean that reads some SessionContext from the HttpServletRequest. This attribute is set in a filter. So this is working absolutely fine while the code runs on the correct thread. @Component @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES) public class SessionContextProviderImpl implements SessionContextProvider<SessionContext> { private final HttpServletRequest _request; @Autowired public SessionContextProviderImpl(HttpServletRequest request

Java 8 CompletableFuture , Stream and Timeouts

眉间皱痕 提交于 2019-12-03 22:25:02
问题 i'm trying to process some amount of data concurrently using CompletableFuture and Stream So far i have: public static void main(String[] args) throws InterruptedException, ExecutionException { System.out.println("start"); List<String> collect = Stream.of("1", "2", "3", "4", "5", "6", "7") .map(x -> CompletableFuture.supplyAsync(getStringSupplier(x))) .collect(Collectors.toList()) .stream() .map(CompletableFuture::join) .collect(Collectors.toList()); System.out.println("stop out!"); } public

Java Future - Spring Authentication is null into AuditorAware

情到浓时终转凉″ 提交于 2019-12-03 21:42:18
问题 This is my scenario: My app has Mongo Auditing enabled, with a custom AuditorAware which gets the current user from the SecurityContext . This works well with synchronous methods, and the current auditor is successfully saved, but I can't make it work properly with @Async methods. I have an async method ( CompletableFuture ) that makes some updates on my Mongo Database. When the AuditorAware.getCurrentAuditor() is called, no authentication info exists, and I can't get the current auditor (

What is the recommended way to wait till the Completable future threads finish

家住魔仙堡 提交于 2019-12-03 16:03:00
问题 I am using CompletableFuture as shown below in the code. But concerning the way I should wait till all runnables finish, I found two ways and I do not know the difference between them and which one is the best practice? They are as follows: Code : this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedSERun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor); this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedNWRun(this.saliencyMat, this.seedXY, this

Which executor is used when composing Java CompletableFutures?

空扰寡人 提交于 2019-12-03 15:20:45
I have a method on some repository class that returns a CompletableFuture . The code that completes these futures uses a third party library which blocks. I intend to have a separate bounded Executor which this repository class will use to make these blocking calls. Here is an example: public class PersonRepository { private Executor executor = ... public CompletableFuture<Void> create(Person person) {...} public CompletableFuture<Boolean> delete(Person person) {...} } The rest of my application will compose these futures and do some other things with the results. When these other functions

Most efficient way to stream on list of Futures

孤者浪人 提交于 2019-12-03 15:02:30
I'm calling an async client method by streaming over a list of objects. The method returns Future. What's the best way to iterate over the list of Futures returned after the call (so as to process those Future which comes first)? Note: The async client only returns Future not CompletableFuture. Following is the code: List<Future<Object>> listOfFuture = objectsToProcess.parallelStream() .map((object) -> { /* calling an async client returning a Future<Object> */ }) .collect(Collectors.toList()); Eugene Having this list of List<Future<Object>> , I would submit it to a custom pool, instead of

Thread vs CompletableFuture

感情迁移 提交于 2019-12-03 11:53:48
问题 What is the advantage of passing code directly to thread vs using CompletableFuture instead? Thread thread = new Thread(() -> {do something}); thread.start(); VS CompletableFuture<Void> cf1 = CompletableFuture.runAsync(() -> {do something}); 回答1: CompletableFuture.runAsync(...) runs the Runnable in the forkJoin-Pool which is managed , while new Thread() creates a new thread which you have to manage . What does "is managed" mean, it's pre-allocated and the threads are shared in the JVM. When

What is the difference between 'CompletionStage' and 'CompletableFuture'

匆匆过客 提交于 2019-12-03 09:53:26
I have seen an example in each of them, but I need to know exactly what is the difference in deep, Because sometimes I think I can use both of them to get the same result, So I want know so that I can choose the correct one? what is the benefit of using each of them? Like this example both are worked: public CompletionStage<Result> getNextQueryUUID() { return CompletableFuture.supplyAsync(() -> { String nextId = dbRequestService.getNextRequestQueryUUID(); return ok(nextId); }, executor); } public CompletableFuture<Result> getNextQueryUUID() { return CompletableFuture.supplyAsync(() -> { String