Skip to content

Commit 672d947

Browse files
authored
BAEL-8908 Reading with timeout in HttpComponentsClientHttpRequestFactory (#18380)
* Update pom.xml * Update RestTemplateBasicLiveTest.java
1 parent d05a006 commit 672d947

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

spring-web-modules/spring-resttemplate/pom.xml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616

1717
<dependencies>
1818
<!-- Spring Boot Dependencies -->
19+
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
20+
<dependency>
21+
<groupId>org.apache.httpcomponents.client5</groupId>
22+
<artifactId>httpclient5</artifactId>
23+
<version>5.4.2</version>
24+
</dependency>
25+
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.core5/httpcore5 -->
26+
<dependency>
27+
<groupId>org.apache.httpcomponents.core5</groupId>
28+
<artifactId>httpcore5</artifactId>
29+
<version>5.3.3</version>
30+
</dependency>
1931
<dependency>
2032
<groupId>org.springframework.boot</groupId>
2133
<artifactId>spring-boot-starter-web</artifactId>
@@ -205,11 +217,11 @@
205217
</profiles>
206218

207219
<properties>
208-
<xstream.version>1.4.20</xstream.version>
220+
<xstream.version>1.4.21</xstream.version>
209221
<!-- Maven plugins -->
210-
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
222+
<cargo-maven2-plugin.version>1.9.0</cargo-maven2-plugin.version>
211223
<!-- okhttp -->
212224
<com.squareup.okhttp3.version>4.12.0</com.squareup.okhttp3.version>
213225
</properties>
214226

215-
</project>
227+
</project>

spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/resttemplate/RestTemplateBasicLiveTest.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@
2828
import org.springframework.util.MultiValueMap;
2929
import org.springframework.web.client.HttpClientErrorException;
3030
import org.springframework.web.client.RequestCallback;
31-
import org.springframework.web.client.RestTemplate;
31+
import org.apache.hc.client5.http.config.RequestConfig;
32+
import org.apache.hc.core5.util.Timeout;
33+
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
34+
import org.apache.hc.core5.http.io.SocketConfig;
35+
import org.apache.hc.client5.http.config.ConnectionConfig;
36+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
37+
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
3238

3339
import com.fasterxml.jackson.databind.JsonNode;
3440
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -187,6 +193,7 @@ public void givenFooService_whenPatchExistingEntity_thenItIsUpdated() {
187193
.getId();
188194
final HttpEntity<Foo> requestUpdate = new HttpEntity<>(updatedResource, headers);
189195
final ClientHttpRequestFactory requestFactory = getClientHttpRequestFactory();
196+
final ClientHttpRequestFactory requestFactoryAlternate = getClientHttpRequestFactoryAlternate();
190197
final RestTemplate template = new RestTemplate(requestFactory);
191198
template.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
192199
template.patchForObject(resourceUrl, requestUpdate, Void.class);
@@ -262,9 +269,44 @@ private RequestCallback requestCallback(final Foo updatedInstance) {
262269

263270
ClientHttpRequestFactory getClientHttpRequestFactory() {
264271
final int timeout = 5;
265-
final HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
266-
clientHttpRequestFactory.setConnectTimeout(timeout * 1000);
272+
final HttpComponentsClientHttpRequestFactory
273+
clientHttpRequestFactory = new
274+
HttpComponentsClientHttpRequestFactory();
275+
clientHttpRequestFactory.setConnectionRequestTimeout(timeout * 1000);
276+
clientHttpRequestFactory.setConnectTimeout(timeout * 2000);
277+
//clientHttpRequestFactory.setReadTimeout(timeout * 3000);
267278
return clientHttpRequestFactory;
268-
}
279+
}
280+
281+
// Alternate GET ClientHttpRequestFactory
282+
ClientHttpRequestFactory getClientHttpRequestFactoryAlternate() {
283+
long timeout = 5;
284+
int readTimeout = 5;
285+
// Connect timeout
286+
ConnectionConfig connectionConfig = ConnectionConfig.custom()
287+
.setConnectTimeout(Timeout.ofMilliseconds(timeout * 2000))
288+
.build();
289+
// Connection request timeout
290+
RequestConfig requestConfig = RequestConfig.custom()
291+
.setConnectionRequestTimeout(Timeout.ofMilliseconds(timeout * 1000))
292+
.build();
293+
// Socket timeout
294+
SocketConfig socketConfig = SocketConfig.custom()
295+
.setSoTimeout(Timeout.ofMilliseconds(timeout * 1000)).build();
296+
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
297+
connectionManager.setDefaultSocketConfig(socketConfig);
298+
connectionManager.setDefaultConnectionConfig(connectionConfig);
299+
CloseableHttpClient httpClient = HttpClientBuilder.create()
300+
.setConnectionManager(connectionManager)
301+
.setDefaultRequestConfig(requestConfig)
302+
.build();
303+
304+
/**final HttpComponentsClientHttpRequestFactory
305+
clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
306+
clientHttpRequestFactory.setReadTimeout(readTimeout*3000);**/
307+
//return clientHttpRequestFactory;
308+
309+
return new HttpComponentsClientHttpRequestFactory(httpClient);
310+
}
269311

270312
}

0 commit comments

Comments
 (0)