[英]Not getting response with Http Async Client
我被這種奇怪的情況困住了,有時我的HTTP請求沒有發出,或者我沒有偶爾收到對請求的HTTP響應。 我的應用程序會定期向其他第三方服務發出幾(100s)個http請求,其中大多數請求都可以正常工作。 我將CloseableHttpAsyncClient(版本4.0)與自定義HttpRequestIntercerptor和HttpResponseInterceptor一起使用。 這些主要是為了調試目的而添加的,其中RequestInterceptor是鏈中的最后一個攔截器,而ResponseInterceptor是第一個。 這個想法是在發送實際請求之前,在最后一個階段記錄每個http請求,並在首次收到它時記錄每個http響應。
我有以下模式來設置異步客戶端:
HttpAsyncClientBuilder asyncClientBuilder = HttpAsyncClientBuilder.create();
asyncClientBuilder.addInterceptorLast(new MyHttpRequestInterceptor());
asyncClientBuilder.addInterceptorFirst(new MyHttpResponseInterceptor());
IOReactorConfig reactorConfig = IOReactorConfig.DEFAULT;
reactorConfig.setConnectTimeout(5 * 60 * 1000); // 5 mins
reactorConfig.setSoTimeout(5 * 60 * 1000); // 5 mins
asyncClientBuilder.setDefaultIOReactorConfig(reactorConfig);
System.setProperty("http.maxConnections", "100");
this.asyncHttpClient = asyncClientBuilder.useSystemProperties().build();
this.asyncHttpClient.start();
要發出請求,我要做:
HttpGet httpGet = new HttpGet("some url");
asyncHttpClient.execute(httpGet, new AsyncHTTPResponseHandler(requestMetadata));
這是我的AsyncHTTPResponseHandler類:
class AsyncHTTPResponseHandler implements FutureCallback<HttpResponse> {
// local copy of the request for reference while processing the response.
private RequestMetadata requestMetadata;
public AsyncHTTPResponseHandler(final RequestMetadata requestMetadata) {
this.setRequestMetadata(requestMetadata);
Thread.currentThread().setUncaughtExceptionHandler(new HttpUncaughtExceptionHandler(requestMetadata));
}
@Override
public void cancelled() {
logger.error("AsyncHTTPResponseHandler#Http request id: {} cancelled",
requestMetadata.getRequestId()));
}
@Override
public void completed(HttpResponse response) {
logger.debug("Received HTTP Response for request id: {}",
requestMetadata.getRequestId());
//handleHttpResponse(requestMetadata, response);
}
@Override
public void failed(Exception e) {
logger.error("AsyncHTTPResponseHandler#Error in Http request id: " + requestMetadata.getRequestId(), e);
}
}
基於此設置,基於攔截器日志,我會看到以下情況:1.我的應用程序http請求觸發了asyncclient HttpRequest,並且獲得了HttpResponse-成功。 2.我的應用程序http請求觸發了一個asyncclient HttpRequest(攔截器記錄了它),但我沒有得到該請求的HttpResponse ---不知道為什么嗎? 3.我的應用程序http請求不會觸發asyncclient HttpRequest(攔截器不會記錄該請求),並且我沒有獲得此請求的HttpResponse ---不知道為什么嗎?
關於如何解決此問題或進一步調試此問題的任何提示或建議?
謝謝!!
因此,我想在這里分享我的發現和解決方案。
我們正在經歷類似於該錯誤的症狀: https : //issues.apache.org/jira/browse/HTTPASYNC-79
如果為“ org.apache.http.impl.nio”包啟用了調試日志記錄,則可以看到交換。 注意:日志將非常詳細。
通過將HttpAsyncClient庫從4.0升級到4.0.2解決了該問題。 我還啟用了套接字和連接超時。 您應該在此在日志文件中看到超時異常。
這是我的HttpAsyncClient實例現在的外觀:
HttpAsyncClientBuilder asyncClientBuilder = HttpAsyncClientBuilder.create();
asyncClientBuilder.addInterceptorLast(new MyHttpRequestInterceptor());
asyncClientBuilder.addInterceptorFirst(new MyHttpResponseInterceptor());
// reactor config
IOReactorConfig reactorConfig = IOReactorConfig.custom()
.setConnectTimeout(TIMEOUT_5_MINS_IN_MILLIS)
.setSoTimeout(TIMEOUT_5_MINS_IN_MILLIS).build();
asyncClientBuilder.setDefaultIOReactorConfig(reactorConfig);
// request config
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(TIMEOUT_5_MINS_IN_MILLIS)
.setConnectionRequestTimeout(TIMEOUT_5_MINS_IN_MILLIS)
.setSocketTimeout(TIMEOUT_5_MINS_IN_MILLIS).build();
asyncClientBuilder.setDefaultRequestConfig(requestConfig);
// connection config
ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setMalformedInputAction(CodingErrorAction.IGNORE)
.setUnmappableInputAction(CodingErrorAction.IGNORE)
.build();
asyncClientBuilder.setDefaultConnectionConfig(connectionConfig);
System.setProperty("http.maxConnections", "100");
System.setProperty("http.conn-manager.timeout", "300000"); // 5 mins
this.asyncHttpClient = asyncClientBuilder.useSystemProperties().build();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.