org.apache.commons.httpclient

Class MultiThreadedHttpConnectionManager

Implemented Interfaces:
HttpConnectionManager

public class MultiThreadedHttpConnectionManager
extends Object
implements HttpConnectionManager

Manages a set of HttpConnections for various HostConfigurations.
Since:
2.0

Field Summary

static int
DEFAULT_MAX_HOST_CONNECTIONS
The default maximum number of connections allowed per host
static int
DEFAULT_MAX_TOTAL_CONNECTIONS
The default maximum number of connections allowed overall

Constructor Summary

MultiThreadedHttpConnectionManager()
No-args constructor

Method Summary

void
closeIdleConnections(long idleTimeout)
void
deleteClosedConnections()
Deletes all closed connections.
HttpConnection
getConnection(HostConfiguration hostConfiguration)
HttpConnection
getConnection(HostConfiguration hostConfiguration, long timeout)
Deprecated. Use #getConnectionWithTimeout(HostConfiguration, long)
HttpConnection
getConnectionWithTimeout(HostConfiguration hostConfiguration, long timeout)
Gets a connection or waits if one is not available.
int
getConnectionsInPool()
Gets the total number of pooled connections.
int
getConnectionsInPool(HostConfiguration hostConfiguration)
Gets the total number of pooled connections for the given host configuration.
int
getConnectionsInUse()
Deprecated. Use getConnectionsInPool()
int
getConnectionsInUse(HostConfiguration hostConfiguration)
Deprecated. Use getConnectionsInPool(HostConfiguration)
int
getMaxConnectionsPerHost()
Deprecated. Use HttpConnectionManagerParams.getDefaultMaxConnectionsPerHost(), HttpConnectionManager.getParams().
int
getMaxTotalConnections()
Deprecated. Use HttpConnectionManagerParams.getMaxTotalConnections(), HttpConnectionManager.getParams().
HttpConnectionManagerParams
getParams()
Returns parameters associated with this connection manager.
boolean
isConnectionStaleCheckingEnabled()
Deprecated. Use HttpConnectionManagerParams.isStaleCheckingEnabled(), HttpConnectionManager.getParams().
void
releaseConnection(HttpConnection conn)
Make the given HttpConnection available for use by other requests.
void
setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled)
Deprecated. Use HttpConnectionManagerParams.setStaleCheckingEnabled(boolean), HttpConnectionManager.getParams().
void
setMaxConnectionsPerHost(int maxHostConnections)
Deprecated. Use HttpConnectionManagerParams.setDefaultMaxConnectionsPerHost(int), HttpConnectionManager.getParams().
void
setMaxTotalConnections(int maxTotalConnections)
Deprecated. Use HttpConnectionManagerParams.setMaxTotalConnections(int), HttpConnectionManager.getParams().
void
setParams(HttpConnectionManagerParams params)
Assigns parameters for this connection manager.
void
shutdown()
Shuts down the connection manager and releases all resources.
static void
shutdownAll()
Shuts down and cleans up resources used by all instances of MultiThreadedHttpConnectionManager.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

DEFAULT_MAX_HOST_CONNECTIONS

public static final int DEFAULT_MAX_HOST_CONNECTIONS
The default maximum number of connections allowed per host
Field Value:
2
Usages and Demos :

View More Examples of MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS
   1:         if (m == null) {
   2:             return MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS;
   3:         } else {
   4:         ...
   5:                         max == null 
   6:                         ? MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS 
   7:                         : max.intValue()
   8:                     );
   9:             }

View Full Code Here

DEFAULT_MAX_TOTAL_CONNECTIONS

public static final int DEFAULT_MAX_TOTAL_CONNECTIONS
The default maximum number of connections allowed overall
Field Value:
20
Usages and Demos :

View More Examples of MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS
   1:             HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
   2:             MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS);
   3:     }
   4: 
   5: }

View Full Code Here

Constructor Details

MultiThreadedHttpConnectionManager

public MultiThreadedHttpConnectionManager()
No-args constructor
Usages and Demos :

