Having a problem with understanding how RestTemplate handles timeouts.
I configured RestTemplate
as a Bean
as shown:
@Bean
public RestTemplate rest(final RestTemplateBuilder builder) {
final RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
return restTemplate;
}
private ClientHttpRequestFactory getClientHttpRequestFactory() {
final int timeout = 50000;
final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
return new HttpComponentsClientHttpRequestFactory(client);
}
The value of 50000 is only a academic value.
I'm using my RestTemplate
withhin a Hystrix
wrapped Service Connector:
@HystrixCommand(commandProperties = { @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE") }, fallbackMethod = "fallbackActivityCall")
public Optional<ResponseActivityValue> callForActivity() {
final StringBuilder urlBuilder = new StringBuilder(accessConfig.getTracking().getUrl());
final HttpEntity<String> entity = new HttpEntity<>(buildAuthHeader());
final ResponseEntity<ResponseActivityValue> re = restTemplate.exchange(urlBuilder.toString(), HttpMethod.GET, entity, ResponseActivityValue.class);
final HttpStatus code = re.getStatusCode();
return Optional.ofNullable(re.getBody());
}
I tested it with the service to call stopped. Instead of the expected value of 50000, the method returns after 3 seconds. It doesn't matter which value i configure in te RestTemplate
, it always returns after 3 seconds.
Does anyone have an idea?
You can use directly RestTemplateBuilder builder to build you rest Template instance : as below
@Bean
public RestTemplate rest(final RestTemplateBuilder builder) {
return builder
.setConnectTimeout(50000)
.setReadTimeout(50000)
.build()
}
And try setting configuration for hysterix in application.properties
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=50000
or just in your the command directly (@HystrixProperty
)
@HystrixCommand(
commandProperties = {
@HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "50000")
}, fallbackMethod = "fallbackActivityCall")
public Optional<ResponseActivityValue> callForActivity() {
....
}