diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientAsyncWithFuture.java b/httpclient/src/examples/org/apache/http/examples/client/ClientAsyncWithFuture.java
new file mode 100644
index 0000000..2cc7308
--- /dev/null
+++ b/httpclient/src/examples/org/apache/http/examples/client/ClientAsyncWithFuture.java
@@ -0,0 +1,150 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * .
+ *
+ */
+package org.apache.http.client.async;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.ResponseHandler;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.concurrent.FutureCallback;
+import org.apache.http.impl.client.HttpClients;
+
+public class ClientAsyncWithFuture {
+ public static void main(String[] args) {
+ // the simplest way to create a HttpAsyncClientWithFuture
+ HttpAsyncClientWithFuture client = HttpClients.createAsync(3);
+
+ // Because things are asynchronous, you must provide a ResponseHandler
+ ResponseHandler handler = new ResponseHandler() {
+ public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
+ // simply return true if the status was OK
+ return response.getStatusLine().getStatusCode() == 200;
+ }
+ };
+
+ // Simple request ...
+ try {
+ HttpGet request = new HttpGet("http://google.com");
+ HttpAsyncClientFutureTask futureTask = client.execute(request, handler);
+ Boolean wasItOk = futureTask.get();
+ System.out.println("It was ok? " + wasItOk);
+ } catch (InterruptedException e) {
+ // Threads may be interrupted
+ e.printStackTrace();
+ } catch (ExecutionException e) {
+ // if something went wrong, there will be an exception wrapped by an ExecutionException
+ e.printStackTrace();
+ }
+
+ // Cancel a request
+ try {
+ HttpGet request = new HttpGet("http://google.com");
+ HttpAsyncClientFutureTask futureTask = client.execute(request, handler);
+ futureTask.cancel(true);
+ Boolean wasItOk = futureTask.get();
+ System.out.println("It was cancelled so it should never print this: " + wasItOk);
+ } catch (InterruptedException e) {
+ // Threads may be interrupted
+ e.printStackTrace();
+ } catch (CancellationException e) {
+ System.out.println("We cancelled it, so this is expected");
+ } catch (ExecutionException e) {
+ // if something went wrong, there will be an exception wrapped by an ExecutionException
+ e.printStackTrace();
+ }
+
+ // Request with a timeout
+ try {
+ HttpGet request = new HttpGet("http://google.com");
+ HttpAsyncClientFutureTask futureTask = client.execute(request, handler);
+ Boolean wasItOk = futureTask.get(10, TimeUnit.SECONDS);
+ System.out.println("It was ok? " + wasItOk);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ } catch (TimeoutException e) {
+ e.printStackTrace();
+ } catch (ExecutionException e) {
+ e.printStackTrace();
+ }
+
+ FutureCallback callback = new FutureCallback() {
+ public void completed(Boolean result) {
+ System.out.println("completed with " + result);
+ }
+
+ public void failed(Exception ex) {
+ System.out.println("failed with " + ex.getMessage());
+ }
+
+ public void cancelled() {
+ System.out.println("cancelled");
+ }
+ };
+
+ // Simple request with a callback
+ try {
+ HttpGet request = new HttpGet("http://google.com");
+ // using a null HttpContext here since it is optional
+ // the callback will be called when the task completes, fails, or is cancelled
+ HttpAsyncClientFutureTask futureTask = client.execute(request, null, handler, callback);
+ Boolean wasItOk = futureTask.get(10, TimeUnit.SECONDS);
+ System.out.println("It was ok? " + wasItOk);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ } catch (TimeoutException e) {
+ e.printStackTrace();
+ } catch (ExecutionException e) {
+ e.printStackTrace();
+ }
+
+ // Multiple requests, with a callback
+ try {
+ HttpGet request1 = new HttpGet("http://google.com");
+ HttpGet request2 = new HttpGet("http://bing.com");
+ HttpGet request3 = new HttpGet("http://yahoo.com");
+ // using a null HttpContext here since it is optional
+ // the callback will be called for each request as their responses come back.
+ List> futureTask = client.executeMultiple(null, handler, callback,20, TimeUnit.SECONDS, request1,request2,request3);
+ // you can still access the futures directly, if you want. The futures are in the same order as the requests.
+ for (Future future : futureTask) {
+ System.out.println("another result " + future.get());
+ }
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ } catch (ExecutionException e) {
+ e.printStackTrace();
+ }
+ }
+}
\ No newline at end of file
diff --git a/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientWithFuture.java b/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientWithFuture.java
index fa0b155..52b3fd8 100644
--- a/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientWithFuture.java
+++ b/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientWithFuture.java
@@ -26,6 +26,8 @@
*/
package org.apache.http.client.async;
+import java.io.Closeable;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
@@ -33,12 +35,14 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.concurrent.FutureCallback;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.protocol.HttpContext;
/**
@@ -47,13 +51,15 @@ import org.apache.http.protocol.HttpContext;
* Similar to the non-blockcing HttpAsyncClient, a callback handler api is provided.
*/
@ThreadSafe
-public class HttpAsyncClientWithFuture {
+public class HttpAsyncClientWithFuture implements Closeable {
final HttpClient httpclient;
private final ExecutorService executorService;
private final ConnectionMetrics metrics = new ConnectionMetrics();
+ private final AtomicBoolean closed = new AtomicBoolean(false);
+
/**
* Create a new HttpAsyncClientWithFuture.
*
@@ -113,6 +119,9 @@ public class HttpAsyncClientWithFuture {
final HttpContext context,
final ResponseHandler responseHandler,
final FutureCallback callback) throws InterruptedException {
+ if(closed.get()) {
+ throw new IllegalStateException("Close has been called on this httpclient instance.");
+ }
metrics.scheduledConnections.incrementAndGet();
final HttpAsyncClientCallable callable = new HttpAsyncClientCallable(
httpclient, request, context, responseHandler, callback, metrics);
@@ -188,4 +197,11 @@ public class HttpAsyncClientWithFuture {
return metrics;
}
+ public void close() throws IOException {
+ closed.set(true);
+ executorService.shutdownNow();
+ if(httpclient instanceof CloseableHttpClient) {
+ ((CloseableHttpClient) httpclient).close();
+ }
+ }
}
diff --git a/httpclient/src/main/java/org/apache/http/impl/client/HttpClients.java b/httpclient/src/main/java/org/apache/http/impl/client/HttpClients.java
index c51bb19..6b23f13 100644
--- a/httpclient/src/main/java/org/apache/http/impl/client/HttpClients.java
+++ b/httpclient/src/main/java/org/apache/http/impl/client/HttpClients.java
@@ -27,7 +27,12 @@
package org.apache.http.impl.client;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
import org.apache.http.annotation.Immutable;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.async.HttpAsyncClientWithFuture;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
@@ -61,4 +66,17 @@ public class HttpClients {
return new MinimalHttpClient(connManager);
}
+ /**
+ * Creates a simple HttpAsyncClientWithFuture with an executor with the specified number of threads and a matching httpclient.
+ * @param threads
+ * the number of connections and threads used for the httpclient and the executor used by @see
+ * HttpAsyncClientWithFuture.
+ * @return a HttpAsyncClientWithFuture with an httpclient and executor that can handle the specified amount of
+ * threads/connections.
+ */
+ public static HttpAsyncClientWithFuture createAsync(int threads) {
+ HttpClient httpClient = HttpClientBuilder.create().setMaxConnPerRoute(5).setMaxConnTotal(5).build();
+ ExecutorService executorService = Executors.newFixedThreadPool(5);
+ return new HttpAsyncClientWithFuture(httpClient, executorService);
+ }
}
diff --git a/httpclient/src/test/java/org/apache/http/client/async/HttpClientWithFutureTest.java b/httpclient/src/test/java/org/apache/http/client/async/HttpClientWithFutureTest.java
index e3a411e..ea0f102 100644
--- a/httpclient/src/test/java/org/apache/http/client/async/HttpClientWithFutureTest.java
+++ b/httpclient/src/test/java/org/apache/http/client/async/HttpClientWithFutureTest.java
@@ -31,8 +31,6 @@ import java.net.InetSocketAddress;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -47,8 +45,7 @@ import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
import org.apache.http.localserver.LocalTestServer;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
@@ -61,8 +58,6 @@ public class HttpClientWithFutureTest {
private LocalTestServer localServer;
private String uri;
private HttpAsyncClientWithFuture httpAsyncClientWithFuture;
- private CloseableHttpClient httpClient;
- private ExecutorService executorService;
private final AtomicBoolean blocked = new AtomicBoolean(false);
@@ -87,18 +82,14 @@ public class HttpClientWithFutureTest {
this.localServer.start();
final InetSocketAddress address = localServer.getServiceAddress();
uri = "http://" + address.getHostName() + ":" + address.getPort() + "/wait";
-
- httpClient = HttpClientBuilder.create().setMaxConnPerRoute(5).setMaxConnTotal(5).build();
- executorService = Executors.newFixedThreadPool(5);
- httpAsyncClientWithFuture = new HttpAsyncClientWithFuture(httpClient, executorService);
+ httpAsyncClientWithFuture = HttpClients.createAsync(5);
}
@After
public void after() throws Exception {
blocked.set(false); // any remaining requests should unblock
this.localServer.stop();
- httpClient.close();
- executorService.shutdownNow();
+ httpAsyncClientWithFuture.close();
}
@Test