Top Description Fields Constructors Methods
org.apache.http.impl.conn

public Class BasicHttpClientConnectionManager

extends Object
implements HttpClientConnectionManager, Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable, org.apache.http.conn.HttpClientConnectionManager
Annotations
@Contract
threading:SAFE_CONDITIONAL
Imports
java.io.Closeable, .IOException, java.net.InetSocketAddress, java.util.Date, java.util.concurrent.TimeUnit, java.util.concurrent.atomic.AtomicBoolean, org.apache.commons.logging.Log, .LogFactory, org.apache.http.HttpClientConnection, .HttpHost, org.apache.http.annotation.Contract, .ThreadingBehavior, org.apache.http.config.ConnectionConfig, .Lookup, .Registry, .RegistryBuilder, .SocketConfig, org.apache.http.conn.ConnectionRequest, .DnsResolver, .HttpClientConnectionManager, .HttpClientConnectionOperator, .HttpConnectionFactory, .ManagedHttpClientConnection, .SchemePortResolver, org.apache.http.conn.routing.HttpRoute, org.apache.http.conn.socket.ConnectionSocketFactory, .PlainConnectionSocketFactory, org.apache.http.conn.ssl.SSLConnectionSocketFactory, org.apache.http.protocol.HttpContext, org.apache.http.util.Args, .Asserts, .LangUtils

A connection manager for a single connection. This connection manager maintains only one active connection. Even though this class is fully thread-safe it ought to be used by one execution thread only, as only one thread a time can lease the connection at a time.

This connection manager will make an effort to reuse the connection for subsequent requests with the same route. It will, however, close the existing connection and open it for the given route, if the route of the persistent connection does not match that of the connection request. If the connection has been already been allocated IllegalStateException is thrown.

This connection manager implementation should be used inside an EJB container instead of PoolingHttpClientConnectionManager.

Since
4.3

Field Summary

Modifier and TypeField and Description
private ManagedHttpClientConnection
private ConnectionConfig
private final HttpClientConnectionOperator
private final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection>
private long
private final AtomicBoolean
private boolean
private final Log
private HttpRoute
private SocketConfig
private Object
private long

Constructor Summary

AccessConstructor and Description
public
BasicHttpClientConnectionManager(final Lookup<ConnectionSocketFactory> socketFactoryRegistry, final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory, final SchemePortResolver schemePortResolver, final DnsResolver dnsResolver)

public
public
public
public

Method Summary

Modifier and TypeMethod and Description
private void
public void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.
private synchronized void
public synchronized void
public synchronized void
closeIdleConnections(final long
the idle time of connections to be closed
idletime
,
final TimeUnit
the unit for the idletime
timeUnit
)

Implements org.apache.http.conn.HttpClientConnectionManager.closeIdleConnections.

Closes idle connections in the pool.
public void
connect(final HttpClientConnection
the managed connection.
conn
,
final HttpRoute
the route of the connection.
route
,
final int
connect timeout in milliseconds.
connectTimeout
,
final HttpContext
the actual HTTP context.
context
)

Implements org.apache.http.conn.HttpClientConnectionManager.connect.

Connects the underlying connection socket to the connection target in case of a direct route or to the first proxy hop in case of a route via a proxy (or multiple proxies).
protected void
finalize()

Overrides java.lang.Object.finalize.

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
pack-priv synchronized HttpClientConnection
getConnection(final HttpRoute route, final Object state)

public synchronized ConnectionConfig
private static Registry<ConnectionSocketFactory>
pack-priv HttpRoute
public synchronized SocketConfig
pack-priv Object
public synchronized void
releaseConnection(final HttpClientConnection
the managed connection to release.
conn
,
final Object state, final long
the duration of time this connection is valid for reuse.
keepalive
,
final TimeUnit
the time unit.
timeUnit
)

Implements org.apache.http.conn.HttpClientConnectionManager.releaseConnection.

Releases the connection back to the manager making it potentially re-usable by other consumers.
public final ConnectionRequest
requestConnection(final HttpRoute
HTTP route of the requested connection.
route
,
final Object
expected state of the connection or null if the connection is not expected to carry any state.
state
)

Implements org.apache.http.conn.HttpClientConnectionManager.requestConnection.

Returns a new ConnectionRequest, from which a HttpClientConnection can be obtained or the request can be aborted.
public void
routeComplete(final HttpClientConnection
the managed connection.
conn
,
final HttpRoute
the route of the connection.
route
,
final HttpContext
the actual HTTP context.
context
)

Implements org.apache.http.conn.HttpClientConnectionManager.routeComplete.

Marks the connection as fully established with all its intermediate hops completed.
public synchronized void
public synchronized void
setSocketConfig(final SocketConfig socketConfig)

public void
shutdown()

Implements org.apache.http.conn.HttpClientConnectionManager.shutdown.

Shuts down this connection manager and releases allocated resources.
public void
upgrade(final HttpClientConnection
the managed connection.
conn
,
final HttpRoute
the route of the connection.
route
,
final HttpContext
the actual HTTP context.
context
)

Implements org.apache.http.conn.HttpClientConnectionManager.upgrade.

Upgrades the underlying connection socket to TLS/SSL (or another layering protocol) after having executed CONNECT method to all intermediate proxy hops
Inherited from java.lang.Object:
cloneequalsgetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

connback to summary
private ManagedHttpClientConnection conn
connConfigback to summary
private ConnectionConfig connConfig
connectionOperatorback to summary
private final HttpClientConnectionOperator connectionOperator
connFactoryback to summary
private final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory
expiryback to summary
private long expiry
isShutdownback to summary
private final AtomicBoolean isShutdown
leasedback to summary
private boolean leased
logback to summary
private final Log log
routeback to summary
private HttpRoute route
socketConfigback to summary
private SocketConfig socketConfig
stateback to summary
private Object state
updatedback to summary
private long updated

Constructor Detail

BasicHttpClientConnectionManagerback to summary
public BasicHttpClientConnectionManager(final Lookup<ConnectionSocketFactory> socketFactoryRegistry, final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory, final SchemePortResolver schemePortResolver, final DnsResolver dnsResolver)
BasicHttpClientConnectionManagerback to summary
public BasicHttpClientConnectionManager(final HttpClientConnectionOperator httpClientConnectionOperator, final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory)
Since
4.4
BasicHttpClientConnectionManagerback to summary
public BasicHttpClientConnectionManager(final Lookup<ConnectionSocketFactory> socketFactoryRegistry, final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory)
BasicHttpClientConnectionManagerback to summary
public BasicHttpClientConnectionManager(final Lookup<ConnectionSocketFactory> socketFactoryRegistry)
BasicHttpClientConnectionManagerback to summary
public BasicHttpClientConnectionManager()

Method Detail

checkExpiryback to summary
private void checkExpiry()
closeback to summary
public void close()

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
closeConnectionback to summary
private synchronized void closeConnection()
closeExpiredConnectionsback to summary
public synchronized void closeExpiredConnections()

Implements org.apache.http.conn.HttpClientConnectionManager.closeExpiredConnections.

Doc from org.apache.http.conn.HttpClientConnectionManager.closeExpiredConnections.

Closes all expired connections in the pool.

Open connections in the pool that have not been used for the timespan defined when the connection was released will be closed. Currently allocated connections are not subject to this method. Times will be checked with milliseconds precision.

Annotations
@Override
closeIdleConnectionsback to summary
public synchronized void closeIdleConnections(final long idletime, final TimeUnit timeUnit)

Implements org.apache.http.conn.HttpClientConnectionManager.closeIdleConnections.

Doc from org.apache.http.conn.HttpClientConnectionManager.closeIdleConnections.

Closes idle connections in the pool.

Open connections in the pool that have not been used for the timespan given by the argument will be closed. Currently allocated connections are not subject to this method. Times will be checked with milliseconds precision

All expired connections will also be closed.

Parameters
idletime:long

the idle time of connections to be closed

timeUnit:TimeUnit

the unit for the idletime

Annotations
@Override
connectback to summary
public void connect(final HttpClientConnection conn, final HttpRoute route, final int connectTimeout, final HttpContext context) throws IOException

Implements org.apache.http.conn.HttpClientConnectionManager.connect.

Doc from org.apache.http.conn.HttpClientConnectionManager.connect.

Connects the underlying connection socket to the connection target in case of a direct route or to the first proxy hop in case of a route via a proxy (or multiple proxies).

Parameters
conn:HttpClientConnection

the managed connection.

route:HttpRoute

the route of the connection.

connectTimeout:int

connect timeout in milliseconds.

context:HttpContext

the actual HTTP context.

Annotations
@Override
finalizeback to summary
protected void finalize() throws Throwable

Overrides java.lang.Object.finalize.

Doc from java.lang.Object.finalize.

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

When running in a Java virtual machine in which finalization has been disabled or removed, the garbage collector will never call finalize(). In a Java virtual machine in which finalization is enabled, the garbage collector might call finalize only after an indefinite delay.

The general contract of finalize is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Annotations
@Override
Exceptions
Throwable:
the Exception raised by this method
getConnectionback to summary
pack-priv synchronized HttpClientConnection getConnection(final HttpRoute route, final Object state)
getConnectionConfigback to summary
public synchronized ConnectionConfig getConnectionConfig()
getDefaultRegistryback to summary
private static Registry<ConnectionSocketFactory> getDefaultRegistry()
getRouteback to summary
pack-priv HttpRoute getRoute()
getSocketConfigback to summary
public synchronized SocketConfig getSocketConfig()
getStateback to summary
pack-priv Object getState()
releaseConnectionback to summary
public synchronized void releaseConnection(final HttpClientConnection conn, final Object state, final long keepalive, final TimeUnit timeUnit)

Implements org.apache.http.conn.HttpClientConnectionManager.releaseConnection.

Doc from org.apache.http.conn.HttpClientConnectionManager.releaseConnection.

Releases the connection back to the manager making it potentially re-usable by other consumers. Optionally, the maximum period of how long the manager should keep the connection alive can be defined using validDuration and timeUnit parameters.

Parameters
conn:HttpClientConnection

the managed connection to release.

keepalive:long

the duration of time this connection is valid for reuse.

timeUnit:TimeUnit

the time unit.

Annotations
@Override
requestConnectionback to summary
public final ConnectionRequest requestConnection(final HttpRoute route, final Object state)

Implements org.apache.http.conn.HttpClientConnectionManager.requestConnection.

Doc from org.apache.http.conn.HttpClientConnectionManager.requestConnection.

Returns a new ConnectionRequest, from which a HttpClientConnection can be obtained or the request can be aborted.

Please note that newly allocated connections can be returned in the closed state. The consumer of that connection is responsible for fully establishing the route the to the connection target by calling connect in order to connect directly to the target or to the first proxy hop, optionally calling upgrade method to upgrade the connection after having executed CONNECT method to all intermediate proxy hops and and finally calling routeComplete to mark the route as fully completed.

Parameters
route:HttpRoute

HTTP route of the requested connection.

state:Object

expected state of the connection or null if the connection is not expected to carry any state.

Annotations
@Override
routeCompleteback to summary
public void routeComplete(final HttpClientConnection conn, final HttpRoute route, final HttpContext context) throws IOException

Implements org.apache.http.conn.HttpClientConnectionManager.routeComplete.

Doc from org.apache.http.conn.HttpClientConnectionManager.routeComplete.

Marks the connection as fully established with all its intermediate hops completed.

Parameters
conn:HttpClientConnection

the managed connection.

route:HttpRoute

the route of the connection.

context:HttpContext

the actual HTTP context.

Annotations
@Override
setConnectionConfigback to summary
public synchronized void setConnectionConfig(final ConnectionConfig connConfig)
setSocketConfigback to summary
public synchronized void setSocketConfig(final SocketConfig socketConfig)
shutdownback to summary
public void shutdown()

Implements org.apache.http.conn.HttpClientConnectionManager.shutdown.

Doc from org.apache.http.conn.HttpClientConnectionManager.shutdown.

Shuts down this connection manager and releases allocated resources. This includes closing all connections, whether they are currently used or not.

Annotations
@Override
upgradeback to summary
public void upgrade(final HttpClientConnection conn, final HttpRoute route, final HttpContext context) throws IOException

Implements org.apache.http.conn.HttpClientConnectionManager.upgrade.

Doc from org.apache.http.conn.HttpClientConnectionManager.upgrade.

Upgrades the underlying connection socket to TLS/SSL (or another layering protocol) after having executed CONNECT method to all intermediate proxy hops

Parameters
conn:HttpClientConnection

the managed connection.

route:HttpRoute

the route of the connection.

context:HttpContext

the actual HTTP context.

Annotations
@Override