You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by mb...@apache.org on 2004/07/17 20:58:33 UTC

cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestHttpConnectionManager.java

mbecke      2004/07/17 11:58:33

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HostConfiguration.java
                        MultiThreadedHttpConnectionManager.java
               httpclient/src/java/org/apache/commons/httpclient/params
                        HttpConnectionManagerParams.java
               httpclient release_notes.txt
               httpclient/src/test/org/apache/commons/httpclient
                        TestHttpConnectionManager.java
  Log:
  MultiThreadedHttpConnectionManager now support configuring the maximum number of connections on a per host basis.
  
  PR: 29636
  Submitted by: Michael Becke
  Reviewed by: Oleg Kalnichevski
  
  Revision  Changes    Path
  1.19      +10 -3     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HostConfiguration.java
  
  Index: HostConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HostConfiguration.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- HostConfiguration.java	17 May 2004 21:46:03 -0000	1.18
  +++ HostConfiguration.java	17 Jul 2004 18:58:33 -0000	1.19
  @@ -47,6 +47,13 @@
    */
   public class HostConfiguration implements Cloneable {
   
  +    /**
  +     * A value to represent any host configuration, instead of using something like
  +     * <code>null</code>. This value should be treated as immutable and only used in 
  +     * lookups and other such places to represent "any" host config.
  +     */
  +    public static final HostConfiguration ANY_HOST_CONFIGURATION = new HostConfiguration();
  +
       /** The host to use. */
       private String host;
   
  
  
  
  1.42      +13 -25    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java
  
  Index: MultiThreadedHttpConnectionManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -r1.41 -r1.42
  --- MultiThreadedHttpConnectionManager.java	5 Jul 2004 22:46:58 -0000	1.41
  +++ MultiThreadedHttpConnectionManager.java	17 Jul 2004 18:58:33 -0000	1.42
  @@ -295,13 +295,11 @@
        * @param maxHostConnections the number of connections allowed for each
        * hostConfiguration
        * 
  -     * @deprecated Use {@link HttpConnectionManagerParams#MAX_HOST_CONNECTIONS},
  +     * @deprecated Use {@link HttpConnectionManagerParams#setDefaultMaxConnectionsPerHost(int)},
        * {@link HttpConnectionManager#getParams()}.
        */
       public void setMaxConnectionsPerHost(int maxHostConnections) {
  -        this.params.setIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS,
  -            maxHostConnections);
  +        this.params.setDefaultMaxConnectionsPerHost(maxHostConnections);
       }
   
       /**
  @@ -311,13 +309,11 @@
        * @return The maximum number of connections allowed for a given
        * hostConfiguration.
        * 
  -     * @deprecated Use {@link HttpConnectionManagerParams#MAX_HOST_CONNECTIONS},
  +     * @deprecated Use {@link HttpConnectionManagerParams#getDefaultMaxConnectionsPerHost()},
        * {@link HttpConnectionManager#getParams()}.
        */
       public int getMaxConnectionsPerHost() {
  -        return this.params.getIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS,
  -            DEFAULT_MAX_HOST_CONNECTIONS);
  +        return this.params.getDefaultMaxConnectionsPerHost();
       }
   
       /**
  @@ -325,13 +321,11 @@
        *
        * @param maxTotalConnections the maximum number of connections allowed
        * 
  -     * @deprecated Use {@link HttpConnectionManagerParams#MAX_TOTAL_CONNECTIONS},
  +     * @deprecated Use {@link HttpConnectionManagerParams#setMaxTotalConnections(int)(int)},
        * {@link HttpConnectionManager#getParams()}.
        */
       public void setMaxTotalConnections(int maxTotalConnections) {
  -        this.params.setIntParameter(
  -            HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
  -            maxTotalConnections);
  +        this.params.getMaxTotalConnections();
       }
   
       /**
  @@ -339,13 +333,11 @@
        *
        * @return The maximum number of connections allowed
        * 
  -     * @deprecated Use {@link HttpConnectionManagerParams#MAX_TOTAL_CONNECTIONS},
  +     * @deprecated Use {@link HttpConnectionManagerParams#getMaxTotalConnections()},
        * {@link HttpConnectionManager#getParams()}.
        */
       public int getMaxTotalConnections() {
  -        return this.params.getIntParameter(
  -            HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
  -            DEFAULT_MAX_TOTAL_CONNECTIONS);
  +        return this.params.getMaxTotalConnections();
       }
   
       /**
  @@ -430,12 +422,8 @@
   
           HttpConnection connection = null;
   
  -        int maxHostConnections = this.params.getIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS,
  -            DEFAULT_MAX_HOST_CONNECTIONS);
  -        int maxTotalConnections = this.params.getIntParameter(
  -            HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
  -            DEFAULT_MAX_TOTAL_CONNECTIONS);
  +        int maxHostConnections = this.params.getMaxConnectionsPerHost(hostConfiguration);
  +        int maxTotalConnections = this.params.getMaxTotalConnections();
           
           synchronized (connectionPool) {
   
  
  
  
  1.7       +129 -7    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HttpConnectionManagerParams.java
  
  Index: HttpConnectionManagerParams.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HttpConnectionManagerParams.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- HttpConnectionManagerParams.java	13 May 2004 04:01:22 -0000	1.6
  +++ HttpConnectionManagerParams.java	17 Jul 2004 18:58:33 -0000	1.7
  @@ -29,6 +29,13 @@
   
   package org.apache.commons.httpclient.params;
   
  +import java.util.Collections;
  +import java.util.HashMap;
  +import java.util.Map;
  +
  +import org.apache.commons.httpclient.HostConfiguration;
  +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
  +
   /**
    * This class represents a collection of HTTP protocol parameters applicable to 
    * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection managers}. 
  @@ -37,6 +44,7 @@
    * value will be drawn from the parent collection of parameters.
    * 
    * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  + * @author Michael Becke
    * 
    * @version $Revision$
    * 
  @@ -45,19 +53,133 @@
   public class HttpConnectionManagerParams extends HttpConnectionParams {
   
       /** 
  -     * Defines the maximum number of connections allowed per host. 
  +     * Defines the maximum number of connections allowed per host configuration. 
  +     * These values only apply to the number of connections from a particular instance 
  +     * of HttpConnectionManager.
        * <p>
  -     * This parameter expects a value of type {@link Integer}.
  +     * This parameter expects a value of type {@link java.util.Map}.  The value
  +     * should map instances of {@link org.apache.commons.httpclient.HostConfiguration}
  +     * to {@link Integer integers).  The default value can be specified using
  +     * {@link org.apache.commons.httpclient.HostConfiguration#ANY_HOST_CONFIGURATION}.
        * </p>
        */
       public static String MAX_HOST_CONNECTIONS = "http.connection-manager.max-per-host";
   
       /** 
  -     * Defines the maximum number of connections allowed overall.
  +     * Defines the maximum number of connections allowed overall. This value only applies
  +     * to the number of connections from a particular instance of HttpConnectionManager.
        * <p>
        * This parameter expects a value of type {@link Integer}.
        * </p>
        */
       public static String MAX_TOTAL_CONNECTIONS = "http.connection-manager.max-total";
  +    
  +    /**
  +     * Sets the default maximum number of connections allowed for a given
  +     * host config.
  +     *
  +     * @param maxHostConnections The default maximum.
  +     * 
  +     * @see #MAX_HOST_CONNECTIONS
  +     */
  +    public void setDefaultMaxConnectionsPerHost(int maxHostConnections) {
  +        setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxHostConnections);
  +    }
  +
  +    /**
  +     * Sets the maximum number of connections to be used for the given host config.
  +     * 
  +     * @param hostConfiguration The host config to set the maximum for.  Use 
  +     * {@link HostConfiguration#ANY_HOST_CONFIGURATION} to configure the default value
  +     * per host.
  +     * @param maxHostConnections The maximum number of connections, <code>> 0</code>
  +     * 
  +     * @see #MAX_HOST_CONNECTIONS
  +     */
  +    public void setMaxConnectionsPerHost(
  +        HostConfiguration hostConfiguration,
  +        int maxHostConnections) {
  +        
  +        if (maxHostConnections <= 0) {
  +            throw new IllegalArgumentException("maxHostConnections must be greater than 0");
  +        }
  +        
  +        Map currentValues = (Map) getParameter(MAX_HOST_CONNECTIONS);
  +        // param values are meant to be immutable so we'll make a copy
  +        // to modify
  +        Map newValues = new HashMap(currentValues == null ? Collections.EMPTY_MAP : currentValues);
  +        newValues.put(hostConfiguration, new Integer(maxHostConnections));
  +        setParameter(MAX_HOST_CONNECTIONS, newValues);
  +    }
  +    
  +    /**
  +     * Gets the default maximum number of connections allowed for a given
  +     * host config.
  +     *
  +     * @return The default maximum.
  +     * 
  +     * @see #MAX_HOST_CONNECTIONS
  +     */
  +    public int getDefaultMaxConnectionsPerHost() {
  +        return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION);
  +    }
  +
  +    /**
  +     * Gets the maximum number of connections to be used for a particular host config.  If
  +     * the value has not been specified for the given host the default value will be
  +     * returned.
  +     * 
  +     * @param hostConfiguration The host config.
  +     * @return The maximum number of connections to be used for the given host config.
  +     * 
  +     * @see #MAX_HOST_CONNECTIONS
  +     */
  +    public int getMaxConnectionsPerHost(HostConfiguration hostConfiguration) {
  +        
  +        Map m = (Map) getParameter(MAX_HOST_CONNECTIONS);
  +        if (m == null) {
  +            // MAX_HOST_CONNECTIONS have not been configured, using the default value
  +            return MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS;
  +        } else {
  +            Integer max = (Integer) m.get(hostConfiguration);
  +            if (max == null && hostConfiguration != HostConfiguration.ANY_HOST_CONFIGURATION) {
  +                // the value has not been configured specifically for this host config,
  +                // use the default value
  +                return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION);
  +            } else {
  +                return (
  +                        max == null 
  +                        ? MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS 
  +                        : max.intValue()
  +                    );
  +            }
  +        }
  +    }
  +
  +    /**
  +     * Sets the maximum number of connections allowed.
  +     *
  +     * @param maxTotalConnections The maximum number of connections allowed.
  +     * 
  +     * @see #MAX_TOTAL_CONNECTIONS
  +     */
  +    public void setMaxTotalConnections(int maxTotalConnections) {
  +        setIntParameter(
  +            HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
  +            maxTotalConnections);
  +    }
  +
  +    /**
  +     * Gets the maximum number of connections allowed.
  +     *
  +     * @return The maximum number of connections allowed.
  +     * 
  +     * @see #MAX_TOTAL_CONNECTIONS
  +     */
  +    public int getMaxTotalConnections() {
  +        return getIntParameter(
  +            HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
  +            MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS);
  +    }
   
   }
  
  
  
  1.30      +4 -0      jakarta-commons/httpclient/release_notes.txt
  
  Index: release_notes.txt
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/release_notes.txt,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- release_notes.txt	6 Jul 2004 22:00:17 -0000	1.29
  +++ release_notes.txt	17 Jul 2004 18:58:33 -0000	1.30
  @@ -2,6 +2,10 @@
   -------------------
   Changes since Alpha 1:
   
  + * 29636 - MultiThreadedHttpConnectionManager now support configuring the maximum
  +           number of connections on a per host basis.
  +           Contributed by Michael Becke <mbecke at apache.org>
  +
    * 29874 - Deprecated old HTTP method retry handler based on HttpRecoverableException
              and replaced it with a new one that acts upon plain IOExceptions
              Contributed by Oleg Kalnichevski <olegk at apache.org>
  
  
  
  1.23      +19 -34    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java
  
  Index: TestHttpConnectionManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- TestHttpConnectionManager.java	25 Jun 2004 03:34:56 -0000	1.22
  +++ TestHttpConnectionManager.java	17 Jul 2004 18:58:33 -0000	1.23
  @@ -219,8 +219,7 @@
       public void testWriteRequestReleaseConnection() {
   
           MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1);
  +        connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);
   
           HttpClient client = createHttpClient(connectionManager);
           
  @@ -253,8 +252,7 @@
       public void testReleaseConnection() {
   
           MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1);
  +        connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);
   
           HttpClient client = createHttpClient(connectionManager);
           // we shouldn't have to wait if a connection is available
  @@ -303,8 +301,7 @@
       public void testResponseAutoRelease() throws Exception  {
   
           MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1);
  +        connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);
   
           HttpClient client = createHttpClient(connectionManager);
           // we shouldn't have to wait if a connection is available
  @@ -341,10 +338,8 @@
       public void testConnectionReclaiming() {
           
           MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1);
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 1);
  +        connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);
  +        connectionManager.getParams().setMaxTotalConnections(1);
   
           HostConfiguration host1 = new HostConfiguration();
           host1.setHost("host1", -1, "http");
  @@ -373,10 +368,8 @@
       public void testShutdownAll() {
   
           MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1);
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 1);
  +        connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);
  +        connectionManager.getParams().setMaxTotalConnections(1);
   
           HostConfiguration host1 = new HostConfiguration();
           host1.setHost("host1", -1, "http");
  @@ -419,10 +412,8 @@
       public void testShutdown() {
   
           MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1);
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 1);
  +        connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);
  +        connectionManager.getParams().setMaxTotalConnections(1);
   
           HostConfiguration host1 = new HostConfiguration();
           host1.setHost("host1", -1, "http");
  @@ -465,10 +456,8 @@
       public void testMaxConnections() {
           
           MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1);
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 2);
  +        connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);
  +        connectionManager.getParams().setMaxTotalConnections(2);
   
           HostConfiguration host1 = new HostConfiguration();
           host1.setHost("host1", -1, "http");
  @@ -503,10 +492,8 @@
       public void testHostReusePreference() {
           
           final MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1);
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 1);
  +        connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);
  +        connectionManager.getParams().setMaxTotalConnections(1);
   
           final HostConfiguration host1 = new HostConfiguration();
           host1.setHost("host1", -1, "http");
  @@ -553,8 +540,7 @@
       public void testMaxConnectionsPerServer() {
        
           MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
  -        connectionManager.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1);
  +        connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);
   
           HttpClient client = createHttpClient(connectionManager);
           // we shouldn't have to wait if a connection is available
  @@ -674,8 +660,7 @@
   
       public void testTimeout() {
           MultiThreadedHttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
  -        mgr.getParams().setIntParameter(
  -            HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 2);
  +        mgr.getParams().setDefaultMaxConnectionsPerHost(2);
           
           try{
               HostConfiguration hostConfig = new HostConfiguration();
  
  
  

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