Class BasicHttpClientConnectionManager

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, HttpClientConnectionManager

    @Contract(threading=SAFE_CONDITIONAL)
    public class BasicHttpClientConnectionManager
    extends java.lang.Object
    implements HttpClientConnectionManager, java.io.Closeable
    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
    • Method Detail

      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • setSocketConfig

        public void setSocketConfig​(SocketConfig socketConfig)
      • setConnectionConfig

        public void setConnectionConfig​(ConnectionConfig connConfig)
      • requestConnection

        public final ConnectionRequest requestConnection​(HttpRoute route,
                                                         java.lang.Object state)
        Description copied from interface: HttpClientConnectionManager
        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.

        Specified by:
        requestConnection in interface HttpClientConnectionManager
        Parameters:
        route - HTTP route of the requested connection.
        state - expected state of the connection or null if the connection is not expected to carry any state.
      • releaseConnection

        public void releaseConnection​(HttpClientConnection conn,
                                      java.lang.Object state,
                                      long keepalive,
                                      java.util.concurrent.TimeUnit timeUnit)
        Description copied from interface: HttpClientConnectionManager
        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.
        Specified by:
        releaseConnection in interface HttpClientConnectionManager
        Parameters:
        conn - the managed connection to release.
        keepalive - the duration of time this connection is valid for reuse.
        timeUnit - the time unit.
        See Also:
        HttpClientConnectionManager.closeExpiredConnections()
      • connect

        public void connect​(HttpClientConnection conn,
                            HttpRoute route,
                            int connectTimeout,
                            HttpContext context)
                     throws java.io.IOException
        Description copied from interface: HttpClientConnectionManager
        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).
        Specified by:
        connect in interface HttpClientConnectionManager
        Parameters:
        conn - the managed connection.
        route - the route of the connection.
        connectTimeout - connect timeout in milliseconds.
        context - the actual HTTP context.
        Throws:
        java.io.IOException
      • upgrade

        public void upgrade​(HttpClientConnection conn,
                            HttpRoute route,
                            HttpContext context)
                     throws java.io.IOException
        Description copied from interface: HttpClientConnectionManager
        Upgrades the underlying connection socket to TLS/SSL (or another layering protocol) after having executed CONNECT method to all intermediate proxy hops
        Specified by:
        upgrade in interface HttpClientConnectionManager
        Parameters:
        conn - the managed connection.
        route - the route of the connection.
        context - the actual HTTP context.
        Throws:
        java.io.IOException
      • closeExpiredConnections

        public void closeExpiredConnections()
        Description copied from interface: HttpClientConnectionManager
        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.

        Specified by:
        closeExpiredConnections in interface HttpClientConnectionManager
      • closeIdleConnections

        public void closeIdleConnections​(long idletime,
                                         java.util.concurrent.TimeUnit timeUnit)
        Description copied from interface: HttpClientConnectionManager
        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.

        Specified by:
        closeIdleConnections in interface HttpClientConnectionManager
        Parameters:
        idletime - the idle time of connections to be closed
        timeUnit - the unit for the idletime
        See Also:
        HttpClientConnectionManager.closeExpiredConnections()
      • shutdown

        public void shutdown()
        Description copied from interface: HttpClientConnectionManager
        Shuts down this connection manager and releases allocated resources. This includes closing all connections, whether they are currently used or not.
        Specified by:
        shutdown in interface HttpClientConnectionManager