You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2005/10/09 21:53:41 UTC

svn commit: r312494 - in /jakarta/commons/proper/httpclient/trunk/src: java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java test/org/apache/commons/httpclient/TestHttpConnectionManager.java

Author: olegk
Date: Sun Oct  9 12:53:32 2005
New Revision: 312494

URL: http://svn.apache.org/viewcvs?rev=312494&view=rev
Log:
PR #36882 (max connections per host setting does not work)

Contributed by Oleg Kalnichevski
Reviewed by Michael Becke

Modified:
    jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java
    jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java

Modified: jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java?rev=312494&r1=312493&r2=312494&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java Sun Oct  9 12:53:32 2005
@@ -734,38 +734,20 @@
          * @return a new connection or <code>null</code> if none are available
          */
         public synchronized HttpConnection createConnection(HostConfiguration hostConfiguration) {
-            
-            HttpConnectionWithReference connection = null;
-
             HostConnectionPool hostPool = getHostPool(hostConfiguration);
-
-            if ((hostPool.numConnections < getMaxConnectionsPerHost()) 
-                && (numConnections < getMaxTotalConnections())) {
-
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Allocating new connection, hostConfig=" + hostConfiguration);
-                }
-                connection = new HttpConnectionWithReference(hostConfiguration);
-                connection.getParams().setDefaults(MultiThreadedHttpConnectionManager.this.params);
-                connection.setHttpConnectionManager(MultiThreadedHttpConnectionManager.this);
-                numConnections++;
-                hostPool.numConnections++;
-        
-                // store a reference to this connection so that it can be cleaned up
-                // in the event it is not correctly released
-                storeReferenceToConnection(connection, hostConfiguration, this);
-
-            } else if (LOG.isDebugEnabled()) {
-                if (hostPool.numConnections >= getMaxConnectionsPerHost()) {
-                    LOG.debug("No connection allocated, host pool has already reached "
-                        + "maxConnectionsPerHost, hostConfig=" + hostConfiguration
-                        + ", maxConnectionsPerhost=" + getMaxConnectionsPerHost());
-                } else {
-                    LOG.debug("No connection allocated, maxTotalConnections reached, "
-                        + "maxTotalConnections=" + getMaxTotalConnections());
-                }
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Allocating new connection, hostConfig=" + hostConfiguration);
             }
-            
+            HttpConnectionWithReference connection = new HttpConnectionWithReference(
+                    hostConfiguration);
+            connection.getParams().setDefaults(MultiThreadedHttpConnectionManager.this.params);
+            connection.setHttpConnectionManager(MultiThreadedHttpConnectionManager.this);
+            numConnections++;
+            hostPool.numConnections++;
+    
+            // store a reference to this connection so that it can be cleaned up
+            // in the event it is not correctly released
+            storeReferenceToConnection(connection, hostConfiguration, this);
             return connection;
         }
     

Modified: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java?rev=312494&r1=312493&r2=312494&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java Sun Oct  9 12:53:32 2005
@@ -23,9 +23,6 @@
  * individuals on behalf of the Apache Software Foundation.  For more
  * information on the Apache Software Foundation, please see
  * <http://www.apache.org/>.
- *
- * [Additional notices, if required by prior licensing conditions]
- *
  */
 
 package org.apache.commons.httpclient;
@@ -480,8 +477,8 @@
         try {
             // this should fail quickly since the connection has not been released
             connectionManager.getConnectionWithTimeout(host2, 100);
-            fail("a httpConnection should not be available");
-        } catch (ConnectTimeoutException e) {
+            fail("ConnectionPoolTimeoutException should not be available");
+        } catch (ConnectionPoolTimeoutException e) {
             // this should throw an exception
         }
         
@@ -492,9 +489,65 @@
         try {
             // there should be a connection available now
             connection2 = connectionManager.getConnectionWithTimeout(host2, 100);
-        } catch (ConnectTimeoutException e) {
+        } catch (ConnectionPoolTimeoutException e) {
             e.printStackTrace();
             fail("a httpConnection should have been available: " + e);
+        }
+    }    
+
+    /**
+     * Tests the MultiThreadedHttpConnectionManager's ability to restrict the maximum number 
+     * of connections per host.
+     */    
+    public void testMaxConnectionsPerHost() throws Exception {
+        
+        MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
+        connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);
+        connectionManager.getParams().setMaxTotalConnections(100);
+
+        HostConfiguration host1 = new HostConfiguration();
+        host1.setHost("host1", -1, "http");
+
+        HostConfiguration host2 = new HostConfiguration();
+        host2.setHost("host2", -1, "http");
+
+        HostConfiguration host3 = new HostConfiguration();
+        host3.setHost("host3", -1, "http");
+
+        connectionManager.getParams().setMaxConnectionsPerHost(host1, 3);
+        connectionManager.getParams().setMaxConnectionsPerHost(host2, 2);
+
+        // Host1
+        HttpConnection connection1 = connectionManager.getConnectionWithTimeout(host1, 1000);
+        HttpConnection connection2 = connectionManager.getConnectionWithTimeout(host1, 1000);
+        HttpConnection connection3 = connectionManager.getConnectionWithTimeout(host1, 1000);
+        try {
+            // this should fail quickly since the connection has not been released
+            connectionManager.getConnectionWithTimeout(host1, 100);
+            fail("ConnectionPoolTimeoutException should not be available");
+        } catch (ConnectionPoolTimeoutException e) {
+            // expected
+        }
+        
+        // Host2
+        connection1 = connectionManager.getConnectionWithTimeout(host2, 1000);
+        connection2 = connectionManager.getConnectionWithTimeout(host2, 1000);
+        try {
+            // this should fail quickly since the connection has not been released
+            connectionManager.getConnectionWithTimeout(host2, 100);
+            fail("ConnectionPoolTimeoutException should not be available");
+        } catch (ConnectionPoolTimeoutException e) {
+            // expected
+        }
+
+        // Host3 (should use the default per host value)
+        connection1 = connectionManager.getConnectionWithTimeout(host3, 1000);
+        try {
+            // this should fail quickly since the connection has not been released
+            connectionManager.getConnectionWithTimeout(host3, 100);
+            fail("ConnectionPoolTimeoutException should not be available");
+        } catch (ConnectionPoolTimeoutException e) {
+            // expected
         }
     }    
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org