|
18 | 18 |
|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.net.URI; |
| 21 | +import java.util.Iterator; |
| 22 | +import java.util.Properties; |
21 | 23 |
|
22 | 24 | import org.apache.commons.httpclient.Credentials; |
| 25 | +import org.apache.commons.httpclient.HostConfiguration; |
23 | 26 | import org.apache.commons.httpclient.HttpClient; |
24 | 27 | import org.apache.commons.httpclient.HttpConnectionManager; |
| 28 | +import org.apache.commons.httpclient.HttpURL; |
| 29 | +import org.apache.commons.httpclient.HttpsURL; |
25 | 30 | import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; |
26 | 31 | import org.apache.commons.httpclient.NTCredentials; |
| 32 | +import org.apache.commons.httpclient.URIException; |
27 | 33 | import org.apache.commons.httpclient.UsernamePasswordCredentials; |
28 | 34 | import org.apache.commons.httpclient.auth.AuthScope; |
29 | 35 | import org.apache.commons.httpclient.methods.PostMethod; |
@@ -131,6 +137,59 @@ public void setReadTimeout(int timeout) { |
131 | 137 | getHttpClient().getHttpConnectionManager().getParams().setSoTimeout(timeout); |
132 | 138 | } |
133 | 139 |
|
| 140 | + /** |
| 141 | + * Sets the maximum number of connections allowed for the underlying HttpClient. |
| 142 | + * |
| 143 | + * @param maxTotalConnections the maximum number of connections allowed |
| 144 | + * @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setMaxTotalConnections(int) |
| 145 | + */ |
| 146 | + public void setMaxTotalConnections(int maxTotalConnections) { |
| 147 | + if (maxTotalConnections <= 0) { |
| 148 | + throw new IllegalArgumentException("maxTotalConnections must be a positive value"); |
| 149 | + } |
| 150 | + getHttpClient().getHttpConnectionManager().getParams().setMaxTotalConnections(maxTotalConnections); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Sets the maximum number of connections per host for the underlying HttpClient. The maximum number of connections |
| 155 | + * per host can be set in a form accepted by the {@code java.util.Properties} class, like as follows: |
| 156 | + * <pre> |
| 157 | + * https://www.example.com=1 |
| 158 | + * http://www.example.com:8080=7 |
| 159 | + * www.springframework.org=10 |
| 160 | + * *=5 |
| 161 | + * </pre> |
| 162 | + * The host can be specified as hostname, or as URI (with scheme and port). The special host name {@code *} can be |
| 163 | + * used to specify {@link org.apache.commons.httpclient.HostConfiguration#ANY_HOST_CONFIGURATION}. |
| 164 | + * |
| 165 | + * @param maxConnectionsPerHost a properties object specifying the maximum number of connection |
| 166 | + * @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setMaxConnectionsPerHost(org.apache.commons.httpclient.HostConfiguration, |
| 167 | + * int) |
| 168 | + */ |
| 169 | + public void setMaxConnectionsPerHost(Properties maxConnectionsPerHost) throws URIException { |
| 170 | + for (Iterator iterator = maxConnectionsPerHost.keySet().iterator(); iterator.hasNext();) { |
| 171 | + String host = (String) iterator.next(); |
| 172 | + HostConfiguration hostConfiguration = new HostConfiguration(); |
| 173 | + if ("*".equals(host)) { |
| 174 | + hostConfiguration = HostConfiguration.ANY_HOST_CONFIGURATION; |
| 175 | + } |
| 176 | + else if (host.startsWith("http://")) { |
| 177 | + HttpURL httpURL = new HttpURL(host); |
| 178 | + hostConfiguration.setHost(httpURL); |
| 179 | + } |
| 180 | + else if (host.startsWith("https://")) { |
| 181 | + HttpsURL httpsURL = new HttpsURL(host); |
| 182 | + hostConfiguration.setHost(httpsURL); |
| 183 | + } |
| 184 | + else { |
| 185 | + hostConfiguration.setHost(host); |
| 186 | + } |
| 187 | + int maxHostConnections = Integer.parseInt(maxConnectionsPerHost.getProperty(host)); |
| 188 | + getHttpClient().getHttpConnectionManager().getParams() |
| 189 | + .setMaxConnectionsPerHost(hostConfiguration, maxHostConnections); |
| 190 | + } |
| 191 | + } |
| 192 | + |
134 | 193 | /** |
135 | 194 | * Returns the authentication scope to be used. Only used when the <code>credentials</code> property has been set. |
136 | 195 | * <p/> |
|
0 commit comments