Skip to content

Commit 585745d

Browse files
committed
added "connectTimeout" and "readTimeout" properties to SimpleClientHttpRequestFactory (SPR-8614)
1 parent 65077d2 commit 585745d

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* {@link ClientHttpRequestFactory} implementation that uses standard J2SE facilities.
3131
*
3232
* @author Arjen Poutsma
33+
* @author Juergen Hoeller
3334
* @since 3.0
3435
* @see java.net.HttpURLConnection
3536
* @see CommonsClientHttpRequestFactory
@@ -38,6 +39,10 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory
3839

3940
private Proxy proxy;
4041

42+
private int connectTimeout = -1;
43+
44+
private int readTimeout = -1;
45+
4146

4247
/**
4348
* Set the {@link Proxy} to use for this request factory.
@@ -46,9 +51,29 @@ public void setProxy(Proxy proxy) {
4651
this.proxy = proxy;
4752
}
4853

54+
/**
55+
* Set the underlying URLConnection's connect timeout (in milliseconds).
56+
* A timeout value of 0 specifies an infinite timeout.
57+
* <p>Default is the system's default timeout.
58+
* @see URLConnection#setConnectTimeout(int)
59+
*/
60+
public void setConnectTimeout(int connectTimeout) {
61+
this.connectTimeout = connectTimeout;
62+
}
63+
64+
/**
65+
* Set the underlying URLConnection's read timeout (in milliseconds).
66+
* A timeout value of 0 specifies an infinite timeout.
67+
* <p>Default is the system's default timeout.
68+
* @see URLConnection#setReadTimeout(int)
69+
*/
70+
public void setReadTimeout(int readTimeout) {
71+
this.readTimeout = readTimeout;
72+
}
73+
4974

5075
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
51-
HttpURLConnection connection = openConnection(uri.toURL(), proxy);
76+
HttpURLConnection connection = openConnection(uri.toURL(), this.proxy);
5277
prepareConnection(connection, httpMethod.name());
5378
return new SimpleClientHttpRequest(connection);
5479
}
@@ -76,6 +101,12 @@ protected HttpURLConnection openConnection(URL url, Proxy proxy) throws IOExcept
76101
* @throws IOException in case of I/O errors
77102
*/
78103
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
104+
if (this.connectTimeout >= 0) {
105+
connection.setConnectTimeout(this.connectTimeout);
106+
}
107+
if (this.readTimeout >= 0) {
108+
connection.setReadTimeout(this.readTimeout);
109+
}
79110
connection.setDoInput(true);
80111
if ("GET".equals(httpMethod)) {
81112
connection.setInstanceFollowRedirects(true);

0 commit comments

Comments
 (0)