completable-future

How do I get a CompletableFuture<T> from an Async Http Client request?

我与影子孤独终老i 提交于 2019-12-03 08:04:22
On Async Http Client documentation I see how to get a Future<Response> as the result of an asynchronous HTTP Get request simply doing, for example: AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient(); Future<Response> f = asyncHttpClient .prepareGet("http://api.football-data.org/v1/soccerseasons/398") .execute(); Response r = f.get(); However, for convenience I would like to get a CompletableFuture<T> instead, for which I could apply a continuation that converts the result in something else, for instance deserializing the response content from Json into a Java object (e.g.

How to use ExecutorService to poll until a result arrives

送分小仙女□ 提交于 2019-12-03 07:48:27
I have a scenario where I have to poll a remote server checking if a task has completed. Once it has, I make a different call to retrieve the result. I originally figured I should use a SingleThreadScheduledExecutor with scheduleWithFixedDelay for polling: ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); ScheduledFuture future = executor.scheduleWithFixedDelay(() -> poll(jobId), 0, 10, TimeUnit.SECONDS); public void poll(String jobId) { boolean jobDone = remoteServer.isJobDone(jobId); if (jobDone) { retrieveJobResult(jobId); } } But since I can only provide a

Does CompletableFuture have a corresponding Local context?

泄露秘密 提交于 2019-12-03 07:47:35
In the olden days, we had ThreadLocal for programs to carry data along with the request path since all request processing was done on that thread and stuff like Logback used this with MDC.put("requestId", getNewRequestId()); Then Scala and functional programming came along and Future s came along and with them came Local.scala (at least I know the twitter Future s have this class). Future.scala knows about Local.scala and transfers the context through all the map / flatMap , etc. etc. functionality such that I can still do Local.set("requestId", getNewRequestId()); and then downstream after it

What advantage is there to using Spring @Async vs. CompleteableFuture directly?

独自空忆成欢 提交于 2019-12-03 07:32:09
What's the advantage of using Spring Async vs. Just returning the CompletableFuture on your own? Your application is managed by the container. Since it's discouraged to spawn Thread s on you own, you can let the container inject a managed Executor . @Service class MyService { @Autowired private Executor executor; public CompletableFuture<?> compute() { return CompletableFuture.supplyAsync(() -> /* compute value */, executor); } } There is no “ vs. ” between the two: these are complementary technologies: CompletableFuture provides a convenient way to chain different stages of asynchronous

What is the difference between thenApply and thenApplyAsync of Java CompletableFuture?

ぐ巨炮叔叔 提交于 2019-12-03 06:32:30
问题 Suppose I have the following code: CompletableFuture<Integer> future = CompletableFuture.supplyAsync( () -> 0); thenApply case: future.thenApply( x -> x + 1 ) .thenApply( x -> x + 1 ) .thenAccept( x -> System.out.println(x)); Here the output will be 2. Now in case of thenApplyAsync : future.thenApplyAsync( x -> x + 1 ) // first step .thenApplyAsync( x -> x + 1 ) // second step .thenAccept( x -> System.out.println(x)); // third step I read in this blog that each thenApplyAsync are executed in

Thread vs CompletableFuture

放肆的年华 提交于 2019-12-03 05:50:58
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}); 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 the runnable is completed, the thread can be reused for other runnables. This makes better usage of resources

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

孤街浪徒 提交于 2019-12-03 05:42:42
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.seedVal), this.growSeedExecutor); this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedNERun

Java 8 CompletableFuture.allOf(…) with Collection or List [duplicate]

感情迁移 提交于 2019-12-03 04:56:05
问题 This question already has answers here : List<Future> to Future<List> sequence (8 answers) Closed 3 years ago . Java 8 has a function CompletableFuture.allOf(CompletableFuture<?>...cfs) that returns a CompletableFuture that is completed when all the given futures complete. However, I almost always am not dealing with an array of CompletableFuture s, but instead have a List<CompletableFuture> . Of course, I can use the toArray() method, but this ends up being a bit of a pain to have to

How to chose an Executor for CompletableFuture::supplyAsync

无人久伴 提交于 2019-12-03 02:09:07
CompletableFuture::supplyAsync(() -> IO bound queries) How do I chose an Executor for CompletableFuture::supplyAsync to avoid polluting the ForkJoinPool.commonPool() . There are many options in Executors ( newCachedThreadPool , newWorkStealingPool , newFixedThreadPool etc) And I read about new ForkJoinPool here How do I chose the right one for my use case ? You should use public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) method. As executor you can use any from the Executors.new.. - it depends on your needs. It's better to use newFixedThreadPool()

What is the difference between thenApply and thenApplyAsync of Java CompletableFuture?

我们两清 提交于 2019-12-02 21:53:36
Suppose I have the following code: CompletableFuture<Integer> future = CompletableFuture.supplyAsync( () -> 0); thenApply case: future.thenApply( x -> x + 1 ) .thenApply( x -> x + 1 ) .thenAccept( x -> System.out.println(x)); Here the output will be 2. Now in case of thenApplyAsync : future.thenApplyAsync( x -> x + 1 ) // first step .thenApplyAsync( x -> x + 1 ) // second step .thenAccept( x -> System.out.println(x)); // third step I read in this blog that each thenApplyAsync are executed in a separate thread and 'at the same time'(that means following thenApplyAsyncs started before preceding