org.apache.http.impl.client
Class FutureRequestExecutionService

java.lang.Object
  extended by org.apache.http.impl.client.FutureRequestExecutionService
All Implemented Interfaces:
Closeable

@ThreadSafe
public class FutureRequestExecutionService
extends Object
implements Closeable

HttpAsyncClientWithFuture wraps calls to execute with a 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.


Constructor Summary
FutureRequestExecutionService(HttpClient httpclient, ExecutorService executorService)
          Create a new FutureRequestExecutionService.
 
Method Summary
 void close()
           
<T> HttpRequestFutureTask<T>
execute(HttpUriRequest request, HttpContext context, ResponseHandler<T> responseHandler)
          Schedule a request for execution.
<T> HttpRequestFutureTask<T>
execute(HttpUriRequest request, HttpContext context, ResponseHandler<T> responseHandler, FutureCallback<T> callback)
          Schedule a request for execution.
<T> List<Future<T>>
executeMultiple(HttpContext context, ResponseHandler<T> responseHandler, FutureCallback<T> callback, long timeout, TimeUnit timeUnit, HttpUriRequest... requests)
          Schedule multiple requests for execution with a timeout.
<T> List<Future<T>>
executeMultiple(ResponseHandler<T> responseHandler, HttpUriRequest... requests)
          Schedule multiple requests for execution.
 FutureRequestExecutionMetrics metrics()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FutureRequestExecutionService

public FutureRequestExecutionService(HttpClient httpclient,
                                     ExecutorService executorService)
Create a new FutureRequestExecutionService.

Parameters:
httpclient - you should tune your httpclient instance to match your needs. You should align the max number of connections in the pool and the number of threads in the executor; it doesn't make sense to have more threads than connections and if you have less connections than threads, the threads will just end up blocking on getting a connection from the pool.
executorService - any executorService will do here. E.g. Executors.newFixedThreadPool(int)
Method Detail

execute

public <T> HttpRequestFutureTask<T> execute(HttpUriRequest request,
                                            HttpContext context,
                                            ResponseHandler<T> responseHandler)
Schedule a request for execution.

Type Parameters:
T -
Parameters:
request - request to execute
responseHandler - handler that will process the response.
Returns:
HttpAsyncClientFutureTask for the scheduled request.
Throws:
InterruptedException

execute

public <T> HttpRequestFutureTask<T> execute(HttpUriRequest request,
                                            HttpContext context,
                                            ResponseHandler<T> responseHandler,
                                            FutureCallback<T> callback)
Schedule a request for execution.

Type Parameters:
T -
Parameters:
request - request to execute
context - optional context; use null if not needed.
responseHandler - handler that will process the response.
callback - callback handler that will be called when the request is scheduled, started, completed, failed, or cancelled.
Returns:
HttpAsyncClientFutureTask for the scheduled request.
Throws:
InterruptedException

executeMultiple

public <T> List<Future<T>> executeMultiple(ResponseHandler<T> responseHandler,
                                           HttpUriRequest... requests)
                                throws InterruptedException
Schedule multiple requests for execution.

Type Parameters:
T -
Parameters:
responseHandler - handler that will process the responses.
requests - one or more requests.
Returns:
a list of HttpAsyncClientFutureTask for the scheduled requests.
Throws:
InterruptedException

executeMultiple

public <T> List<Future<T>> executeMultiple(HttpContext context,
                                           ResponseHandler<T> responseHandler,
                                           FutureCallback<T> callback,
                                           long timeout,
                                           TimeUnit timeUnit,
                                           HttpUriRequest... requests)
                                throws InterruptedException
Schedule multiple requests for execution with a timeout.

Type Parameters:
T -
Parameters:
context - optional context; use null if not needed.
responseHandler - handler that will process the responses.
callback - callback handler that will be called when requests are scheduled, started, completed, failed, or cancelled.
timeout -
timeUnit -
requests - one or more requests.
Returns:
a list of HttpAsyncClientFutureTask for the scheduled requests.
Throws:
InterruptedException

metrics

public FutureRequestExecutionMetrics metrics()
Returns:
metrics gathered for this instance.
See Also:
FutureRequestExecutionMetrics

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Throws:
IOException


Copyright © 1999-2013 The Apache Software Foundation. All Rights Reserved.