Category Archives: apache-httpasyncclient

Async response streaming with Apache Async Http client

I am using apache async http client to stream objects from azure storage.

I only need to return the HttpResponse object which has the stream associated. My clients will actually have to read from that stream to store the file locally.

So Apache Async clients use a BasicAsyncResponseConsumer which actually buffers the entire file in local memory before calling the completed callback.

I am trying to create my own implementation of AbstractAsyncResponseConsumer so that I can stream the response body instead of actually storing it first but have been unsuccessful to do so till now.

Here is the bare bones cosumer class for reference ->

public class MyConsumer extends` AbstractAsyncResponseConsumer<HttpResponse> {
@Override
protected void onResponseReceived(HttpResponse response) throws HttpException, IOException {

}

@Override
protected void onContentReceived(ContentDecoder decoder, IOControl ioctrl) throws IOException {

}

@Override
protected void onEntityEnclosed(HttpEntity entity, ContentType contentType) throws IOException {

}

@Override
protected HttpResponse buildResult(HttpContext context) throws Exception {
    return null;
}

@Override
protected void releaseResources() {

}

}

And here is the code to send the request and return the response ->

public void getFile(HttpRequestBase request) {

    MyConsumer myConsumer = new MyConsumer();
    HttpAsyncRequestProducer producer = 
    HttpAsyncMethods.create(request);
    CompletableFuture<HttpResponse> future = new CompletableFuture<>();
    return Future<HttpResponse> responseFuture = 
    httpclient.execute(producer,myConsumer,                                                                                                                   
    new FutureCallback<HttpResponse>() {
      @Override
      public void completed(HttpResponse result) {
     //This is called only when all the response body has been read
     //future.complete(Result)

      }
      @Override                                                                      
      public void failed(Exception ex) {
      }
      @Override
      public void cancelled() {                                                                       
      }
   });

return future;

 }

I will be returning a CompletableFuture of the HttpResponse object to my clients.

They shouldnt be waiting for my http client to read all the response body first in local buffer.

They ideally should start copying directly from the stream provided in the response object.

What should I add inmy implementation of the consumer to get the desired result ?

MultipartFormEntity – Content too long

I'm using Java with HttpAsyncClient, and trying to make a post request to a server using multipart/form. There are two parameters: one is just a string, while the second one is a fie/byte array. When I need to perform requests with large size byte arrays, I get the following exception: org.apache.http.ContentTooLongException: Content length is too long.

Is there any way to overcome this issue? How could I make a large sized request with multipart/form using Java and this entity?

Here is some of my code:

final HttpPost post = new HttpPost(uri);
final HttpEntity entity = MultipartEntityBuilder.create()
                .addTextBody("name", fileName).addBinaryBody("file", rawContent, ContentType.APPLICATION_OCTET_STREAM, fileName)
                .build();
post.setEntity(entity);
return client.execute(post, null);

Where rawContent is just a byte array.

Apache ConnectionClosedException: Connection closed,HttpAsyncRequestExecutor.endOfInput(HttpAsyncRequestExecutor.java:344)

we have three applications A,B and C deployed on same tomcat server.There is an HTTP call that happens between A to B(REST CALL) and Another http call from B to C(REST CALL)

We were using synchronous HTTP call initially,recently we changed our code to Asynchronous call. We have used HttpAsyncClients for (Application A to call Application B) and for (Application B to call Application C)..

we are getting intermittent org.apache.http.ConnectionClosedException: Some time we are getting this exception in Logs of application A(Async Client to B) and some time in Application B(Async Client to C)

 CloseableHttpAsyncClient  client = HttpAsyncClients.custom()
                    .setMaxConnPerRoute(100))
                    .setMaxConnTotal(config.getInt(150)).build();

and for Every request we gave timeout of

final RequestConfig params = RequestConfig.custom().setConnectTimeout(300000) // 5min
                    .setSocketTimeout(300000).setConnectionRequestTimeout(300000).build();

Please find the stack trace below:

   org.apache.http.ConnectionClosedException: Connection closed
        at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.endOfInput(HttpAsyncRequestExecutor.java:344) [httpcore-nio-4.4.5.jar:4.4.5]
        at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:261) [httpcore-nio-4.4.5.jar:na]
        at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81) [httpasyncclient-4.1.2.jar:na]
        at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39) [httpasyncclient-4.1.2.jar:na]
        at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114) [httpcore-nio-4.4.5.jar:4.4.5]
        at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162) [httpcore-nio-4.4.5.jar:4.4.5]
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337) [httpcore-nio-4.4.5.jar:4.4.5]
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315) [httpcore-nio-4.4.5.jar:4.4.5]
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276) [httpcore-nio-4.4.5.jar:4.4.5]
        at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104) [httpcore-nio-4.4.5.jar:4.4.5]
        at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588) [httpcore-nio-4.4.5.jar:4.4.5]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_60]

Please Note this question is not duplicate of Intermittent ConnectionClosedException with httpasynclient stacj trace is completely different..And event this issue happens intermittent.

Intermittent ConnectionClosedException with httpasynclient

we have three applications A,B and C deployed on same tomcat server.There is an HTTP call that happens between A to B(REST CALL) and Another http call from B to C(REST CALL)

We were using synchronous HTTP call initially,recently we changed our code to Asynchronous call. We have used HttpAsyncClients for (Application A to call Application B) and for (Application B to call Application C)..

we are getting intermittent org.apache.http.ConnectionClosedException: Some time we are getting this exception in Logs of application A(Async Client to B) and some time in Application B(Async Client to C)

 CloseableHttpAsyncClient  client = HttpAsyncClients.custom()
                    .setMaxConnPerRoute(100))
                    .setMaxConnTotal(config.getInt(150)).build();

and for Every request we gave timeout of

final RequestConfig params = RequestConfig.custom().setConnectTimeout(300000) // 5min
                    .setSocketTimeout(300000).setConnectionRequestTimeout(300000).build();

Please find the stack trace below:

org.apache.http.ConnectionClosedException: Connection closed
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.connectionAllocated(AbstractClientExchangeHandler.java:320) [httpasyncclient-4.1.2.jar:na]
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.access$000(AbstractClientExchangeHandler.java:62) [httpasyncclient-4.1.2.jar:na]
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler$1.completed(AbstractClientExchangeHandler.java:373) [httpasyncclient-4.1.2.jar:na]
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler$1.completed(AbstractClientExchangeHandler.java:369) [httpasyncclient-4.1.2.jar:na]
        at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119) [httpcore-4.4.4.jar:na]
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalPoolEntryCallback.completed(PoolingNHttpClientConnectionManager.java:494) [httpasyncclient-4.1.2.jar:4.1.2]
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalPoolEntryCallback.completed(PoolingNHttpClientConnectionManager.java:477) [httpasyncclient-4.1.2.jar:4.1.2]
        at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119) [httpcore-4.4.4.jar:na]
        at org.apache.http.nio.pool.AbstractNIOConnPool.fireCallbacks(AbstractNIOConnPool.java:456) [httpcore-nio-4.4.5.jar:na]
        at org.apache.http.nio.pool.AbstractNIOConnPool.lease(AbstractNIOConnPool.java:286) [httpcore-nio-4.4.5.jar:na]
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.requestConnection(PoolingNHttpClientConnectionManager.java:266) [httpasyncclient-4.1.2.jar:4.1.2]
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.requestConnection(AbstractClientExchangeHandler.java:363) [httpasyncclient-4.1.2.jar:na]

Java / Apache Async Http Client

I would like to do an asynchronous http request in Java. I embedded all the libraries from the HttpAsyncClient 4.1.2 (GA) here: https://hc.apache.org/downloads.cgi.

Here is the code:

        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    try {

        httpclient.start();

        String status = new JSONObject()
                .put("test","test")
                .toString();

        HttpPost request = new HttpPost("my url");
        request.setEntity(new StringEntity(status));

        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");

        httpclient.execute(request, new FutureCallback<HttpResponse>() {

            public void cancelled() {
                logString("Cancelled");
            }

            public void completed(HttpResponse arg0) {
                logString("Completed");
            }

            public void failed(Exception arg0) {
                logString("Failed");
            }
        });

    } finally {
        try {
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

I also embedded the java json library.

Everytime it throws the following exception.

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
...
Caused by: java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.nio.codecs.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:53)
at org.apache.http.impl.nio.codecs.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:57)
at org.apache.http.impl.nio.codecs.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:47)...

Am I missing something?

How does an HTTP Connection Manager deal with broken connections in its connection pool?

I am getting java.io.IOException: Broken pipe on one of the connection while using Apache PollingNHttpClientConnectionManager. How does connection management work? Can I be sure that this connection will be resurrected (if broken) by connection manager and that it won't be given (as broken) to service any further requests?

org.apache.http.nio.reactor.IOReactorException: I/O dispatch worker terminated abnormally

I have a service which uses apache HttpAsyncClient. (versions: httpasyncclient-4.0.2.jar, httpcore-4.4.3.jar, httpcore-nio-4.3.3.jar)

All requests start failing some time after starting the async client with following being initial exception -

[#|2016-03-16T22:31:59.376-0700|SEVERE|glassfish3.1.2|org.apache.http.impl.nio.client.InternalHttpAsyncClient|_ThreadID=564;_ThreadName=Thread-6;|I/O reactor terminated abnormally org.apache.http.nio.reactor.IOReactorException: I/O dispatch worker terminated abnormally at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:357) at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:189) at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.doExecute(CloseableHttpAsyncClientBase.java:67) at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.access$000(CloseableHttpAsyncClientBase.java:38) at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:57) at java.lang.Thread.run(Unknown Source) Caused by: RestException(statusCode=500, code=null, message=I/O operation failed, developerMessage=RestException(statusCode=500, code=null, message=I/O operation failed, developerMessage=null) at com.notificationservice.analytics.client.AsyncResponse$2.failed(AsyncResponse.java:178) at org.apache.http.concurrent.BasicFuture.failed(BasicFuture.java:134) at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.failed(DefaultClientExchangeHandlerImpl.java:258) at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.exception(HttpAsyncRequestExecutor.java:127) at org.apache.http.impl.nio.client.InternalIODispatch.onException(InternalIODispatch.java:68) at org.apache.http.impl.nio.client.InternalIODispatch.onException(InternalIODispatch.java:37) at org.apache.http.impl.nio.reactor.AbstractIODispatch.outputReady(AbstractIODispatch.java:154) at org.apache.http.impl.nio.reactor.BaseIOReactor.writable(BaseIOReactor.java:180) at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:342) at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:316) at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:277) at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105) at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:586) at java.lang.Thread.run(Unknown Source) ) at com.notificationservice.client.AsyncResponse$2.failed(AsyncResponse.java:178) at org.apache.http.concurrent.BasicFuture.failed(BasicFuture.java:134) at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.failed(DefaultClientExchangeHandlerImpl.java:258) at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.exception(HttpAsyncRequestExecutor.java:127) at org.apache.http.impl.nio.client.InternalIODispatch.onException(InternalIODispatch.java:68) at org.apache.http.impl.nio.client.InternalIODispatch.onException(InternalIODispatch.java:37) at org.apache.http.impl.nio.reactor.AbstractIODispatch.outputReady(AbstractIODispatch.java:154) at org.apache.http.impl.nio.reactor.BaseIOReactor.writable(BaseIOReactor.java:180) at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:342) at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:316) at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:277) at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105) at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:586) ... 1 more

Same problem happens with new versions - httpasyncclient-4.1.1.jar, httpcore-4.4.4.jar, httpcore-nio-4.4.4.jar

Any insight would be highly appreciated. Is there some IOReactorConfig parameter which needs to be changed?

java.lang.IllegalStateException: Request cannot be executed; I/O reactor status: STOPPED

I have a service that is expected to execute requests at ~5 or more requests/min. This service depends on Apache AsyncHttpClient. After every few minutes, the clients hits some condition which causes java.lang.IllegalStateException: Request cannot be executed; I/O reactor status: STOPPED. All requests to the client start failing with same exception message. After service is restarted, this cycle repeats.

It is really hard to debug this problem as the request execution failure surprisingly does not cause a callback to the failed() method of the AsyncResponse.

From what I could gather, there has been a fix HTTPCORE-370 in HttpCore NIO which solved a similar problem in 4.3.2. I am using the following version - commons-httpclient-3.1.jar httpasyncclient-4.1.1.jar httpcore-4.4.4.jar httpcore-nio-4.4.4.jar

Yet seeing this problem. Any help would be really appreciated.