Skip to content

Instantly share code, notes, and snippets.

@tomfitzhenry
Last active August 17, 2021 13:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomfitzhenry/4bb032f6a9f56d95f6fb54454e617fc1 to your computer and use it in GitHub Desktop.
Save tomfitzhenry/4bb032f6a9f56d95f6fb54454e617fc1 to your computer and use it in GitHub Desktop.
CompletableHttpAsyncClient: Adaptor for HttpAsyncClient + Java 8
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.nio.client.HttpAsyncClient;
import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
import org.apache.http.protocol.HttpContext;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
public class CompletableHttpAsyncClient {
private final HttpAsyncClient httpAsyncClient;
public CompletableHttpAsyncClient(HttpAsyncClient httpAsyncClient) {
this.httpAsyncClient = httpAsyncClient;
}
public <T> CompletableFuture<T> execute(HttpAsyncRequestProducer httpAsyncRequestProducer, HttpAsyncResponseConsumer<T> httpAsyncResponseConsumer, HttpContext httpContext) {
return toCompletableFuture(fc -> httpAsyncClient.execute(httpAsyncRequestProducer, httpAsyncResponseConsumer, httpContext, fc));
}
public <T> CompletableFuture<T> execute(HttpAsyncRequestProducer httpAsyncRequestProducer, HttpAsyncResponseConsumer<T> httpAsyncResponseConsumer) {
return toCompletableFuture(fc -> httpAsyncClient.execute(httpAsyncRequestProducer, httpAsyncResponseConsumer, fc));
}
public CompletableFuture<HttpResponse> execute(HttpHost httpHost, HttpRequest httpRequest, HttpContext httpContext) {
return toCompletableFuture(fc -> httpAsyncClient.execute(httpHost, httpRequest, httpContext, fc));
}
public CompletableFuture<HttpResponse> execute(HttpHost httpHost, HttpRequest httpRequest) {
return toCompletableFuture(fc -> httpAsyncClient.execute(httpHost, httpRequest, fc));
}
public CompletableFuture<HttpResponse> execute(HttpUriRequest httpUriRequest, HttpContext httpContext) {
return toCompletableFuture(fc -> httpAsyncClient.execute(httpUriRequest, httpContext, fc));
}
public CompletableFuture<HttpResponse> execute(HttpUriRequest httpUriRequest) {
return toCompletableFuture(fc -> httpAsyncClient.execute(httpUriRequest, fc));
}
private static <T> CompletableFuture<T> toCompletableFuture(Consumer<FutureCallback<T>> c) {
CompletableFuture<T> promise = new CompletableFuture<>();
c.accept(new FutureCallback<T>() {
@Override
public void completed(T t) {
promise.complete(t);
}
@Override
public void failed(Exception e) {
promise.completeExceptionally(e);
}
@Override
public void cancelled() {
promise.cancel(true);
}
});
return promise;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment