httpClient4.3开始弃用了DefaultHttpClient和PoolingClientConnectionManager.原来这样配置的连接工厂
<bean id="clientHttpRequestFactory"
class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<constructor-arg>
<bean class="org.apache.http.impl.client.DefaultHttpClient">
<constructor-arg>
<bean class="org.apache.http.impl.conn.PoolingClientConnectionManager">
<property name="maxTotal" value="10" />
<property name="defaultMaxPerRoute" value="5" />
</bean>
</constructor-arg>
</bean>
</constructor-arg>
<property name="connectTimeout" value="30000" />
<property name="readTimeout" value="30000" />
</bean>
现在改用spring推荐的类,配置如下:
<bean id="pollingConnectionManager"
class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
<property name="maxTotal" value="10" />
<property name="defaultMaxPerRoute" value="5" />
</bean>
<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"
factory-method="create">
<property name="connectionManager" ref="pollingConnectionManager" />
</bean>
<bean id="httpClient" factory-bean="httpClientBuilder"
factory-method="build" />
<!-- -->
<bean id="clientHttpRequestFactory"
class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<constructor-arg ref="httpClient" />
<property name="connectTimeout" value="30000" />
<property name="readTimeout" value="30000" />
</bean>
配置带有连接池的org.springframework.web.client.RestTemplate
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="clientHttpRequestFactory" />
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean
class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter" />
</list>
</property>
</bean>
已有 0 人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