@SimonLogic I use the following code to print timeouts to log. And of course you can modify it to return them back to caller.
@Slf4j
public class RestTemplateInspectionUtil {
public static void inspectTimeouts(RestTemplate restTemplate) {
try {
var requestFactory = restTemplate.getRequestFactory();
if (requestFactory instanceof AbstractClientHttpRequestFactoryWrapper) {
Field requestFactoryField = AbstractClientHttpRequestFactoryWrapper.class.getDeclaredField("requestFactory");
requestFactoryField.setAccessible(true);
requestFactory = (ClientHttpRequestFactory) requestFactoryField.get(requestFactory);
}
if (requestFactory instanceof HttpComponentsClientHttpRequestFactory) {
HttpClient httpClient = ((HttpComponentsClientHttpRequestFactory) requestFactory).getHttpClient();
Method createRequestConfigMethod =
HttpComponentsClientHttpRequestFactory.class.getDeclaredMethod("createRequestConfig", Object.class);
createRequestConfigMethod.setAccessible(true);
RequestConfig requestConfig = (RequestConfig) createRequestConfigMethod.invoke(requestFactory, httpClient);
log.debug("socket (read) timeout: {}, connection timeout: {}, connection request timeout: {}",
requestConfig.getSocketTimeout(), requestConfig.getConnectTimeout(), requestConfig.getConnectionRequestTimeout());
} else if (requestFactory instanceof SimpleClientHttpRequestFactory) {
Field readTimeoutField = requestFactory.getClass().getDeclaredField("readTimeout");
readTimeoutField.setAccessible(true);
Field connectTimeoutField = requestFactory.getClass().getDeclaredField("connectTimeout");
connectTimeoutField.setAccessible(true);
int readTimeout = readTimeoutField.getInt(requestFactory);
int connectTimeout = connectTimeoutField.getInt(requestFactory);
log.debug("read timeout: {}, connection timeout: {}", readTimeout, connectTimeout);
}
} catch (Exception ex) {
log.debug("Failed to check restTemplate timeouts: {}", ex);
}
}
}