Renamed HttpAsyncClientWithFuture to FutureRequestExecutionService; moved implementation classes to o.a.h.impl.client package
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1463471 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
458575bf59
commit
0373fbed65
|
@ -29,23 +29,31 @@ package org.apache.http.examples.client;
|
|||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.ResponseHandler;
|
||||
import org.apache.http.client.async.HttpAsyncClientFutureTask;
|
||||
import org.apache.http.client.async.HttpAsyncClientWithFuture;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.concurrent.FutureCallback;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.client.FutureRequestExecutionService;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpRequestFutureTask;
|
||||
|
||||
public class ClientAsyncWithFuture {
|
||||
public class ClientWithRequestFuture {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// the simplest way to create a HttpAsyncClientWithFuture
|
||||
HttpAsyncClientWithFuture client = HttpClients.createAsync(3);
|
||||
HttpClient httpclient = HttpClientBuilder.create()
|
||||
.setMaxConnPerRoute(5)
|
||||
.setMaxConnTotal(5).build();
|
||||
ExecutorService execService = Executors.newFixedThreadPool(5);
|
||||
FutureRequestExecutionService requestExecService = new FutureRequestExecutionService(httpclient, execService);
|
||||
|
||||
// Because things are asynchronous, you must provide a ResponseHandler
|
||||
ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() {
|
||||
|
@ -57,14 +65,16 @@ public class ClientAsyncWithFuture {
|
|||
|
||||
// Simple request ...
|
||||
HttpGet request1 = new HttpGet("http://google.com");
|
||||
HttpAsyncClientFutureTask<Boolean> futureTask1 = client.execute(request1, handler);
|
||||
HttpRequestFutureTask<Boolean> futureTask1 = requestExecService.execute(request1,
|
||||
HttpClientContext.create(), handler);
|
||||
Boolean wasItOk1 = futureTask1.get();
|
||||
System.out.println("It was ok? " + wasItOk1);
|
||||
|
||||
// Cancel a request
|
||||
try {
|
||||
HttpGet request2 = new HttpGet("http://google.com");
|
||||
HttpAsyncClientFutureTask<Boolean> futureTask2 = client.execute(request2, handler);
|
||||
HttpRequestFutureTask<Boolean> futureTask2 = requestExecService.execute(request2,
|
||||
HttpClientContext.create(), handler);
|
||||
futureTask2.cancel(true);
|
||||
Boolean wasItOk2 = futureTask2.get();
|
||||
System.out.println("It was cancelled so it should never print this: " + wasItOk2);
|
||||
|
@ -74,7 +84,8 @@ public class ClientAsyncWithFuture {
|
|||
|
||||
// Request with a timeout
|
||||
HttpGet request3 = new HttpGet("http://google.com");
|
||||
HttpAsyncClientFutureTask<Boolean> futureTask3 = client.execute(request3, handler);
|
||||
HttpRequestFutureTask<Boolean> futureTask3 = requestExecService.execute(request3,
|
||||
HttpClientContext.create(), handler);
|
||||
Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS);
|
||||
System.out.println("It was ok? " + wasItOk3);
|
||||
|
||||
|
@ -96,7 +107,8 @@ public class ClientAsyncWithFuture {
|
|||
HttpGet request4 = 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<Boolean> futureTask4 = client.execute(request4, null, handler, callback);
|
||||
HttpRequestFutureTask<Boolean> futureTask4 = requestExecService.execute(request4,
|
||||
HttpClientContext.create(), handler, callback);
|
||||
Boolean wasItOk4 = futureTask4.get(10, TimeUnit.SECONDS);
|
||||
System.out.println("It was ok? " + wasItOk4);
|
||||
|
||||
|
@ -106,8 +118,9 @@ public class ClientAsyncWithFuture {
|
|||
HttpGet request7 = 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<Future<Boolean>> futureTask = client.executeMultiple(null, handler, callback,20,
|
||||
TimeUnit.SECONDS, request5, request6, request7);
|
||||
List<Future<Boolean>> futureTask = requestExecService.executeMultiple(
|
||||
HttpClientContext.create(), handler, callback,
|
||||
20,TimeUnit.SECONDS, request5, request6, request7);
|
||||
// you can still access the futures directly, if you want. The futures are in the same order as the requests.
|
||||
for (Future<Boolean> future : futureTask) {
|
||||
System.out.println("another result " + future.get());
|
|
@ -1,96 +0,0 @@
|
|||
/*
|
||||
* ====================================================================
|
||||
* 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
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.http.client.async;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* Collection of different counters used to gather metrics for {@link HttpAsyncClientWithFuture}.
|
||||
*/
|
||||
public class ConnectionMetrics {
|
||||
|
||||
final AtomicLong activeConnections = new AtomicLong();
|
||||
final AtomicLong scheduledConnections = new AtomicLong();
|
||||
final DurationCounter successfulConnections = new DurationCounter();
|
||||
final DurationCounter failedConnections = new DurationCounter();
|
||||
final DurationCounter requests = new DurationCounter();
|
||||
final DurationCounter tasks = new DurationCounter();
|
||||
|
||||
public ConnectionMetrics() {
|
||||
}
|
||||
|
||||
public String metricsAsJson() {
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
buf.append("{\n");
|
||||
buf.append(" \"totalConnections\":" + requests.count() + ",\n");
|
||||
buf.append(" \"failedConnections\":" + failedConnections + ",\n");
|
||||
buf.append(" \"successfulConnections\":" + successfulConnections + ",\n");
|
||||
buf.append(" \"averageRequestDuration\":" + requests.averageDuration() + ",\n");
|
||||
buf.append(" \"averageTaskDuration\":" + tasks.averageDuration() + ",\n");
|
||||
buf.append(" \"activeConnections\":" + activeConnections + ",\n");
|
||||
buf.append(" \"scheduledConnections\":" + scheduledConnections + "\n");
|
||||
buf.append("}\n");
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public long activeConnections() {
|
||||
return activeConnections.get();
|
||||
}
|
||||
|
||||
public long scheduledConnections() {
|
||||
return scheduledConnections.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return metricsAsJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* A counter that can measure duration and number of events.
|
||||
*/
|
||||
public static class DurationCounter {
|
||||
private final AtomicLong count = new AtomicLong(0);
|
||||
private final AtomicLong cumulativeDuration = new AtomicLong(0);
|
||||
|
||||
public void increment(final long startTime) {
|
||||
count.incrementAndGet();
|
||||
cumulativeDuration.addAndGet(System.currentTimeMillis() - startTime);
|
||||
}
|
||||
|
||||
public long count() {
|
||||
return count.get();
|
||||
}
|
||||
|
||||
public long averageDuration() {
|
||||
final long counter = count.get();
|
||||
return cumulativeDuration.get() / counter;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
* ====================================================================
|
||||
* 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
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.http.impl.client;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* Collection of different counters used to gather metrics for {@link FutureRequestExecutionService}.
|
||||
*/
|
||||
public final class FutureRequestExecutionMetrics {
|
||||
|
||||
private final AtomicLong activeConnections = new AtomicLong();
|
||||
private final AtomicLong scheduledConnections = new AtomicLong();
|
||||
private final DurationCounter successfulConnections = new DurationCounter();
|
||||
private final DurationCounter failedConnections = new DurationCounter();
|
||||
private final DurationCounter requests = new DurationCounter();
|
||||
private final DurationCounter tasks = new DurationCounter();
|
||||
|
||||
FutureRequestExecutionMetrics() {
|
||||
}
|
||||
|
||||
AtomicLong getActiveConnections() {
|
||||
return activeConnections;
|
||||
}
|
||||
|
||||
AtomicLong getScheduledConnections() {
|
||||
return scheduledConnections;
|
||||
}
|
||||
|
||||
DurationCounter getSuccessfulConnections() {
|
||||
return successfulConnections;
|
||||
}
|
||||
|
||||
DurationCounter getFailedConnections() {
|
||||
return failedConnections;
|
||||
}
|
||||
|
||||
DurationCounter getRequests() {
|
||||
return requests;
|
||||
}
|
||||
|
||||
DurationCounter getTasks() {
|
||||
return tasks;
|
||||
}
|
||||
|
||||
public long getActiveConnectionCount() {
|
||||
return activeConnections.get();
|
||||
}
|
||||
|
||||
public long getScheduledConnectionCount() {
|
||||
return scheduledConnections.get();
|
||||
}
|
||||
|
||||
public long getSuccessfulConnectionCount() {
|
||||
return successfulConnections.count();
|
||||
}
|
||||
|
||||
public long getSuccessfulConnectionAverageDuration() {
|
||||
return successfulConnections.averageDuration();
|
||||
}
|
||||
|
||||
public long getFailedConnectionCount() {
|
||||
return failedConnections.count();
|
||||
}
|
||||
|
||||
public long getFailedConnectionAverageDuration() {
|
||||
return failedConnections.averageDuration();
|
||||
}
|
||||
|
||||
public long getRequestCount() {
|
||||
return requests.count();
|
||||
}
|
||||
|
||||
public long getRequestAverageDuration() {
|
||||
return requests.averageDuration();
|
||||
}
|
||||
|
||||
public long getTaskCount() {
|
||||
return tasks.count();
|
||||
}
|
||||
|
||||
public long getTaskAverageDuration() {
|
||||
return tasks.averageDuration();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("[activeConnections=").append(activeConnections)
|
||||
.append(", scheduledConnections=").append(scheduledConnections)
|
||||
.append(", successfulConnections=").append(successfulConnections)
|
||||
.append(", failedConnections=").append(failedConnections)
|
||||
.append(", requests=").append(requests)
|
||||
.append(", tasks=").append(tasks)
|
||||
.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A counter that can measure duration and number of events.
|
||||
*/
|
||||
static class DurationCounter {
|
||||
|
||||
private final AtomicLong count = new AtomicLong(0);
|
||||
private final AtomicLong cumulativeDuration = new AtomicLong(0);
|
||||
|
||||
public void increment(final long startTime) {
|
||||
count.incrementAndGet();
|
||||
cumulativeDuration.addAndGet(System.currentTimeMillis() - startTime);
|
||||
}
|
||||
|
||||
public long count() {
|
||||
return count.get();
|
||||
}
|
||||
|
||||
public long averageDuration() {
|
||||
final long counter = count.get();
|
||||
return cumulativeDuration.get() / counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("[count=").append(count())
|
||||
.append(", averageDuration=").append(averageDuration())
|
||||
.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -24,7 +24,7 @@
|
|||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.http.client.async;
|
||||
package org.apache.http.impl.client;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
@ -41,27 +41,25 @@ 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.client.protocol.HttpClientContext;
|
||||
import org.apache.http.concurrent.FutureCallback;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
|
||||
/**
|
||||
* HttpAsyncClientWithFuture wraps calls to execute with a {@link HttpAsyncClientFutureTask}
|
||||
* HttpAsyncClientWithFuture wraps calls to execute with a {@link HttpRequestFutureTask}
|
||||
* and schedules them using the provided executor service. Scheduled calls may be cancelled.
|
||||
* Similar to the non-blockcing HttpAsyncClient, a callback handler api is provided.
|
||||
*/
|
||||
@ThreadSafe
|
||||
public class HttpAsyncClientWithFuture implements Closeable {
|
||||
final HttpClient httpclient;
|
||||
public class FutureRequestExecutionService implements Closeable {
|
||||
|
||||
private final HttpClient httpclient;
|
||||
private final ExecutorService executorService;
|
||||
|
||||
private final ConnectionMetrics metrics = new ConnectionMetrics();
|
||||
|
||||
private final FutureRequestExecutionMetrics metrics = new FutureRequestExecutionMetrics();
|
||||
private final AtomicBoolean closed = new AtomicBoolean(false);
|
||||
|
||||
/**
|
||||
* Create a new HttpAsyncClientWithFuture.
|
||||
* Create a new FutureRequestExecutionService.
|
||||
*
|
||||
* @param httpclient
|
||||
* you should tune your httpclient instance to match your needs. You should
|
||||
|
@ -72,7 +70,7 @@ public class HttpAsyncClientWithFuture implements Closeable {
|
|||
* @param executorService
|
||||
* any executorService will do here. E.g. {@link Executors#newFixedThreadPool(int)}
|
||||
*/
|
||||
public HttpAsyncClientWithFuture(
|
||||
public FutureRequestExecutionService(
|
||||
final HttpClient httpclient,
|
||||
final ExecutorService executorService) {
|
||||
this.httpclient = httpclient;
|
||||
|
@ -91,10 +89,11 @@ public class HttpAsyncClientWithFuture implements Closeable {
|
|||
* @return HttpAsyncClientFutureTask for the scheduled request.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public <T> HttpAsyncClientFutureTask<T> execute(
|
||||
public <T> HttpRequestFutureTask<T> execute(
|
||||
final HttpUriRequest request,
|
||||
final ResponseHandler<T> responseHandler) throws InterruptedException {
|
||||
return execute(request, null, responseHandler, null);
|
||||
final HttpContext context,
|
||||
final ResponseHandler<T> responseHandler) {
|
||||
return execute(request, context, responseHandler, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -114,18 +113,18 @@ public class HttpAsyncClientWithFuture implements Closeable {
|
|||
* @return HttpAsyncClientFutureTask for the scheduled request.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public <T> HttpAsyncClientFutureTask<T> execute(
|
||||
public <T> HttpRequestFutureTask<T> execute(
|
||||
final HttpUriRequest request,
|
||||
final HttpContext context,
|
||||
final ResponseHandler<T> responseHandler,
|
||||
final FutureCallback<T> callback) throws InterruptedException {
|
||||
final FutureCallback<T> callback) {
|
||||
if(closed.get()) {
|
||||
throw new IllegalStateException("Close has been called on this httpclient instance.");
|
||||
}
|
||||
metrics.scheduledConnections.incrementAndGet();
|
||||
final HttpAsyncClientCallable<T> callable = new HttpAsyncClientCallable<T>(
|
||||
metrics.getScheduledConnections().incrementAndGet();
|
||||
final HttpRequestTaskCallable<T> callable = new HttpRequestTaskCallable<T>(
|
||||
httpclient, request, context, responseHandler, callback, metrics);
|
||||
final HttpAsyncClientFutureTask<T> httpRequestFutureTask = new HttpAsyncClientFutureTask<T>(
|
||||
final HttpRequestFutureTask<T> httpRequestFutureTask = new HttpRequestFutureTask<T>(
|
||||
request, callable);
|
||||
executorService.execute(httpRequestFutureTask);
|
||||
|
||||
|
@ -147,7 +146,7 @@ public class HttpAsyncClientWithFuture implements Closeable {
|
|||
public <T> List<Future<T>> executeMultiple(
|
||||
final ResponseHandler<T> responseHandler,
|
||||
final HttpUriRequest... requests) throws InterruptedException {
|
||||
return executeMultiple(null, responseHandler, null, -1, null, requests);
|
||||
return executeMultiple(HttpClientContext.create(), responseHandler, null, -1, null, requests);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -175,10 +174,10 @@ public class HttpAsyncClientWithFuture implements Closeable {
|
|||
final FutureCallback<T> callback,
|
||||
final long timeout, final TimeUnit timeUnit,
|
||||
final HttpUriRequest... requests) throws InterruptedException {
|
||||
metrics.scheduledConnections.incrementAndGet();
|
||||
metrics.getScheduledConnections().incrementAndGet();
|
||||
final List<Callable<T>> callables = new ArrayList<Callable<T>>();
|
||||
for (final HttpUriRequest request : requests) {
|
||||
final HttpAsyncClientCallable<T> callable = new HttpAsyncClientCallable<T>(
|
||||
final HttpRequestTaskCallable<T> callable = new HttpRequestTaskCallable<T>(
|
||||
httpclient, request, context, responseHandler, callback, metrics);
|
||||
callables.add(callable);
|
||||
}
|
||||
|
@ -191,17 +190,17 @@ public class HttpAsyncClientWithFuture implements Closeable {
|
|||
|
||||
/**
|
||||
* @return metrics gathered for this instance.
|
||||
* @see ConnectionMetrics
|
||||
* @see FutureRequestExecutionMetrics
|
||||
*/
|
||||
public ConnectionMetrics metrics() {
|
||||
public FutureRequestExecutionMetrics metrics() {
|
||||
return metrics;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
closed.set(true);
|
||||
executorService.shutdownNow();
|
||||
if(httpclient instanceof CloseableHttpClient) {
|
||||
((CloseableHttpClient) httpclient).close();
|
||||
if (httpclient instanceof Closeable) {
|
||||
((Closeable) httpclient).close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -27,12 +27,7 @@
|
|||
|
||||
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;
|
||||
|
||||
|
@ -66,17 +61,4 @@ 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.http.client.async;
|
||||
package org.apache.http.impl.client;
|
||||
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
|
@ -36,15 +36,14 @@ import org.apache.http.client.methods.HttpUriRequest;
|
|||
*
|
||||
* @param <V>
|
||||
*/
|
||||
public class HttpAsyncClientFutureTask<V> extends FutureTask<V> {
|
||||
public class HttpRequestFutureTask<V> extends FutureTask<V> {
|
||||
|
||||
private final HttpUriRequest request;
|
||||
private final HttpRequestTaskCallable<V> callable;
|
||||
|
||||
private final HttpAsyncClientCallable<V> callable;
|
||||
|
||||
public HttpAsyncClientFutureTask(
|
||||
public HttpRequestFutureTask(
|
||||
final HttpUriRequest request,
|
||||
final HttpAsyncClientCallable<V> httpCallable) {
|
||||
final HttpRequestTaskCallable<V> httpCallable) {
|
||||
super(httpCallable);
|
||||
this.request = request;
|
||||
this.callable = httpCallable;
|
||||
|
@ -67,14 +66,14 @@ public class HttpAsyncClientFutureTask<V> extends FutureTask<V> {
|
|||
* @return the time in millis the task was scheduled.
|
||||
*/
|
||||
public long scheduledTime() {
|
||||
return callable.scheduled;
|
||||
return callable.getScheduled();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the time in millis the task was started.
|
||||
*/
|
||||
public long startedTime() {
|
||||
return callable.started;
|
||||
return callable.getStarted();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,7 +81,7 @@ public class HttpAsyncClientFutureTask<V> extends FutureTask<V> {
|
|||
*/
|
||||
public long endedTime() {
|
||||
if (isDone()) {
|
||||
return callable.ended;
|
||||
return callable.getEnded();
|
||||
} else {
|
||||
throw new IllegalStateException("Task is not done yet");
|
||||
}
|
||||
|
@ -115,4 +114,5 @@ public class HttpAsyncClientFutureTask<V> extends FutureTask<V> {
|
|||
public String toString() {
|
||||
return request.getRequestLine().getUri();
|
||||
}
|
||||
|
||||
}
|
|
@ -24,7 +24,7 @@
|
|||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.http.client.async;
|
||||
package org.apache.http.impl.client;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
@ -35,40 +35,29 @@ import org.apache.http.client.methods.HttpUriRequest;
|
|||
import org.apache.http.concurrent.FutureCallback;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
|
||||
/**
|
||||
* Implementation of Callable that is wrapped with a {@link HttpAsyncClientFutureTask} by
|
||||
* {@link HttpAsyncClientWithFuture}. The callable orchestrates the invocation of
|
||||
* {@link HttpClient#execute(HttpUriRequest, ResponseHandler, HttpContext)} and callbacks in
|
||||
* {@link HttpAsyncClientCallback}.
|
||||
*
|
||||
* @param <V>
|
||||
* type returned by the responseHandler
|
||||
*/
|
||||
final class HttpAsyncClientCallable<V> implements Callable<V> {
|
||||
class HttpRequestTaskCallable<V> implements Callable<V> {
|
||||
|
||||
private final HttpUriRequest request;
|
||||
|
||||
private final HttpClient httpclient;
|
||||
|
||||
private final AtomicBoolean cancelled = new AtomicBoolean(false);
|
||||
|
||||
final long scheduled = System.currentTimeMillis();
|
||||
long started = -1;
|
||||
long ended = -1;
|
||||
private final long scheduled = System.currentTimeMillis();
|
||||
private long started = -1;
|
||||
private long ended = -1;
|
||||
|
||||
private final HttpContext context;
|
||||
private final ResponseHandler<V> responseHandler;
|
||||
private final FutureCallback<V> callback;
|
||||
|
||||
private final ConnectionMetrics metrics;
|
||||
private final FutureRequestExecutionMetrics metrics;
|
||||
|
||||
HttpAsyncClientCallable(
|
||||
HttpRequestTaskCallable(
|
||||
final HttpClient httpClient,
|
||||
final HttpUriRequest request,
|
||||
final HttpContext context,
|
||||
final ResponseHandler<V> responseHandler,
|
||||
final FutureCallback<V> callback,
|
||||
final ConnectionMetrics metrics) {
|
||||
final FutureRequestExecutionMetrics metrics) {
|
||||
this.httpclient = httpClient;
|
||||
this.responseHandler = responseHandler;
|
||||
this.request = request;
|
||||
|
@ -77,26 +66,34 @@ final class HttpAsyncClientCallable<V> implements Callable<V> {
|
|||
this.metrics = metrics;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.util.concurrent.Callable#call()
|
||||
*/
|
||||
public long getScheduled() {
|
||||
return scheduled;
|
||||
}
|
||||
|
||||
public long getStarted() {
|
||||
return started;
|
||||
}
|
||||
|
||||
public long getEnded() {
|
||||
return ended;
|
||||
}
|
||||
|
||||
public V call() throws Exception {
|
||||
if (!cancelled.get()) {
|
||||
try {
|
||||
metrics.activeConnections.incrementAndGet();
|
||||
metrics.getActiveConnections().incrementAndGet();
|
||||
started = System.currentTimeMillis();
|
||||
try {
|
||||
metrics.scheduledConnections.decrementAndGet();
|
||||
metrics.getScheduledConnections().decrementAndGet();
|
||||
final V result = httpclient.execute(request, responseHandler, context);
|
||||
ended = System.currentTimeMillis();
|
||||
metrics.successfulConnections.increment(started);
|
||||
metrics.getSuccessfulConnections().increment(started);
|
||||
if (callback != null) {
|
||||
callback.completed(result);
|
||||
}
|
||||
return result;
|
||||
} catch (final Exception e) {
|
||||
metrics.failedConnections.increment(started);
|
||||
metrics.getFailedConnections().increment(started);
|
||||
ended = System.currentTimeMillis();
|
||||
if (callback != null) {
|
||||
callback.failed(e);
|
||||
|
@ -104,9 +101,9 @@ final class HttpAsyncClientCallable<V> implements Callable<V> {
|
|||
throw e;
|
||||
}
|
||||
} finally {
|
||||
metrics.requests.increment(started);
|
||||
metrics.tasks.increment(started);
|
||||
metrics.activeConnections.decrementAndGet();
|
||||
metrics.getRequests().increment(started);
|
||||
metrics.getTasks().increment(started);
|
||||
metrics.getActiveConnections().decrementAndGet();
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException("call has been cancelled for request " + request.getURI());
|
|
@ -24,13 +24,15 @@
|
|||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.http.client.async;
|
||||
package org.apache.http.impl.client;
|
||||
|
||||
import java.io.IOException;
|
||||
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;
|
||||
|
@ -42,10 +44,11 @@ import org.apache.http.HttpException;
|
|||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.ResponseHandler;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.concurrent.FutureCallback;
|
||||
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;
|
||||
|
@ -57,7 +60,7 @@ public class HttpClientWithFutureTest {
|
|||
|
||||
private LocalTestServer localServer;
|
||||
private String uri;
|
||||
private HttpAsyncClientWithFuture httpAsyncClientWithFuture;
|
||||
private FutureRequestExecutionService httpAsyncClientWithFuture;
|
||||
|
||||
private final AtomicBoolean blocked = new AtomicBoolean(false);
|
||||
|
||||
|
@ -82,7 +85,11 @@ public class HttpClientWithFutureTest {
|
|||
this.localServer.start();
|
||||
final InetSocketAddress address = localServer.getServiceAddress();
|
||||
uri = "http://" + address.getHostName() + ":" + address.getPort() + "/wait";
|
||||
httpAsyncClientWithFuture = HttpClients.createAsync(5);
|
||||
final HttpClient httpClient = HttpClientBuilder.create()
|
||||
.setMaxConnPerRoute(5)
|
||||
.build();
|
||||
final ExecutorService executorService = Executors.newFixedThreadPool(5);
|
||||
httpAsyncClientWithFuture = new FutureRequestExecutionService(httpClient, executorService);
|
||||
}
|
||||
|
||||
@After
|
||||
|
@ -94,15 +101,15 @@ public class HttpClientWithFutureTest {
|
|||
|
||||
@Test
|
||||
public void shouldExecuteSingleCall() throws InterruptedException, ExecutionException {
|
||||
final HttpAsyncClientFutureTask<Boolean> task = httpAsyncClientWithFuture.execute(
|
||||
new HttpGet(uri), new OkidokiHandler());
|
||||
final HttpRequestFutureTask<Boolean> task = httpAsyncClientWithFuture.execute(
|
||||
new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler());
|
||||
Assert.assertTrue("request should have returned OK", task.get().booleanValue());
|
||||
}
|
||||
|
||||
@Test(expected=CancellationException.class)
|
||||
public void shouldCancel() throws InterruptedException, ExecutionException {
|
||||
final HttpAsyncClientFutureTask<Boolean> task = httpAsyncClientWithFuture.execute(
|
||||
new HttpGet(uri), new OkidokiHandler());
|
||||
final HttpRequestFutureTask<Boolean> task = httpAsyncClientWithFuture.execute(
|
||||
new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler());
|
||||
task.cancel(true);
|
||||
task.get();
|
||||
}
|
||||
|
@ -110,8 +117,8 @@ public class HttpClientWithFutureTest {
|
|||
@Test(expected=TimeoutException.class)
|
||||
public void shouldTimeout() throws InterruptedException, ExecutionException, TimeoutException {
|
||||
blocked.set(true);
|
||||
final HttpAsyncClientFutureTask<Boolean> task = httpAsyncClientWithFuture.execute(
|
||||
new HttpGet(uri), new OkidokiHandler());
|
||||
final HttpRequestFutureTask<Boolean> task = httpAsyncClientWithFuture.execute(
|
||||
new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler());
|
||||
task.get(10, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
Loading…
Reference in New Issue