See below , How im using the Client. I believe it's Auto Closable.
I don't think i have an option to close the connection.
PoolingNHttpClientConnectionManager will manage all connections right?
CloseableHttpAsyncClient client =
CustomHttpClientBuilder.buildAsyncHttpClient();
private void executeRequest(final HttpPost postReq){
try{
client.execute(postReq, new FutureCallback<HttpResponse>() {
public void completed(final HttpResponse response) {
try {
int statusCode =
response.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK){
LOG.debug("Endpoint : {} responded non 2XX
response , code : {} , Request : {} ",postReq.getURI(), statusCode,
HttpUtils.getIncomingRequestEntity(postReq.getEntity()));
}
} catch (Exception e) {
LOG.error("error while consuming resp ", e);
}finally{
HttpClientUtils.closeQuietly(response);
}
}
public void failed(final Exception ex) {
if(ex instanceof java.util.concurrent.TimeoutException){
LOG.debug("(TimeoutException) Failed to post events
to destination EndPoint : {} , Request : {}, Error Msg : {} ",
postReq.getURI(), HttpUtils.getIncomingRequestEntity(postReq.getEntity()),
ex);
}else if( ex instanceof UnknownHostException){
LOG.error("UnknownHostException, Connector Key : {} ,
EndPoint: {} ", connectorKey, postReq.getURI());
}else if( ex instanceof ConnectException){
LOG.error("ConnectException, Connector Key : {} ,
EndPoint: {} ", connectorKey, postReq.getURI());
}else if( ex instanceof SocketTimeoutException){
LOG.error("SocketTimeoutException, Connector Key : {}
, EndPoint: {} ", connectorKey, postReq.getURI());
}else{
LOG.error("(Other Exception) Failed to post log to
destination EndPoint : {} , Connector Key : {}, Error Msg : {}",
postReq.getURI(), connectorKey, ex.getMessage());
}
}
public void cancelled() {
LOG.error("task cancelled by http client, Retry after
few seconds , Connector Key : {} , EndPoint: {} ", connectorKey,
postReq.getURI());
}
});
}catch (Exception e) {
LOG.error(" Error while executing POST request : {} , EndPoint
: {} ", HttpUtils.getIncomingRequestEntity(postReq.getEntity()),
postReq.getURI(), e);
}finally{
}
}
On Fri, Aug 28, 2020 at 2:44 PM 樊超 <[email protected]> wrote:
> English is not my native language, please excuse typing errors.
> I guess your problem is that you forget close the connection when
> exception happened.
> If so, you should put the close method in the finally block.
> If connection doesn’t released, the connection pool will be fill up.
>
> 发件人: SenthilKumar K
> 发送时间: 2020年8月28日 16:56
> 收件人: [email protected]
> 抄送: Senthil kumar
> 主题: Task Cancelled by HttpClient - 4.1.4
>
> Hello Experts, We have been running Apache Async Http Client in Product to
> Post Events to Splunk & Datadog Third-Party Systems. Most of the times it
> works well but when there was some Connection Closed Exception happens for
> a particular endpoint, Client is continuously getting task cancelled error
> which is resulting in the data loss from our side.
>
>
> Context:
> We run the AsyncClient in Container, Configuration of Container: 2 CPUs, 7
> GB memory & 1GB disk.
>
> The moment AsyncHttpClient receives " Failed to post log to destination
> EndPoint : https://abcde.com/v1/input/jskjdhk , *Error Msg: Connection
> closed " *the all subsequent calls getting task cancelled by http client.
>
> What's wrong with the client configuration? Am i missing something?
>
> Client Settings:
>
> public static final RequestConfig.Builder requestConfigBuilder =
> RequestConfig.custom()
> .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS) // 10
> seconds
> .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT_MILLIS) // 10 seconds
>
> .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS); //
> 10 seconds
>
> public static final ConnectionConfig connectionConfig =
> ConnectionConfig.custom()
> .setBufferSize(DEFAULT_BUFFER_SIZE)
> .setMessageConstraints(messageConstraints)
> .setFragmentSizeHint(DEFAULT_BUFFER_SIZE)
> .build();
>
> final PoolingNHttpClientConnectionManager mgr = new
> PoolingNHttpClientConnectionManager(ioreactor, registry);
> mgr.setDefaultConnectionConfig(connectionConfig);
> mgr.setMaxTotal(DEFAULT_MAX_CONN_TOTAL); // 500
> mgr.setDefaultMaxPerRoute(DEFAULT_MAX_CONN_PER_ROUTE); // 25
> mgr.closeExpiredConnections();
>
> final HttpAsyncClientBuilder httpClientBuilder =
> HttpAsyncClientBuilder.create()
>
> .setDefaultRequestConfig(requestConfigBuilder.build())
> .setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE)
> //25
> .setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL)//500
> .setDefaultConnectionConfig(connectionConfig)
> .setConnectionManager(mgr)
> .setSSLContext(getSSLContext())
> .setSSLHostnameVerifier(hostNameVerifier)
> .disableConnectionState()
> .disableAuthCaching()
> .disableCookieManagement()
> .useSystemProperties();
> CloseableHttpAsyncClient httpClient = httpClientBuilder.build();
> httpClient.start();
>
>
> <dependency>
> <groupId>org.apache.httpcomponents</groupId>
> <artifactId>*httpasyncclient*</artifactId>
> <version>4.1.4</version>
> </dependency>
>
> --Senthil
>
>
>