|
28 | 28 | import org.springframework.util.MultiValueMap;
|
29 | 29 | import org.springframework.web.client.HttpClientErrorException;
|
30 | 30 | 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; |
32 | 38 |
|
33 | 39 | import com.fasterxml.jackson.databind.JsonNode;
|
34 | 40 | import com.fasterxml.jackson.databind.ObjectMapper;
|
@@ -187,6 +193,7 @@ public void givenFooService_whenPatchExistingEntity_thenItIsUpdated() {
|
187 | 193 | .getId();
|
188 | 194 | final HttpEntity<Foo> requestUpdate = new HttpEntity<>(updatedResource, headers);
|
189 | 195 | final ClientHttpRequestFactory requestFactory = getClientHttpRequestFactory();
|
| 196 | + final ClientHttpRequestFactory requestFactoryAlternate = getClientHttpRequestFactoryAlternate(); |
190 | 197 | final RestTemplate template = new RestTemplate(requestFactory);
|
191 | 198 | template.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
|
192 | 199 | template.patchForObject(resourceUrl, requestUpdate, Void.class);
|
@@ -262,9 +269,44 @@ private RequestCallback requestCallback(final Foo updatedInstance) {
|
262 | 269 |
|
263 | 270 | ClientHttpRequestFactory getClientHttpRequestFactory() {
|
264 | 271 | 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); |
267 | 278 | 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 | + } |
269 | 311 |
|
270 | 312 | }
|
0 commit comments