I'd like to have Spring IoC configure a CloseableHttpClient
object and inject it into my class so that customization of its configuration can be done via XML.
From what I can see, HttpClient
seems to resist this pattern quite forcibly. They want you to do things like
CloseableHttpClient chc =
HttpClients.custom().set<thing that should be a property>().build();
Ick.
Is there not some mechanism for making a singleton CloseableHttpClient
bean that I can then use?
This seems to work for me:
<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig"
factory-method="custom">
<property name="socketTimeout" value="${socketTimeoutInMillis}" />
<property name="connectTimeout" value="${connectionTimeoutInMillis}" />
</bean>
<bean id="requestConfig" factory-bean="requestConfigBuilder" factory-method="build" />
<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"
factory-method="create">
<property name="defaultRequestConfig" ref="requestConfig" />
</bean>
<bean id="httpClient" factory-bean="httpClientBuilder" factory-method="build" />
That gives me a CloseableHttpClient in the "httpClient" bean, with the socket and connection timeouts configured. You should be able to add more properties to either the requestConfigBuilder or the httpClientBuilder.