-
Notifications
You must be signed in to change notification settings - Fork 985
Add CompletableFuture-based executeAsync overloads to fluent Async for simpler async composition. #782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| final ThreadFactory threadFactory = r -> { | ||
| final Thread t = new Thread(r, "httpclient5-fluent-async-" + instanceId + "-" + threadCount.incrementAndGet()); | ||
| t.setDaemon(true); | ||
| return t; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use DefaultThreadFactory from core
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
| * | ||
| * @since 4.3 | ||
| */ | ||
| public class Async { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class needs a thread-safety policy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just add Contract
| private Executor executor; | ||
| private java.util.concurrent.Executor concurrentExec; | ||
| private ExecutorService ownedConcurrentExec; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These fields need to either be volatile, final, or guarded by some other memory barrier (e.g. only accessed from synchronized methods)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
| * | ||
| * @since 5.7 | ||
| */ | ||
| public <T> CompletableFuture<T> executeAsync(final Request request, final HttpClientResponseHandler<T> handler) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why we can't push this down into HttpAsyncClient? Seems like this would work as a simple default interface method to adapt the older continuation-passing API to the new promise (CompletableFuture) based API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, I'm confused. This class provides an async API for synchronous request execution?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, I'm confused. This class provides an async API for synchronous request execution?
@rschmitt Fluent is a wrapper around the classic HttpClient with simplified APIs for most common use cases. Async in Fluent is indeed just a wrapper around multi-threaded executor and synchronous request execution.
This is historic and goes back to the early days of HttpClient 4.x
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, I'm confused. This class provides an async API for synchronous request execution?
Fluent is built on the classic client. The new CompletableFuture methods are just adapters over the existing callback-based API.
…r simpler async composition. Provide an optional bounded default executor with configurable thread and queue limits. Keep existing Future-based API and default execution behavior unchanged.
31b22c2 to
d2f2ed4
Compare
This change improves the fluent Async API without altering existing behaviour or contracts. It adds executeAsync overloads returning CompletableFuture, including variants that also invoke the existing FutureCallback, making it easier to compose and aggregate asynchronous request execution using standard Java primitives. It also introduces an optional owned bounded default executor, configurable in terms of thread count and queue capacity, for applications that prefer not to manage their own ExecutorService. The existing Future-based methods and the default execution model remain unchanged.