groovyx.net.http
Class AsyncHTTPBuilder

java.lang.Object
  extended by groovyx.net.http.HTTPBuilder
      extended by groovyx.net.http.AsyncHTTPBuilder

public class AsyncHTTPBuilder
extends HTTPBuilder

This implementation makes all requests asynchronous by submitting jobs to a ThreadPoolExecutor. All request methods (including get and post) return a Future instance, whose get method will provide access to whatever value was returned from the response handler closure.

Author:
Tom Nichols

Nested Class Summary
 
Nested classes/interfaces inherited from class groovyx.net.http.HTTPBuilder
HTTPBuilder.SendDelegate
 
Field Summary
static int DEFAULT_POOL_SIZE
          Default pool size is one is not supplied in the constructor.
protected  ThreadPoolExecutor threadPool
           
 
Fields inherited from class groovyx.net.http.HTTPBuilder
auth, client, contentEncodingHandler, defaultContentType, defaultRequestHeaders, defaultResponseHandlers, defaultURI, encoders, log, parsers
 
Constructor Summary
AsyncHTTPBuilder(Map<String,?> args)
          Accepts the following named parameters: poolSizeMax number of concurrent requests urlDefault request URL contentTypeDefault content type for requests and responses
 
Method Summary
protected  Future<?> doRequest(HTTPBuilder.SendDelegate delegate)
          Submits a Callable instance to the job pool, which in turn will call HTTPBuilder.doRequest(SendDelegate) in an asynchronous thread.
protected  void finalize()
          
 ThreadPoolExecutor getThreadPoolExecutor()
          Access the underlying threadpool to adjust things like job timeouts.
protected  void initThreadPools(int poolSize)
          Initializes threading parameters for the HTTPClient's ThreadSafeClientConnManager, and this class' ThreadPoolExecutor.
 void shutdown()
          Release any system resources held by this instance.
 
Methods inherited from class groovyx.net.http.HTTPBuilder
buildDefaultResponseHandlers, defaultFailureHandler, defaultSuccessHandler, doRequest, get, get, getAuth, getClient, getEncoder, getHandler, getHeaders, getParser, getURL, post, post, request, request, request, setAuthConfig, setContentEncoding, setContentEncodingRegistry, setContentType, setEncoderRegistry, setHeaders, setParserRegistry, setProxy, setURL
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 final ThreadPoolExecutor threadPool
Constructor Detail

AsyncHTTPBuilder

public AsyncHTTPBuilder(Map<String,?> args)
                 throws URISyntaxException
Accepts the following named parameters:
poolSize
Max number of concurrent requests
url
Default request URL
contentType
Default content type for requests and responses

Throws:
URISyntaxException
Method Detail

doRequest

protected Future<?> doRequest(HTTPBuilder.SendDelegate delegate)
Submits a Callable instance to the job pool, which in turn will call HTTPBuilder.doRequest(SendDelegate) in an asynchronous thread. The Future instance returned by this value (which in turn should be returned by any of the public request methods (including get and post) may be used to retrieve whatever value may be returned from the executed response handler closure.

Overrides:
doRequest in class HTTPBuilder

initThreadPools

protected void initThreadPools(int poolSize)
Initializes threading parameters for the HTTPClient's ThreadSafeClientConnManager, and this class' ThreadPoolExecutor.


getThreadPoolExecutor

public ThreadPoolExecutor getThreadPoolExecutor()

Access the underlying threadpool to adjust things like job timeouts.

Note that this is not the same thread pool used by the HttpClient's ThreadSafeClientConnManager. Therefore, increasing the maximum 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 an available connection.

Since ThreadSafeClientConnManager has no public mechanism to adjust its pool size, the value

Returns:

shutdown

public void shutdown()
Release any system resources held by this instance.

Overrides:
shutdown in class HTTPBuilder
See Also:
ClientConnectionManager.shutdown()

finalize

protected void finalize()
                 throws Throwable

Overrides:
finalize in class Object
Throws:
Throwable
See Also:
shutdown()


Copyright © 2008-2009. All Rights Reserved.