简体   繁体   中英

Retrieve timeout values from HttpComponentsClientHttpRequestFactory using spring ReflectionUtils

I am using org.springframework.http.client.HttpComponentsClientHttpRequestFactory in my code and I want to use Reflection (org.springframework.util.ReflectionUtils) to retrieve some timeout values like ConnectTimeout from this object. I am not very clear on how to retrieve this field from this object.

I tried something like: ReflectionUtils.findField(httpComponentsClientHttpRequestFactory.getClass(), "connectTimeout");

But, the value is returned as null.

The timeout values are part of the RequestConfig field of HttpComponentsClientHttpRequestFactory. Following is the code to access the same.

HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(1000);

Field requestConfigField = ReflectionUtils.findField(
   HttpComponentsClientHttpRequestFactory.class, "requestConfig", RequestConfig.class);
requestConfigField.setAccessible(true);
RequestConfig requestConfig = (RequestConfig) ReflectionUtils.getField(requestConfigField, factory);

System.out.println("Connect timeoout = " + requestConfig.getConnectTimeout());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM