How to - Spring IoC and HttpClient 4.3.1 CloseableHttpClient?

一曲冷凌霜 提交于 2019-11-28 18:05:51

问题


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?


回答1:


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.




回答2:


With Java config, this is as simple as

@Bean
public CloseableHttpClient httpClient() {
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setEverything(everything); // configure it
    CloseableHttpClient httpClient = builder.build();
}

With XML config, it's a little more complex. You can create your own FactoryBean implementation, say CloseableHttpClientFactoryBean, which delegates all the calls to a HttpClientBuilder and calls build() inside getObject().

public class CloseableHttpClientFactoryBean implements FactoryBean<CloseableHttpClient> {
    private HttpClientBuilder builder;
    public CloseableHttpClientFactoryBean() {
        builder = HttpClientBuilder.create();
    }

    ... // all the setters
    // for example
    public void setEverything(Everything everything) {
        // delegate
        builder.setEverything(everything);
    }

    public CloseableHttpClient getObject() {
        return builder.build();
    }
}

And the config

<bean name="httpClient" class="com.spring.http.clients.CloseableHttpClientFactoryBean">
    <property name="everything" ref="everything"/>
</bean>

You will need a setter method for each HttpClientBuilder method.

Note that if you don't need any custom configuration, you can use factory-method to get a default CloseableHttpClient

<bean name="httpClient" class="org.apache.http.impl.client.HttpClients" factory-method="createDefault" >
</bean>


来源:https://stackoverflow.com/questions/20340470/how-to-spring-ioc-and-httpclient-4-3-1-closeablehttpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!