Class PoolingNHttpClientConnectionManager

  • All Implemented Interfaces:
    NHttpClientConnectionManager, org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>

    @Contract(threading=SAFE)
    public class PoolingNHttpClientConnectionManager
    extends Object
    implements NHttpClientConnectionManager, org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
    PoolingNHttpClientConnectionManager maintains a pool of NHttpClientConnections and is able to service connection requests from multiple execution threads. Connections are pooled on a per route basis. A request for a route which already the manager has persistent connections for available in the pool will be services by leasing a connection from the pool rather than creating a brand new connection.

    PoolingNHttpClientConnectionManager maintains a maximum limit of connection on a per route basis and in total. Per default this implementation will create no more than than 2 concurrent connections per given route and no more 20 connections in total. For many real-world applications these limits may prove too constraining, especially if they use HTTP as a transport protocol for their services. Connection limits, however, can be adjusted using ConnPoolControl methods.

    Since:
    4.0
    • Constructor Detail

      • PoolingNHttpClientConnectionManager

        public PoolingNHttpClientConnectionManager​(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor)
      • PoolingNHttpClientConnectionManager

        public PoolingNHttpClientConnectionManager​(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor,
                                                   org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry)
      • PoolingNHttpClientConnectionManager

        public PoolingNHttpClientConnectionManager​(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor,
                                                   NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory,
                                                   org.apache.http.conn.DnsResolver dnsResolver)
      • PoolingNHttpClientConnectionManager

        public PoolingNHttpClientConnectionManager​(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor,
                                                   NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory,
                                                   org.apache.http.nio.pool.SocketAddressResolver<org.apache.http.conn.routing.HttpRoute> socketAddressResolver)
      • PoolingNHttpClientConnectionManager

        public PoolingNHttpClientConnectionManager​(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor,
                                                   NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory,
                                                   org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry,
                                                   org.apache.http.nio.pool.SocketAddressResolver<org.apache.http.conn.routing.HttpRoute> socketAddressResolver)
      • PoolingNHttpClientConnectionManager

        public PoolingNHttpClientConnectionManager​(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor,
                                                   NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory,
                                                   org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry,
                                                   org.apache.http.conn.SchemePortResolver schemePortResolver,
                                                   org.apache.http.conn.DnsResolver dnsResolver,
                                                   long timeToLive,
                                                   TimeUnit tunit)
      • PoolingNHttpClientConnectionManager

        public PoolingNHttpClientConnectionManager​(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor,
                                                   NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory,
                                                   org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry,
                                                   org.apache.http.nio.pool.SocketAddressResolver<org.apache.http.conn.routing.HttpRoute> socketAddressResolver,
                                                   long timeToLive,
                                                   TimeUnit tunit)
    • Method Detail

      • requestConnection

        public Future<org.apache.http.nio.NHttpClientConnection> requestConnection​(org.apache.http.conn.routing.HttpRoute route,
                                                                                   Object state,
                                                                                   long connectTimeout,
                                                                                   long leaseTimeout,
                                                                                   TimeUnit tunit,
                                                                                   org.apache.http.concurrent.FutureCallback<org.apache.http.nio.NHttpClientConnection> callback)
        Description copied from interface: NHttpClientConnectionManager
        Returns a Future for a NHttpClientConnection.

        Please note that the consumer of that connection is responsible for fully establishing the route the to the connection target by calling startRoute in order to start the process of connection initialization, 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 NHttpClientConnectionManager
        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.
        connectTimeout - connect timeout.
        leaseTimeout - connection request timeout.
        tunit - time unit of the previous two timeout values.
        callback - future callback.
      • releaseConnection

        public void releaseConnection​(org.apache.http.nio.NHttpClientConnection managedConn,
                                      Object state,
                                      long keepalive,
                                      TimeUnit tunit)
        Description copied from interface: NHttpClientConnectionManager
        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 NHttpClientConnectionManager
        Parameters:
        managedConn - the managed connection to release.
        keepalive - the duration of time this connection is valid for reuse.
        tunit - the time unit.
        See Also:
        NHttpClientConnectionManager.closeExpiredConnections()
      • startRoute

        public void startRoute​(org.apache.http.nio.NHttpClientConnection managedConn,
                               org.apache.http.conn.routing.HttpRoute route,
                               org.apache.http.protocol.HttpContext context)
                        throws IOException
        Description copied from interface: NHttpClientConnectionManager
        Starts the process of connection initialization. Connection route may consist of several intermediate hops and may require a protocol upgrade. Once the route is fully established the routeComplete method must be called.
        Specified by:
        startRoute in interface NHttpClientConnectionManager
        Parameters:
        managedConn - the managed connection to initialize.
        route - the connection route.
        context - the context
        Throws:
        IOException
      • upgrade

        public void upgrade​(org.apache.http.nio.NHttpClientConnection managedConn,
                            org.apache.http.conn.routing.HttpRoute route,
                            org.apache.http.protocol.HttpContext context)
                     throws IOException
        Description copied from interface: NHttpClientConnectionManager
        Upgrades the underlying connection I/O session to TLS/SSL (or another layering protocol) after having executed CONNECT method to all intermediate proxy hops.
        Specified by:
        upgrade in interface NHttpClientConnectionManager
        Parameters:
        managedConn - the managed connection to upgrade.
        route - the connection route.
        context - the context
        Throws:
        IOException
      • routeComplete

        public void routeComplete​(org.apache.http.nio.NHttpClientConnection managedConn,
                                  org.apache.http.conn.routing.HttpRoute route,
                                  org.apache.http.protocol.HttpContext context)
        Description copied from interface: NHttpClientConnectionManager
        Marks the connection as fully established with all its intermediate hops completed.
        Specified by:
        routeComplete in interface NHttpClientConnectionManager
        Parameters:
        managedConn - the managed connection to mark as route complete.
        route - the connection route.
        context - the context
      • isRouteComplete

        public boolean isRouteComplete​(org.apache.http.nio.NHttpClientConnection managedConn)
        Description copied from interface: NHttpClientConnectionManager
        Determines if the given connection has been fully established and marked as route complete.
        Specified by:
        isRouteComplete in interface NHttpClientConnectionManager
        Parameters:
        managedConn - the managed connection.
      • closeIdleConnections

        public void closeIdleConnections​(long idleTimeout,
                                         TimeUnit tunit)
        Description copied from interface: NHttpClientConnectionManager
        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 NHttpClientConnectionManager
        Parameters:
        idleTimeout - the idle time of connections to be closed
        tunit - the unit for the idletime
        See Also:
        NHttpClientConnectionManager.closeExpiredConnections()
      • closeExpiredConnections

        public void closeExpiredConnections()
        Description copied from interface: NHttpClientConnectionManager
        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 NHttpClientConnectionManager
      • validatePendingRequests

        public void validatePendingRequests()
      • getMaxTotal

        public int getMaxTotal()
        Specified by:
        getMaxTotal in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
      • setMaxTotal

        public void setMaxTotal​(int max)
        Specified by:
        setMaxTotal in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
      • getDefaultMaxPerRoute

        public int getDefaultMaxPerRoute()
        Specified by:
        getDefaultMaxPerRoute in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
      • setDefaultMaxPerRoute

        public void setDefaultMaxPerRoute​(int max)
        Specified by:
        setDefaultMaxPerRoute in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
      • getMaxPerRoute

        public int getMaxPerRoute​(org.apache.http.conn.routing.HttpRoute route)
        Specified by:
        getMaxPerRoute in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
      • setMaxPerRoute

        public void setMaxPerRoute​(org.apache.http.conn.routing.HttpRoute route,
                                   int max)
        Specified by:
        setMaxPerRoute in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
      • getTotalStats

        public org.apache.http.pool.PoolStats getTotalStats()
        Specified by:
        getTotalStats in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
      • getStats

        public org.apache.http.pool.PoolStats getStats​(org.apache.http.conn.routing.HttpRoute route)
        Specified by:
        getStats in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
      • getRoutes

        public Set<org.apache.http.conn.routing.HttpRoute> getRoutes()
        Since:
        4.1
      • getDefaultConnectionConfig

        public org.apache.http.config.ConnectionConfig getDefaultConnectionConfig()
      • setDefaultConnectionConfig

        public void setDefaultConnectionConfig​(org.apache.http.config.ConnectionConfig defaultConnectionConfig)
      • getConnectionConfig

        public org.apache.http.config.ConnectionConfig getConnectionConfig​(org.apache.http.HttpHost host)
      • setConnectionConfig

        public void setConnectionConfig​(org.apache.http.HttpHost host,
                                        org.apache.http.config.ConnectionConfig connectionConfig)