Removed #executeMultiple from FutureRequestExecutionService
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1491801 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b1c387e986
commit
656c6ecedc
|
@ -27,11 +27,9 @@
|
|||
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;
|
||||
|
@ -112,20 +110,6 @@ public class ClientWithRequestFuture {
|
|||
HttpClientContext.create(), handler, callback);
|
||||
Boolean wasItOk4 = futureTask4.get(10, TimeUnit.SECONDS);
|
||||
System.out.println("It was ok? " + wasItOk4);
|
||||
|
||||
// Multiple requests, with a callback
|
||||
HttpGet request5 = new HttpGet("http://google.com");
|
||||
HttpGet request6 = new HttpGet("http://bing.com");
|
||||
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 = 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());
|
||||
}
|
||||
} finally {
|
||||
requestExecService.close();
|
||||
}
|
||||
|
|
|
@ -28,19 +28,13 @@ package org.apache.http.impl.client;
|
|||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
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.client.protocol.HttpClientContext;
|
||||
import org.apache.http.concurrent.FutureCallback;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
|
||||
|
@ -131,63 +125,6 @@ public class FutureRequestExecutionService implements Closeable {
|
|||
return httpRequestFutureTask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule multiple requests for execution.
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param responseHandler
|
||||
* handler that will process the responses.
|
||||
* @param requests
|
||||
* one or more requests.
|
||||
* @return a list of HttpAsyncClientFutureTask for the scheduled requests.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public <T> List<Future<T>> executeMultiple(
|
||||
final ResponseHandler<T> responseHandler,
|
||||
final HttpUriRequest... requests) throws InterruptedException {
|
||||
return executeMultiple(HttpClientContext.create(), responseHandler, null, -1, null, requests);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule multiple requests for execution with a timeout.
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param context
|
||||
* optional context; use null if not needed.
|
||||
* @param responseHandler
|
||||
* handler that will process the responses.
|
||||
* @param callback
|
||||
* callback handler that will be called when requests are scheduled,
|
||||
* started, completed, failed, or cancelled.
|
||||
* @param timeout
|
||||
* @param timeUnit
|
||||
* @param requests
|
||||
* one or more requests.
|
||||
* @return a list of HttpAsyncClientFutureTask for the scheduled requests.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public <T> List<Future<T>> executeMultiple(
|
||||
final HttpContext context,
|
||||
final ResponseHandler<T> responseHandler,
|
||||
final FutureCallback<T> callback,
|
||||
final long timeout, final TimeUnit timeUnit,
|
||||
final HttpUriRequest... requests) throws InterruptedException {
|
||||
metrics.getScheduledConnections().incrementAndGet();
|
||||
final List<Callable<T>> callables = new ArrayList<Callable<T>>();
|
||||
for (final HttpUriRequest request : requests) {
|
||||
final HttpRequestTaskCallable<T> callable = new HttpRequestTaskCallable<T>(
|
||||
httpclient, request, context, responseHandler, callback, metrics);
|
||||
callables.add(callable);
|
||||
}
|
||||
if (timeout > 0) {
|
||||
return executorService.invokeAll(callables, timeout, timeUnit);
|
||||
} else {
|
||||
return executorService.invokeAll(callables);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return metrics gathered for this instance.
|
||||
* @see FutureRequestExecutionMetrics
|
||||
|
|
|
@ -28,7 +28,8 @@ package org.apache.http.impl.client;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
@ -126,29 +127,30 @@ public class TestFutureRequestExecutionService {
|
|||
|
||||
@Test
|
||||
public void shouldExecuteMultipleCalls() throws InterruptedException, ExecutionException {
|
||||
final HttpGet[] requests= new HttpGet[100];
|
||||
for(int i=0;i<100;i++) {
|
||||
requests[i]=new HttpGet(uri);
|
||||
final int reqNo = 100;
|
||||
final Queue<Future<Boolean>> tasks = new LinkedList<Future<Boolean>>();
|
||||
for(int i = 0; i < reqNo; i++) {
|
||||
final Future<Boolean> task = httpAsyncClientWithFuture.execute(
|
||||
new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler());
|
||||
tasks.add(task);
|
||||
}
|
||||
final List<Future<Boolean>> tasks = httpAsyncClientWithFuture.executeMultiple(
|
||||
new OkidokiHandler(), requests);
|
||||
for (final Future<Boolean> task : tasks) {
|
||||
Assert.assertTrue("request should have returned OK", task.get().booleanValue());
|
||||
final Boolean b = task.get();
|
||||
Assert.assertNotNull(b);
|
||||
Assert.assertTrue("request should have returned OK", b.booleanValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteMultipleCallsAndCallback() throws InterruptedException {
|
||||
final int reqNo = 100;
|
||||
final HttpGet[] requests= new HttpGet[reqNo];
|
||||
for(int i = 0; i < reqNo; i++) {
|
||||
requests[i] = new HttpGet(uri);
|
||||
}
|
||||
final CountDownLatch latch = new CountDownLatch(reqNo);
|
||||
final CountingCallback callback = new CountingCallback(latch);
|
||||
httpAsyncClientWithFuture.executeMultiple(null,
|
||||
new OkidokiHandler(), callback , 10, TimeUnit.SECONDS, requests);
|
||||
|
||||
for(int i = 0; i < reqNo; i++) {
|
||||
httpAsyncClientWithFuture.execute(
|
||||
new HttpGet(uri), HttpClientContext.create(),
|
||||
new OkidokiHandler(), callback);
|
||||
}
|
||||
latch.await(10, TimeUnit.SECONDS);
|
||||
|
||||
Assert.assertEquals(100, callback.completed.get());
|
||||
|
@ -188,7 +190,8 @@ public class TestFutureRequestExecutionService {
|
|||
|
||||
|
||||
private final class OkidokiHandler implements ResponseHandler<Boolean> {
|
||||
public Boolean handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
|
||||
public Boolean handleResponse(
|
||||
final HttpResponse response) throws ClientProtocolException, IOException {
|
||||
return response.getStatusLine().getStatusCode() == 200;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue