@@ -73,6 +73,8 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
73
73
74
74
private long connectionRequestTimeout = -1 ;
75
75
76
+
77
+ private long readTimeout = -1 ;
76
78
77
79
/**
78
80
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
@@ -136,7 +138,7 @@ public void setConnectTimeout(int connectTimeout) {
136
138
* handshakes or CONNECT requests; for that, it is required to
137
139
* use the {@link SocketConfig} on the
138
140
* {@link HttpClient} itself.
139
- * @param connectTimeout the timeout value in milliseconds
141
+ * @param connectTimeout the timeout as {@code Duration}.
140
142
* @since 6.1
141
143
* @see RequestConfig#getConnectTimeout()
142
144
* @see SocketConfig#getSoTimeout
@@ -167,7 +169,7 @@ public void setConnectionRequestTimeout(int connectionRequestTimeout) {
167
169
* A timeout value of 0 specifies an infinite timeout.
168
170
* <p>Additional properties can be configured by specifying a
169
171
* {@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}.
171
173
* @since 6.1
172
174
* @see RequestConfig#getConnectionRequestTimeout()
173
175
*/
@@ -177,6 +179,43 @@ public void setConnectionRequestTimeout(Duration connectionRequestTimeout) {
177
179
this .connectionRequestTimeout = connectionRequestTimeout .toMillis ();
178
180
}
179
181
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
+
180
219
/**
181
220
* Indicates whether this request factory should buffer the request body internally.
182
221
* <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) {
262
301
*/
263
302
@ SuppressWarnings ("deprecation" ) // setConnectTimeout
264
303
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
266
305
return clientConfig ;
267
306
}
268
307
@@ -273,6 +312,9 @@ protected RequestConfig mergeRequestConfig(RequestConfig clientConfig) {
273
312
if (this .connectionRequestTimeout >= 0 ) {
274
313
builder .setConnectionRequestTimeout (this .connectionRequestTimeout , TimeUnit .MILLISECONDS );
275
314
}
315
+ if (this .readTimeout >= 0 ) {
316
+ builder .setResponseTimeout (this .readTimeout , TimeUnit .MILLISECONDS );
317
+ }
276
318
return builder .build ();
277
319
}
278
320
0 commit comments