在升级到Spring 4时,我们必须更改http调用程序的上下文配置,以匹配http-client 4.4.9和http-core 4.5.5,如下所示:
<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig" factory-method="custom">
<property name="socketTimeout" value="${connection.timeout}" />
<property name="connectTimeout" value="${connection.timeout}" />
<property name="staleConnectionCheckEnabled" value="${connection.doStaleCheck}"/>
<property name="connectionRequestTimeout" value="${connection.poolTimeout}" />
</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" />
<property name="maxConnTotal" value="${connection.maxActive}" />
<property name="maxConnPerRoute" value="${connection.maxActive}" />
</bean>
<bean id="httpClientBld" factory-bean="httpClientBuilder" factorymethod="build" />但是,在测试时,套接字超时和连接超时似乎不能正常工作.它总是等待大约1000 ms的连接超时和60000的套接字超时,而不是配置的值。
发布于 2018-05-31 12:49:43
在深入研究这个问题之后,结果发现它是Spring 4中的一个问题,而不是来自http客户端jar,并且只在上下文配置XML文件传递超时值时发生。作为一种解决方案,我们通过语法方式设置超时值。
CloseableHttpClient httpClient = (CloseableHttpClient) context.getBean("httpClientBld");
HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(httpClient);
HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
httpInvokerProxyFactoryBean.setServiceInterface(InterfaceClass.class);
httpInvokerProxyFactoryBean.setServiceUrl(endpoint);
httpInvokerProxyFactoryBean.setHttpInvokerRequestExecutor(executor);
httpInvokerProxyFactoryBean.afterPropertiesSet();
this.cainServer = (InterfaceClass) httpInvokerProxyFactoryBean.getObject();在我们刚才使用的上下文中:
其中的值是从Java代码传递的。注意,还有一个问题仍然存在,那就是连接超时仍然是100 ms,而不是配置的。但是这个问题来自http客户端jar,而不是Spring 4。
https://stackoverflow.com/questions/50133059
复制相似问题