Skip to content

Commit 73908dd

Browse files
committed
SWS-371
1 parent f32f593 commit 73908dd

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

core/src/main/java/org/springframework/ws/transport/http/CommonsHttpMessageSender.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818

1919
import java.io.IOException;
2020
import java.net.URI;
21+
import java.util.Iterator;
22+
import java.util.Properties;
2123

2224
import org.apache.commons.httpclient.Credentials;
25+
import org.apache.commons.httpclient.HostConfiguration;
2326
import org.apache.commons.httpclient.HttpClient;
2427
import org.apache.commons.httpclient.HttpConnectionManager;
28+
import org.apache.commons.httpclient.HttpURL;
29+
import org.apache.commons.httpclient.HttpsURL;
2530
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
2631
import org.apache.commons.httpclient.NTCredentials;
32+
import org.apache.commons.httpclient.URIException;
2733
import org.apache.commons.httpclient.UsernamePasswordCredentials;
2834
import org.apache.commons.httpclient.auth.AuthScope;
2935
import org.apache.commons.httpclient.methods.PostMethod;
@@ -131,6 +137,59 @@ public void setReadTimeout(int timeout) {
131137
getHttpClient().getHttpConnectionManager().getParams().setSoTimeout(timeout);
132138
}
133139

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+
134193
/**
135194
* Returns the authentication scope to be used. Only used when the <code>credentials</code> property has been set.
136195
* <p/>

core/src/test/java/org/springframework/ws/transport/http/CommonsHttpMessageSenderIntegrationTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package org.springframework.ws.transport.http;
1818

1919
import java.net.URI;
20+
import java.net.URISyntaxException;
21+
import java.util.Properties;
2022

2123
import org.apache.commons.httpclient.ConnectTimeoutException;
24+
import org.apache.commons.httpclient.URIException;
2225

2326
import org.springframework.ws.MockWebServiceMessage;
2427
import org.springframework.ws.WebServiceMessage;
@@ -44,4 +47,15 @@ public void testConnectionTimeout() throws Exception {
4447
}
4548
}
4649

50+
public void testMaxConnections() throws URISyntaxException, URIException {
51+
CommonsHttpMessageSender messageSender = new CommonsHttpMessageSender();
52+
messageSender.setMaxTotalConnections(2);
53+
Properties maxConnectionsPerHost = new Properties();
54+
maxConnectionsPerHost.setProperty("https://www.example.com", "1");
55+
maxConnectionsPerHost.setProperty("http://www.example.com:8080", "7");
56+
maxConnectionsPerHost.setProperty("www.springframework.org", "10");
57+
maxConnectionsPerHost.setProperty("*", "5");
58+
messageSender.setMaxConnectionsPerHost(maxConnectionsPerHost);
59+
}
60+
4761
}

0 commit comments

Comments
 (0)