Skip to content

Commit 453e796

Browse files
cmagnusonsimonbasle
authored andcommitted
Add read timeout setter on HttpComponentsClientHttpRequestFactory
This commit adds support for HttpComponentsClientHttpRequestFactory to set a read timeout, used on the underlying client as a RequestTimeout. This brings the HttpComponentsClientHttpRequestFactory into alignment with JDK, Jetty, OkHttp3, Reactor and SimpleClient ClientHttpRequestFactory implementations. See gh-33556
1 parent 6f09c21 commit 453e796

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
7373

7474
private long connectionRequestTimeout = -1;
7575

76+
77+
private long readTimeout = -1;
7678

7779
/**
7880
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
@@ -136,7 +138,7 @@ public void setConnectTimeout(int connectTimeout) {
136138
* handshakes or CONNECT requests; for that, it is required to
137139
* use the {@link SocketConfig} on the
138140
* {@link HttpClient} itself.
139-
* @param connectTimeout the timeout value in milliseconds
141+
* @param connectTimeout the timeout as {@code Duration}.
140142
* @since 6.1
141143
* @see RequestConfig#getConnectTimeout()
142144
* @see SocketConfig#getSoTimeout
@@ -167,7 +169,7 @@ public void setConnectionRequestTimeout(int connectionRequestTimeout) {
167169
* A timeout value of 0 specifies an infinite timeout.
168170
* <p>Additional properties can be configured by specifying a
169171
* {@link RequestConfig} instance on a custom {@link HttpClient}.
170-
* @param connectionRequestTimeout the timeout value to request a connection in milliseconds
172+
* @param connectionRequestTimeout the timeout value to request a connection as {@code Duration}.
171173
* @since 6.1
172174
* @see RequestConfig#getConnectionRequestTimeout()
173175
*/
@@ -177,6 +179,43 @@ public void setConnectionRequestTimeout(Duration connectionRequestTimeout) {
177179
this.connectionRequestTimeout = connectionRequestTimeout.toMillis();
178180
}
179181

182+
/**
183+
* Set the response timeout for the underlying {@link RequestConfig}.
184+
* A timeout value of 0 specifies an infinite timeout.
185+
* <p>Additional properties can be configured by specifying a
186+
* {@link RequestConfig} instance on a custom {@link HttpClient}.
187+
* <p>This options does not affect connection timeouts for SSL
188+
* handshakes or CONNECT requests; for that, it is required to
189+
* use the {@link SocketConfig} on the
190+
* {@link HttpClient} itself.
191+
* @param readTimeout the timeout value in milliseconds
192+
* @since 6.2
193+
* @see RequestConfig#getResponseTimeout()
194+
*/
195+
public void setReadTimeout(int readTimeout) {
196+
Assert.isTrue(readTimeout >= 0, "Timeout must be a non-negative value");
197+
this.readTimeout = readTimeout;
198+
}
199+
200+
/**
201+
* Set the response timeout for the underlying {@link RequestConfig}.
202+
* A timeout value of 0 specifies an infinite timeout.
203+
* <p>Additional properties can be configured by specifying a
204+
* {@link RequestConfig} instance on a custom {@link HttpClient}.
205+
* <p>This options does not affect connection timeouts for SSL
206+
* handshakes or CONNECT requests; for that, it is required to
207+
* use the {@link SocketConfig} on the
208+
* {@link HttpClient} itself.
209+
* @param readTimeout the timeout as {@code Duration}.
210+
* @since 6.2
211+
* @see RequestConfig#getResponseTimeout()
212+
*/
213+
public void setReadTimeout(Duration readTimeout) {
214+
Assert.notNull(readTimeout, "ReadTimeout must not be null");
215+
Assert.isTrue(!readTimeout.isNegative(), "Timeout must be a non-negative value");
216+
this.readTimeout = readTimeout.toMillis();
217+
}
218+
180219
/**
181220
* Indicates whether this request factory should buffer the request body internally.
182221
* <p>Default is {@code true}. When sending large amounts of data via POST or PUT, it is
@@ -262,7 +301,7 @@ protected RequestConfig createRequestConfig(Object client) {
262301
*/
263302
@SuppressWarnings("deprecation") // setConnectTimeout
264303
protected RequestConfig mergeRequestConfig(RequestConfig clientConfig) {
265-
if (this.connectTimeout == -1 && this.connectionRequestTimeout == -1) { // nothing to merge
304+
if (this.connectTimeout == -1 && this.connectionRequestTimeout == -1 && this.readTimeout == -1) { // nothing to merge
266305
return clientConfig;
267306
}
268307

@@ -273,6 +312,9 @@ protected RequestConfig mergeRequestConfig(RequestConfig clientConfig) {
273312
if (this.connectionRequestTimeout >= 0) {
274313
builder.setConnectionRequestTimeout(this.connectionRequestTimeout, TimeUnit.MILLISECONDS);
275314
}
315+
if (this.readTimeout >= 0) {
316+
builder.setResponseTimeout(this.readTimeout, TimeUnit.MILLISECONDS);
317+
}
276318
return builder.build();
277319
}
278320

0 commit comments

Comments
 (0)