Class AsyncHTTPBuilder
- java.lang.Object
-
- groovyx.net.http.HTTPBuilder
-
- groovyx.net.http.AsyncHTTPBuilder
-
public class AsyncHTTPBuilder extends HTTPBuilder
This implementation makes all requests asynchronous by submitting jobs to aThreadPoolExecutor
. All request methods (includingget
andpost
) return aFuture
instance, whoseget
method will provide access to whatever value was returned from the response handler closure.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class groovyx.net.http.HTTPBuilder
HTTPBuilder.RequestConfigDelegate
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_POOL_SIZE
Default pool size is one is not supplied in the constructor.protected java.util.concurrent.ExecutorService
threadPool
-
Fields inherited from class groovyx.net.http.HTTPBuilder
auth, autoAcceptHeader, contentEncodingHandler, defaultContentType, defaultRequestContentType, defaultRequestHeaders, defaultResponseHandlers, defaultURI, encoders, log, parsers
-
-
Constructor Summary
Constructors Constructor Description AsyncHTTPBuilder(java.util.Map<java.lang.String,?> args)
Accepts the following named parameters: threadPoolCustomExecutorService
instance for running submitted requests.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
defaultFailureHandler(HttpResponseDecorator resp)
For 'failure' responses (e.g.protected java.lang.Object
defaultSuccessHandler(HttpResponseDecorator resp, java.lang.Object parsedData)
This is the defaultresponse.success
handler.protected java.util.concurrent.Future<?>
doRequest(HTTPBuilder.RequestConfigDelegate delegate)
Submits aCallable
instance to the job pool, which in turn will callHTTPBuilder.doRequest(RequestConfigDelegate)
in an asynchronous thread.private java.lang.Object
doRequestSuper(HTTPBuilder.RequestConfigDelegate delegate)
protected void
finalize()
java.util.concurrent.ExecutorService
getThreadExecutor()
Access the underlying threadpool to adjust things like job timeouts.int
getTimeout()
Get the timeout in for establishing an HTTP connection.protected void
initThreadPools(int poolSize, java.util.concurrent.ExecutorService threadPool)
Initializes threading parameters for the HTTPClient'sThreadSafeClientConnManager
, and this class' ThreadPoolExecutor.void
setTimeout(int timeout)
This timeout is used for both the time to wait for an established connection, and the time to wait for data.void
shutdown()
Release any system resources held by this instance.-
Methods inherited from class groovyx.net.http.HTTPBuilder
buildDefaultResponseHandlers, createClient, doRequest, get, get, getAuth, getClient, getContentType, getEncoder, getHandler, getHeaders, getParser, getUri, ignoreSSLIssues, isAutoAcceptHeader, parseResponse, post, post, request, request, request, setAuthConfig, setAutoAcceptHeader, setClient, setContentEncoding, setContentEncodingRegistry, setContentType, setEncoderRegistry, setHeaders, setParserRegistry, setProxy, setUri
-
-
-
-
Field Detail
-
DEFAULT_POOL_SIZE
public static final int DEFAULT_POOL_SIZE
Default pool size is one is not supplied in the constructor.- See Also:
- Constant Field Values
-
threadPool
protected java.util.concurrent.ExecutorService threadPool
-
-
Constructor Detail
-
AsyncHTTPBuilder
public AsyncHTTPBuilder(java.util.Map<java.lang.String,?> args) throws java.net.URISyntaxException
Accepts the following named parameters:- threadPool
- Custom
ExecutorService
instance for running submitted requests. If this is an instance ofThreadPoolExecutor
, the poolSize will be determined byThreadPoolExecutor.getMaximumPoolSize()
. The default threadPool uses an unbounded queue to accept an unlimited number of requests. - poolSize
- Max number of concurrent requests
- uri
- Default request URI
- contentType
- Default content type for requests and responses
- timeout
- Timeout in milliseconds to wait for a connection to be established and request to complete.
- Throws:
java.net.URISyntaxException
-
-
Method Detail
-
doRequest
protected java.util.concurrent.Future<?> doRequest(HTTPBuilder.RequestConfigDelegate delegate)
Submits aCallable
instance to the job pool, which in turn will callHTTPBuilder.doRequest(RequestConfigDelegate)
in an asynchronous thread. TheFuture
instance returned by this value (which in turn should be returned by any of the publicrequest
methods (includingget
andpost
) may be used to retrieve whatever value may be returned from the executed response handler closure.- Overrides:
doRequest
in classHTTPBuilder
-
doRequestSuper
private java.lang.Object doRequestSuper(HTTPBuilder.RequestConfigDelegate delegate) throws java.io.IOException
- Throws:
java.io.IOException
-
initThreadPools
protected void initThreadPools(int poolSize, java.util.concurrent.ExecutorService threadPool)
Initializes threading parameters for the HTTPClient'sThreadSafeClientConnManager
, and this class' ThreadPoolExecutor.
-
defaultSuccessHandler
protected java.lang.Object defaultSuccessHandler(HttpResponseDecorator resp, java.lang.Object parsedData) throws ResponseParseException
This is the default
response.success
handler. It will be executed if the response is not handled by a status-code-specific handler (i.e.response.'200'= {..}
) and no generic 'success' handler is given (i.e.response.success = {..}
.) This handler simply returns the parsed data from the response body. In most cases you will probably want to define aresponse.success = {...}
handler from the request closure, which will replace the response handler defined by this method.Note for parsers that return streaming content:
For responses parsed as
BINARY
orTEXT
, the parser will return streaming content -- anInputStream
orReader
. In these cases, this handler will buffer the the response content before the network connection is closed.In practice, a user-supplied response handler closure is designed to handle streaming content so it can be read directly from the response stream without buffering, which will be much more efficient. Therefore, it is recommended that request method variants be used which explicitly accept a response handler closure in these cases.
- Overrides:
defaultSuccessHandler
in classHTTPBuilder
- Parameters:
resp
- HTTP responseparsedData
- parsed data as resolved from this instance'sParserRegistry
- Returns:
- the parsed data object (whatever the parser returns).
- Throws:
ResponseParseException
- if there is an error buffering a streaming response.
-
defaultFailureHandler
protected void defaultFailureHandler(HttpResponseDecorator resp) throws HttpResponseException
For 'failure' responses (e.g. a 404), the exception will be wrapped in aExecutionException
and held by theFuture
instance. The exception is then re-thrown when callingfuture.get()
. You can access the original exception (e.g. anHttpResponseException
) by callingex.getCause()
.- Overrides:
defaultFailureHandler
in classHTTPBuilder
- Throws:
HttpResponseException
-
setTimeout
public void setTimeout(int timeout)
This timeout is used for both the time to wait for an established connection, and the time to wait for data.- Parameters:
timeout
- time to wait in milliseconds.- See Also:
HttpConnectionParams.setSoTimeout(HttpParams, int)
,HttpConnectionParams.setConnectionTimeout(HttpParams, int)
-
getTimeout
public int getTimeout()
Get the timeout in for establishing an HTTP connection.- Returns:
- timeout in milliseconds.
-
getThreadExecutor
public java.util.concurrent.ExecutorService getThreadExecutor()
Access the underlying threadpool to adjust things like job timeouts.
Note that this is not the same pool used by the HttpClient's
ThreadSafeClientConnManager
. Therefore, increasing themaximum pool size
will not in turn increase the number of possible concurrent requests. It will simply cause more requests to be attempted which will then simply block while waiting for a free connection.- Returns:
- the service used to execute requests. By default this is a
ThreadPoolExecutor
.
-
shutdown
public void shutdown()
Release any system resources held by this instance.- Overrides:
shutdown
in classHTTPBuilder
- See Also:
ClientConnectionManager.shutdown()
-
finalize
protected void finalize() throws java.lang.Throwable
- Overrides:
finalize
in classjava.lang.Object
- Throws:
java.lang.Throwable
- See Also:
shutdown()
-
-