michael@0: /*
michael@0: * ====================================================================
michael@0: *
michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more
michael@0: * contributor license agreements. See the NOTICE file distributed with
michael@0: * this work for additional information regarding copyright ownership.
michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0
michael@0: * (the "License"); you may not use this file except in compliance with
michael@0: * the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing, software
michael@0: * distributed under the License is distributed on an "AS IS" BASIS,
michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0: * See the License for the specific language governing permissions and
michael@0: * limitations under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: *
michael@0: * ThreadSafeClientConnManager maintains a maximum limit of connection on michael@0: * a per route basis and in total. Per default this implementation will michael@0: * create no more than than 2 concurrent connections per given route michael@0: * and no more 20 connections in total. For many real-world applications michael@0: * these limits may prove too constraining, especially if they use HTTP michael@0: * as a transport protocol for their services. Connection limits, however, michael@0: * can be adjusted using HTTP parameters. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @ThreadSafe michael@0: public class ThreadSafeClientConnManager implements ClientConnectionManager { michael@0: michael@0: public HttpClientAndroidLog log; michael@0: michael@0: /** The schemes supported by this connection manager. */ michael@0: protected final SchemeRegistry schemeRegistry; // @ThreadSafe michael@0: michael@0: @Deprecated michael@0: protected final AbstractConnPool connectionPool; michael@0: michael@0: /** The pool of connections being managed. */ michael@0: protected final ConnPoolByRoute pool; michael@0: michael@0: /** The operator for opening and updating connections. */ michael@0: protected final ClientConnectionOperator connOperator; // DefaultClientConnectionOperator is @ThreadSafe michael@0: michael@0: protected final ConnPerRouteBean connPerRoute; michael@0: michael@0: /** michael@0: * Creates a new thread safe connection manager. michael@0: * michael@0: * @param schreg the scheme registry. michael@0: */ michael@0: public ThreadSafeClientConnManager(final SchemeRegistry schreg) { michael@0: this(schreg, -1, TimeUnit.MILLISECONDS); michael@0: } michael@0: michael@0: /** michael@0: * @since 4.1 michael@0: */ michael@0: public ThreadSafeClientConnManager() { michael@0: this(SchemeRegistryFactory.createDefault()); michael@0: } michael@0: michael@0: /** michael@0: * Creates a new thread safe connection manager. michael@0: * michael@0: * @param schreg the scheme registry. michael@0: * @param connTTL max connection lifetime, <=0 implies "infinity" michael@0: * @param connTTLTimeUnit TimeUnit of connTTL michael@0: * michael@0: * @since 4.1 michael@0: */ michael@0: public ThreadSafeClientConnManager(final SchemeRegistry schreg, michael@0: long connTTL, TimeUnit connTTLTimeUnit) { michael@0: super(); michael@0: if (schreg == null) { michael@0: throw new IllegalArgumentException("Scheme registry may not be null"); michael@0: } michael@0: this.log = new HttpClientAndroidLog(getClass()); michael@0: this.schemeRegistry = schreg; michael@0: this.connPerRoute = new ConnPerRouteBean(); michael@0: this.connOperator = createConnectionOperator(schreg); michael@0: this.pool = createConnectionPool(connTTL, connTTLTimeUnit) ; michael@0: this.connectionPool = this.pool; michael@0: } michael@0: michael@0: /** michael@0: * Creates a new thread safe connection manager. michael@0: * michael@0: * @param params the parameters for this manager. michael@0: * @param schreg the scheme registry. michael@0: * michael@0: * @deprecated use {@link ThreadSafeClientConnManager#ThreadSafeClientConnManager(SchemeRegistry)} michael@0: */ michael@0: @Deprecated michael@0: public ThreadSafeClientConnManager(HttpParams params, michael@0: SchemeRegistry schreg) { michael@0: if (schreg == null) { michael@0: throw new IllegalArgumentException("Scheme registry may not be null"); michael@0: } michael@0: this.log = new HttpClientAndroidLog(getClass()); michael@0: this.schemeRegistry = schreg; michael@0: this.connPerRoute = new ConnPerRouteBean(); michael@0: this.connOperator = createConnectionOperator(schreg); michael@0: this.pool = (ConnPoolByRoute) createConnectionPool(params) ; michael@0: this.connectionPool = this.pool; michael@0: } michael@0: michael@0: @Override michael@0: protected void finalize() throws Throwable { michael@0: try { michael@0: shutdown(); michael@0: } finally { michael@0: super.finalize(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Hook for creating the connection pool. michael@0: * michael@0: * @return the connection pool to use michael@0: * michael@0: * @deprecated use #createConnectionPool(long, TimeUnit)) michael@0: */ michael@0: @Deprecated michael@0: protected AbstractConnPool createConnectionPool(final HttpParams params) { michael@0: return new ConnPoolByRoute(connOperator, params); michael@0: } michael@0: michael@0: /** michael@0: * Hook for creating the connection pool. michael@0: * michael@0: * @return the connection pool to use michael@0: * michael@0: * @since 4.1 michael@0: */ michael@0: protected ConnPoolByRoute createConnectionPool(long connTTL, TimeUnit connTTLTimeUnit) { michael@0: return new ConnPoolByRoute(connOperator, connPerRoute, 20, connTTL, connTTLTimeUnit); michael@0: } michael@0: michael@0: /** michael@0: * Hook for creating the connection operator. michael@0: * It is called by the constructor. michael@0: * Derived classes can override this method to change the michael@0: * instantiation of the operator. michael@0: * The default implementation here instantiates michael@0: * {@link DefaultClientConnectionOperator DefaultClientConnectionOperator}. michael@0: * michael@0: * @param schreg the scheme registry. michael@0: * michael@0: * @return the connection operator to use michael@0: */ michael@0: protected ClientConnectionOperator michael@0: createConnectionOperator(SchemeRegistry schreg) { michael@0: michael@0: return new DefaultClientConnectionOperator(schreg);// @ThreadSafe michael@0: } michael@0: michael@0: public SchemeRegistry getSchemeRegistry() { michael@0: return this.schemeRegistry; michael@0: } michael@0: michael@0: public ClientConnectionRequest requestConnection( michael@0: final HttpRoute route, michael@0: final Object state) { michael@0: michael@0: final PoolEntryRequest poolRequest = pool.requestPoolEntry( michael@0: route, state); michael@0: michael@0: return new ClientConnectionRequest() { michael@0: michael@0: public void abortRequest() { michael@0: poolRequest.abortRequest(); michael@0: } michael@0: michael@0: public ManagedClientConnection getConnection( michael@0: long timeout, TimeUnit tunit) throws InterruptedException, michael@0: ConnectionPoolTimeoutException { michael@0: if (route == null) { michael@0: throw new IllegalArgumentException("Route may not be null."); michael@0: } michael@0: michael@0: if (log.isDebugEnabled()) { michael@0: log.debug("Get connection: " + route + ", timeout = " + timeout); michael@0: } michael@0: michael@0: BasicPoolEntry entry = poolRequest.getPoolEntry(timeout, tunit); michael@0: return new BasicPooledConnAdapter(ThreadSafeClientConnManager.this, entry); michael@0: } michael@0: michael@0: }; michael@0: michael@0: } michael@0: michael@0: public void releaseConnection(ManagedClientConnection conn, long validDuration, TimeUnit timeUnit) { michael@0: michael@0: if (!(conn instanceof BasicPooledConnAdapter)) { michael@0: throw new IllegalArgumentException michael@0: ("Connection class mismatch, " + michael@0: "connection not obtained from this manager."); michael@0: } michael@0: BasicPooledConnAdapter hca = (BasicPooledConnAdapter) conn; michael@0: if ((hca.getPoolEntry() != null) && (hca.getManager() != this)) { michael@0: throw new IllegalArgumentException michael@0: ("Connection not obtained from this manager."); michael@0: } michael@0: synchronized (hca) { michael@0: BasicPoolEntry entry = (BasicPoolEntry) hca.getPoolEntry(); michael@0: if (entry == null) { michael@0: return; michael@0: } michael@0: try { michael@0: // make sure that the response has been read completely michael@0: if (hca.isOpen() && !hca.isMarkedReusable()) { michael@0: // In MTHCM, there would be a call to michael@0: // SimpleHttpConnectionManager.finishLastResponse(conn); michael@0: // Consuming the response is handled outside in 4.0. michael@0: michael@0: // make sure this connection will not be re-used michael@0: // Shut down rather than close, we might have gotten here michael@0: // because of a shutdown trigger. michael@0: // Shutdown of the adapter also clears the tracked route. michael@0: hca.shutdown(); michael@0: } michael@0: } catch (IOException iox) { michael@0: if (log.isDebugEnabled()) michael@0: log.debug("Exception shutting down released connection.", michael@0: iox); michael@0: } finally { michael@0: boolean reusable = hca.isMarkedReusable(); michael@0: if (log.isDebugEnabled()) { michael@0: if (reusable) { michael@0: log.debug("Released connection is reusable."); michael@0: } else { michael@0: log.debug("Released connection is not reusable."); michael@0: } michael@0: } michael@0: hca.detach(); michael@0: pool.freeEntry(entry, reusable, validDuration, timeUnit); michael@0: } michael@0: } michael@0: } michael@0: michael@0: public void shutdown() { michael@0: log.debug("Shutting down"); michael@0: pool.shutdown(); michael@0: } michael@0: michael@0: /** michael@0: * Gets the total number of pooled connections for the given route. michael@0: * This is the total number of connections that have been created and michael@0: * are still in use by this connection manager for the route. michael@0: * This value will not exceed the maximum number of connections per host. michael@0: * michael@0: * @param route the route in question michael@0: * michael@0: * @return the total number of pooled connections for that route michael@0: */ michael@0: public int getConnectionsInPool(final HttpRoute route) { michael@0: return pool.getConnectionsInPool(route); michael@0: } michael@0: michael@0: /** michael@0: * Gets the total number of pooled connections. This is the total number of michael@0: * connections that have been created and are still in use by this connection michael@0: * manager. This value will not exceed the maximum number of connections michael@0: * in total. michael@0: * michael@0: * @return the total number of pooled connections michael@0: */ michael@0: public int getConnectionsInPool() { michael@0: return pool.getConnectionsInPool(); michael@0: } michael@0: michael@0: public void closeIdleConnections(long idleTimeout, TimeUnit tunit) { michael@0: if (log.isDebugEnabled()) { michael@0: log.debug("Closing connections idle longer than " + idleTimeout + " " + tunit); michael@0: } michael@0: pool.closeIdleConnections(idleTimeout, tunit); michael@0: } michael@0: michael@0: public void closeExpiredConnections() { michael@0: log.debug("Closing expired connections"); michael@0: pool.closeExpiredConnections(); michael@0: } michael@0: michael@0: /** michael@0: * since 4.1 michael@0: */ michael@0: public int getMaxTotal() { michael@0: return pool.getMaxTotalConnections(); michael@0: } michael@0: michael@0: /** michael@0: * since 4.1 michael@0: */ michael@0: public void setMaxTotal(int max) { michael@0: pool.setMaxTotalConnections(max); michael@0: } michael@0: michael@0: /** michael@0: * @since 4.1 michael@0: */ michael@0: public int getDefaultMaxPerRoute() { michael@0: return connPerRoute.getDefaultMaxPerRoute(); michael@0: } michael@0: michael@0: /** michael@0: * @since 4.1 michael@0: */ michael@0: public void setDefaultMaxPerRoute(int max) { michael@0: connPerRoute.setDefaultMaxPerRoute(max); michael@0: } michael@0: michael@0: /** michael@0: * @since 4.1 michael@0: */ michael@0: public int getMaxForRoute(final HttpRoute route) { michael@0: return connPerRoute.getMaxForRoute(route); michael@0: } michael@0: michael@0: /** michael@0: * @since 4.1 michael@0: */ michael@0: public void setMaxForRoute(final HttpRoute route, int max) { michael@0: connPerRoute.setMaxForRoute(route, max); michael@0: } michael@0: michael@0: } michael@0: