【发布时间】:2015-08-19 00:18:00
【问题描述】:
我已经配置了一个这样的 AsyncRestTemplate,这里只是一个说明我正在使用一个 HttpComponentsAsyncClientHttpRequestFactory 并初始化 connectTimeout 和 readTimeout带值 - 使用 Spring 4.0.8 RELEASE:
<bean id="myAsynchRequestFactory" class="org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory">
<property name="httpAsyncClient" ref="myCloseableHttpAsynchClient"></property>
<property name="connectTimeout" value="444"></property>
<property name="readTimeout" value="555"></property>
</bean>
<bean id="myAsynchRestTemplate" class="org.springframework.web.client.AsyncRestTemplate">
<constructor-arg>
<ref bean="myAsynchRequestFactory"/>
</constructor-arg>
</bean>
我还配置了一个RequestConfig,因为spring代码建议不推荐使用前一种方法,所以我这样添加:
<bean id="myRequestConfigBuilder" class="org.apache.http.client.config.RequestConfig" factory-method="custom">
<property name="connectionRequestTimeout" value="555"></property>
<property name="connectTimeout" value="400"></property>
<property name="socketTimeout" value="555"></property>
</bean>
<bean id="myRequestConfig" factory-bean="myRequestConfigBuilder" factory-method="build">
</bean>
<bean id="myHttpAsyncClientBuilder" class="org.apache.http.impl.nio.client.HttpAsyncClientBuilder">
<property name="connectionManager" ref="myHttpAsyncConnectionManager"></property>
<property name="defaultRequestConfig" ref="myRequestConfig"></property>
</bean>
<bean id="myHttpAsyncClientBuilder" class="org.apache.http.impl.nio.client.HttpAsyncClientBuilder">
<property name="connectionManager" ref="myHttpAsyncConnectionManager"></property>
<property name="defaultRequestConfig" ref="myRequestConfig"></property>
</bean>
<bean id="myCloseableHttpAsynchClient" factory-bean="myHttpAsyncClientBuilder" factory-method="build">
</bean>
现在我正在使用 AsyncRestTemplate 及其 addCallback() 方法,如下所示(如图所示):
response = myAsynchRestTemplate.getForEntity( ... );
response.addCallback(new org.springframework.util.concurrent.ListenableFutureCallback<ResponseEntity<?>>() {
@Override
public void onSuccess(ResponseEntity<?> result) {
System.out.println("SUCCESS");
}
@Override
public void onFailure(Throwable ex) {
System.out.println("FAILURE")
}
}
我希望当服务的回复时间超过 555 毫秒时,永远不会调用 onSuccess(...)。但这不会发生。即使我的服务假装了 5000 毫秒的延迟,无论如何都会调用 onSuccess 方法。
我已经在网上搜索了可能的解决方案来为回调添加某种超时,但没有找到任何合适的解决方案。我尝试阅读自 4.0 以来任何版本中的 org.springframework.util.concurrent.* 代码,但尚未找到任何会阻止注册回调执行的内容。
我已经看过以下 Stackoverflow 问题:
How to cancel AsyncRestTemplate HTTP request if they are taking too much time?这个建议超时应该可以工作
ListenableFuture, FutureCallback and timeouts这个是使用一些谷歌番石榴示例,但我使用的是org.springframework.util.concurrent.ListenableFuture
有人有解决这个问题的办法吗?
更新
我找到了为什么这不起作用的根本原因......
HttpComponentsAsyncClientHttpRequestFactory 不采用我提供的 RequestConfig。相反,它始终为空,并且重新创建为 RequestConfig.DEFAULT。这发生在这里:
来自 HttpComponentesAsyncClientHttpRequestFactory:
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpAsyncClient asyncClient = getHttpAsyncClient();
startAsyncClient();
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
/** HERE: It tries to create a HttpContext **/
HttpContext context = createHttpContext(httpMethod, uri);
if (context == null) {
context = HttpClientContext.create();
}
// Request configuration not set in the context
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
// Use request configuration given by the user, when available
RequestConfig config = null;
if (httpRequest instanceof Configurable) {
config = ((Configurable) httpRequest).getConfig();
}
if (config == null) {
config = RequestConfig.DEFAULT;
}
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
return new HttpComponentsAsyncClientHttpRequest(asyncClient, httpRequest, context);
}
但是方法createHttpContext(httpMethod, uri)总是返回null:
/**
* Template methods that creates a {@link HttpContext} for the given HTTP method and URI.
* <p>The default implementation returns {@code null}.
* @param httpMethod the HTTP method
* @param uri the URI
* @return the http context
*/
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
return null;
}
因此,HttpContext 被重新创建并附加了一个 RequestConfig.DEFAULT。
现在比较这个与非异步版本,HttpComponentsClientHttpRequestFactory。 这个重新创建了一个带有超时的 RequestConfig:
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
CloseableHttpClient client = (CloseableHttpClient) getHttpClient();
Assert.state(client != null, "Synchronous execution requires an HttpClient to be set");
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
if (context == null) {
context = HttpClientContext.create();
}
// Request configuration not set in the context
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
// Use request configuration given by the user, when available
RequestConfig config = null;
if (httpRequest instanceof Configurable) {
config = ((Configurable) httpRequest).getConfig();
}
if (config == null) {
/** LOOK HERE - THE SYNC WORLD HAS THIS WORKAROUND */
if (this.socketTimeout > 0 || this.connectTimeout > 0) {
config = RequestConfig.custom()
.setConnectTimeout(this.connectTimeout)
.setSocketTimeout(this.socketTimeout)
.build();
}
else {
config = RequestConfig.DEFAULT;
}
}
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
if (this.bufferRequestBody) {
return new HttpComponentsClientHttpRequest(client, httpRequest, context);
}
else {
return new HttpComponentsStreamingClientHttpRequest(client, httpRequest, context);
}
}
当我使用 SimpleClientHttpRequestFactory 时,我配置的超时将被采用,而不是 onSuccess(...),onFailure(...) 方法使用 SocketTimeoutException
调用这完全令人费解-也许任何人都可以提示为什么 HttpComponentsAsyncClientHttpRequestFactory 是按原样实现的?
有没有正确使用 RequestConfig 对象的例子?
【问题讨论】:
标签: java spring asynchronous