View More Examples of MultiThreadedHttpConnectionManager()
   1:         
   2:         connectionManager = new MultiThreadedHttpConnectionManager();
   3:     }
   4:     
   5:     public static HttpClient createHttpClient()

View Full Code Here
   1:         HttpConnectionManager connectionManager = 
   2:                     new MultiThreadedHttpConnectionManager();
   3:                     
   4:         HttpConnectionManagerParams params = connectionManager.getParams();
   5:         params.setIntParameter( HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 2 );

View Full Code Here
   1:         {
   2:             client = new HttpClient(new MultiThreadedHttpConnectionManager());
   3:             final HostConfiguration config = new HostConfiguration();
   4:             config.setHost(hostname, port, scheme);

View Full Code Here
   1:         
   2:         HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
   3:         httpClient.getHostConfiguration().setHost("jakarta.apache.org", 80, "http");
   4:         
   5:         String[] urisToGet = {

View Full Code Here
   1:     public CommonsHttpMessageSender() {
   2:         httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
   3:     }
   4: 
   5:     public CommonsHttpMessageSender(HttpClient httpClient) {

View Full Code Here

Method Details

closeIdleConnections

public void closeIdleConnections(long idleTimeout)
Specified by:
closeIdleConnections in interface HttpConnectionManager
Since:
3.0

deleteClosedConnections

public void deleteClosedConnections()
Deletes all closed connections. Only connections currently owned by the connection manager are processed.
Since:
3.0

getConnection

public HttpConnection getConnection(HostConfiguration hostConfiguration)
Specified by:
getConnection in interface HttpConnectionManager

getConnection

public HttpConnection getConnection(HostConfiguration hostConfiguration,
                                    long timeout)
            throws HttpException

Deprecated. Use #getConnectionWithTimeout(HostConfiguration, long)

Specified by:
getConnection in interface HttpConnectionManager

getConnectionWithTimeout

public HttpConnection getConnectionWithTimeout(HostConfiguration hostConfiguration,
                                               long timeout)
            throws ConnectionPoolTimeoutException
Gets a connection or waits if one is not available. A connection is available if one exists that is not being used or if fewer than maxHostConnections have been created in the connectionPool, and fewer than maxTotalConnections have been created in all connectionPools.
Specified by:
getConnectionWithTimeout in interface HttpConnectionManager
Parameters:
hostConfiguration - The host configuration specifying the connection details.
timeout - the number of milliseconds to wait for a connection, 0 to wait indefinitely
Returns:
HttpConnection an available connection
Since:
3.0

getConnectionsInPool

public int getConnectionsInPool()
Gets the total number of pooled connections. This is the total number of connections that have been created and are still in use by this connection manager. This value will not exceed the maximum number of connections.
Returns:
the total number of pooled connections

getConnectionsInPool

public int getConnectionsInPool(HostConfiguration hostConfiguration)
Gets the total number of pooled connections for the given host configuration. This is the total number of connections that have been created and are still in use by this connection manager for the host configuration. This value will not exceed the maximum number of connections per host.
Parameters:
hostConfiguration - The host configuration
Returns:
The total number of pooled connections

getConnectionsInUse

public int getConnectionsInUse()

Deprecated. Use getConnectionsInPool()

Gets the total number of connections in use.
Returns:
the total number of connections in use

getConnectionsInUse

public int getConnectionsInUse(HostConfiguration hostConfiguration)

Deprecated. Use getConnectionsInPool(HostConfiguration)

Gets the number of connections in use for this configuration.
Parameters:
hostConfiguration - the key that connections are tracked on
Returns:
the number of connections in use

getMaxConnectionsPerHost

public int getMaxConnectionsPerHost()

Deprecated. Use HttpConnectionManagerParams.getDefaultMaxConnectionsPerHost(), HttpConnectionManager.getParams().

Gets the maximum number of connections allowed for a given hostConfiguration.
Returns:
The maximum number of connections allowed for a given hostConfiguration.

getMaxTotalConnections

public int getMaxTotalConnections()

Deprecated. Use HttpConnectionManagerParams.getMaxTotalConnections(), HttpConnectionManager.getParams().

Gets the maximum number of connections allowed for this connection manager.
Returns:
The maximum number of connections allowed

getParams

public HttpConnectionManagerParams getParams()
Returns parameters associated with this connection manager.
Specified by:
getParams in interface HttpConnectionManager
Since:
3.0

isConnectionStaleCheckingEnabled

public boolean isConnectionStaleCheckingEnabled()

Deprecated. Use HttpConnectionManagerParams.isStaleCheckingEnabled(), HttpConnectionManager.getParams().

Gets the staleCheckingEnabled value to be set on HttpConnections that are created.
Returns:
true if stale checking will be enabled on HttpConnections

releaseConnection

public void releaseConnection(HttpConnection conn)
Make the given HttpConnection available for use by other requests. If another thread is blocked in getConnection() that could use this connection, it will be woken up.
Specified by:
releaseConnection in interface HttpConnectionManager
Parameters:
conn - the HttpConnection to make available.

setConnectionStaleCheckingEnabled

public void setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled)

Deprecated. Use HttpConnectionManagerParams.setStaleCheckingEnabled(boolean), HttpConnectionManager.getParams().

Sets the staleCheckingEnabled value to be set on HttpConnections that are created.
Parameters:
connectionStaleCheckingEnabled - true if stale checking will be enabled on HttpConnections

setMaxConnectionsPerHost

public void setMaxConnectionsPerHost(int maxHostConnections)

Deprecated. Use HttpConnectionManagerParams.setDefaultMaxConnectionsPerHost(int), HttpConnectionManager.getParams().

Sets the maximum number of connections allowed for a given HostConfiguration. Per RFC 2616 section 8.1.4, this value defaults to 2.
Parameters:
maxHostConnections - the number of connections allowed for each hostConfiguration
Usages and Demos :

View More Examples of setMaxConnectionsPerHost(int maxHostConnections)
   1: import org.apache.commons.httpclient.HostConfiguration;
   2: import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
   3: 
   4:         ...
   5: 
   6:         MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
   7:         ...
   8:         httpConnectionManager.setMaxConnectionsPerHost(maxHttpConnections);
   9:         httpConnectionManager.setMaxTotalConnections(maxHttpConnections);

View Full Code Here

setMaxTotalConnections

public void setMaxTotalConnections(int maxTotalConnections)

Deprecated. Use HttpConnectionManagerParams.setMaxTotalConnections(int), HttpConnectionManager.getParams().

Sets the maximum number of connections allowed for this connection manager.
Parameters:
maxTotalConnections - the maximum number of connections allowed
Usages and Demos :

View More Examples of setMaxTotalConnections(int maxTotalConnections)
   1: import org.apache.commons.httpclient.HostConfiguration;
   2: import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
   3: 
   4:         ...
   5: 
   6:         MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
   7:         httpConnectionManager.setMaxConnectionsPerHost(maxHttpConnections);
   8:         ...
   9:         httpConnectionManager.setMaxTotalConnections(maxHttpConnections);
  10:         httpClient = new HttpClient(httpConnectionManager);

View Full Code Here

setParams

public void setParams(HttpConnectionManagerParams params)
Assigns parameters for this connection manager.
Specified by:
setParams in interface HttpConnectionManager
Since:
3.0

shutdown

public void shutdown()
Shuts down the connection manager and releases all resources. All connections associated with this class will be closed and released.

The connection manager can no longer be used once shut down.

Calling this method more than once will have no effect.

Usages and Demos :

View More Examples of shutdown()
   1: import org.apache.commons.httpclient.UsernamePasswordCredentials;
   2: import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
   3: import org.apache.commons.httpclient.methods.GetMethod;
   4:         ...
   5:             if (currentPassword != null) {
   6:                 MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
   7:                 try {
   8:         ...
   9:                 } finally {
  10:                     connectionManager.shutdown();
  11:                 }

View Full Code Here

shutdownAll

public static void shutdownAll()
Shuts down and cleans up resources used by all instances of MultiThreadedHttpConnectionManager. All static resources are released, all threads are stopped, and shutdown() is called on all live instances of MultiThreadedHttpConnectionManager.
See Also:
shutdown()