繁体   English   中英

Http Async Client无法获得响应

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM