You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Petteri Hentilä <pe...@gmail.com> on 2013/11/22 09:55:53 UTC

Apache CloseableHttpAsyncClient - way to access HttpContext in a callback method?

Hello,

I'm creating my own asynchronous HTTP client with for performance testing
purposes. I am thinking of using Apache's CloseableHttpAsyncClient for this.

Here is an example how to use this client:
http://hc.apache.org/httpcomponents-asyncclient-4.0.x/quickstart.html

With CloseableHttpAsyncClient there are three callback methods completed(),
failed() and cancelled(). The completed() -method gets the HttpResponse as
a parameter and the failed() -method gets the Exception as the parameter.

However, I would also need an access to the HttpContext object in all of
these callbacks methods. HttpContext represents execution state of an HTTP
process. You can for example store any user related objects to HttpContext.

Basically what I need is to have an access to HttpContext at least in the
following three different situations: 1. When the Http response is fully
received 2. When an exception occurs (for example the target host is down)
3. When a connection timeout occurs

Does anyone know a way to achieve this?

Re: Apache CloseableHttpAsyncClient - way to access HttpContext in a callback method?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Fri, 2013-11-22 at 15:49 +0200, Petteri Hentilä wrote:
> Yes, creating an separate request object for each request fixed the
> problem. Now it seems to work OK. I have yet to test timeouts. I suppose
> either the failed() or cancelled() method is called in a case of connection
> timeout.
> 

#failed()

Oleg

> 
> On Fri, Nov 22, 2013 at 3:37 PM, Oleg Kalnichevski <ol...@apache.org> wrote:
> 
> > On Fri, 2013-11-22 at 15:10 +0200, Petteri Hentilä wrote:
> > > Thanks again.
> > >
> > > I made my own implementation of the FutureCallback interface:
> > >
> > > private static class FutureCallbackImpl implements
> > > FutureCallback<HttpResponse> {
> > >
> > >         private HttpCoreContext coreContext;
> > >         public FutureCallbackImpl(HttpCoreContext coreContext) {
> > >             this.coreContext = coreContext;
> > >         }
> > >
> > >         @Override
> > >         public void cancelled() {
> > >
> > >         }
> > >
> > >         @Override
> > >         public void completed(HttpResponse response) {
> > >             System.out.println("completed" + "->" +
> > > response.getStatusLine();
> > >
> > >         }
> > >
> > >         @Override
> > >         public void failed(Exception cause) {
> > >             System.out.println("failed" + "->" + cause);
> > >         }
> > >     }
> > >
> > > And here is how I use it.
> > >
> > > HttpAsyncRequester requester = new HttpAsyncRequester(httpproc);
> > >
> > >         final HttpHost target = new HttpHost("www.google.fi", 80,
> > "http");
> > >         BasicHttpRequest request = new BasicHttpRequest("GET", "/");
> > >
> > >         for(int i=0; i<50; i++) {
> > >             HttpCoreContext coreContext = HttpCoreContext.create();
> > >             coreContext.setAttribute("index", i);
> > >             FutureCallbackImpl callBack = new
> > > FutureCallbackImpl(coreContext);
> > >
> > >             requester.execute(
> > >                 new BasicAsyncRequestProducer(target, request),
> > >                 new BasicAsyncResponseConsumer(),
> > >                 pool,
> > >                 coreContext,
> > >                 // Handle HTTP response from a callback
> > >                 callBack
> > >             );
> > >
> > >         }
> > >
> > > What I do here is to send 50 requests inside one loop.
> > >
> > > However, each time I run the program the failed() callback method is
> > called
> > > a several times with the exception being
> > > java.util.ConcurrentModificationException.
> > >
> > > There seems to be a concurrency problem somewhere?
> > >
> >
> > Oh, where could only that be? Let me look at the stack trace. Hmm. No
> > stack trace. Let me try mind reading.
> >
> > What do you think should happen when you attempt to execute the _same_
> > request object 50 times using multiple CPU cores?
> >
> > Oleg
> >
> > >
> > >
> > > On Fri, Nov 22, 2013 at 2:37 PM, Oleg Kalnichevski <ol...@apache.org>
> > wrote:
> > >
> > > > On Fri, 2013-11-22 at 14:27 +0200, Petteri Hentilä wrote:
> > > > > A couple of comments.
> > > > >
> > > > > 1. I cannot pass the HttpContext to FutureCallback as a construction
> > > > > parameter, because that class has only one parameterless
> > constructor. I
> > > > > also cannot put the HttpContext as the generic type for
> > FutureCallback
> > > > > because it gives me a compile error.
> > > > >
> > > >
> > > > FutureCallback is an _interface_. One can have any constructor
> > > > parameters for a class that implements this interface.
> > > >
> > > > > 2. I derived my own class from BasicAsyncResponseConsumer to have
> > more
> > > > > control over the response processing, but that class does not have
> > the
> > > > > correct methods that I would need. It has a method
> > > > > responseReceived(HttpResponse) but it does not get HttpContext as a
> > > > > parameter. It also has a method failed(Exception) but it does not get
> > > > > HttpContext as a parameter either.
> > > > >
> > > >
> > > > Same deal as with FutureCallback. You can pass any additional
> > contextual
> > > > parameters at the construction time.
> > > >
> > > > Oleg
> > > >
> > > >
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > > > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > > >
> > > >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Apache CloseableHttpAsyncClient - way to access HttpContext in a callback method?

Posted by Petteri Hentilä <pe...@gmail.com>.
Yes, creating an separate request object for each request fixed the
problem. Now it seems to work OK. I have yet to test timeouts. I suppose
either the failed() or cancelled() method is called in a case of connection
timeout.


On Fri, Nov 22, 2013 at 3:37 PM, Oleg Kalnichevski <ol...@apache.org> wrote:

> On Fri, 2013-11-22 at 15:10 +0200, Petteri Hentilä wrote:
> > Thanks again.
> >
> > I made my own implementation of the FutureCallback interface:
> >
> > private static class FutureCallbackImpl implements
> > FutureCallback<HttpResponse> {
> >
> >         private HttpCoreContext coreContext;
> >         public FutureCallbackImpl(HttpCoreContext coreContext) {
> >             this.coreContext = coreContext;
> >         }
> >
> >         @Override
> >         public void cancelled() {
> >
> >         }
> >
> >         @Override
> >         public void completed(HttpResponse response) {
> >             System.out.println("completed" + "->" +
> > response.getStatusLine();
> >
> >         }
> >
> >         @Override
> >         public void failed(Exception cause) {
> >             System.out.println("failed" + "->" + cause);
> >         }
> >     }
> >
> > And here is how I use it.
> >
> > HttpAsyncRequester requester = new HttpAsyncRequester(httpproc);
> >
> >         final HttpHost target = new HttpHost("www.google.fi", 80,
> "http");
> >         BasicHttpRequest request = new BasicHttpRequest("GET", "/");
> >
> >         for(int i=0; i<50; i++) {
> >             HttpCoreContext coreContext = HttpCoreContext.create();
> >             coreContext.setAttribute("index", i);
> >             FutureCallbackImpl callBack = new
> > FutureCallbackImpl(coreContext);
> >
> >             requester.execute(
> >                 new BasicAsyncRequestProducer(target, request),
> >                 new BasicAsyncResponseConsumer(),
> >                 pool,
> >                 coreContext,
> >                 // Handle HTTP response from a callback
> >                 callBack
> >             );
> >
> >         }
> >
> > What I do here is to send 50 requests inside one loop.
> >
> > However, each time I run the program the failed() callback method is
> called
> > a several times with the exception being
> > java.util.ConcurrentModificationException.
> >
> > There seems to be a concurrency problem somewhere?
> >
>
> Oh, where could only that be? Let me look at the stack trace. Hmm. No
> stack trace. Let me try mind reading.
>
> What do you think should happen when you attempt to execute the _same_
> request object 50 times using multiple CPU cores?
>
> Oleg
>
> >
> >
> > On Fri, Nov 22, 2013 at 2:37 PM, Oleg Kalnichevski <ol...@apache.org>
> wrote:
> >
> > > On Fri, 2013-11-22 at 14:27 +0200, Petteri Hentilä wrote:
> > > > A couple of comments.
> > > >
> > > > 1. I cannot pass the HttpContext to FutureCallback as a construction
> > > > parameter, because that class has only one parameterless
> constructor. I
> > > > also cannot put the HttpContext as the generic type for
> FutureCallback
> > > > because it gives me a compile error.
> > > >
> > >
> > > FutureCallback is an _interface_. One can have any constructor
> > > parameters for a class that implements this interface.
> > >
> > > > 2. I derived my own class from BasicAsyncResponseConsumer to have
> more
> > > > control over the response processing, but that class does not have
> the
> > > > correct methods that I would need. It has a method
> > > > responseReceived(HttpResponse) but it does not get HttpContext as a
> > > > parameter. It also has a method failed(Exception) but it does not get
> > > > HttpContext as a parameter either.
> > > >
> > >
> > > Same deal as with FutureCallback. You can pass any additional
> contextual
> > > parameters at the construction time.
> > >
> > > Oleg
> > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > >
> > >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: Apache CloseableHttpAsyncClient - way to access HttpContext in a callback method?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Fri, 2013-11-22 at 15:10 +0200, Petteri Hentilä wrote:
> Thanks again.
> 
> I made my own implementation of the FutureCallback interface:
> 
> private static class FutureCallbackImpl implements
> FutureCallback<HttpResponse> {
> 
>         private HttpCoreContext coreContext;
>         public FutureCallbackImpl(HttpCoreContext coreContext) {
>             this.coreContext = coreContext;
>         }
> 
>         @Override
>         public void cancelled() {
> 
>         }
> 
>         @Override
>         public void completed(HttpResponse response) {
>             System.out.println("completed" + "->" +
> response.getStatusLine();
> 
>         }
> 
>         @Override
>         public void failed(Exception cause) {
>             System.out.println("failed" + "->" + cause);
>         }
>     }
> 
> And here is how I use it.
> 
> HttpAsyncRequester requester = new HttpAsyncRequester(httpproc);
> 
>         final HttpHost target = new HttpHost("www.google.fi", 80, "http");
>         BasicHttpRequest request = new BasicHttpRequest("GET", "/");
> 
>         for(int i=0; i<50; i++) {
>             HttpCoreContext coreContext = HttpCoreContext.create();
>             coreContext.setAttribute("index", i);
>             FutureCallbackImpl callBack = new
> FutureCallbackImpl(coreContext);
> 
>             requester.execute(
>                 new BasicAsyncRequestProducer(target, request),
>                 new BasicAsyncResponseConsumer(),
>                 pool,
>                 coreContext,
>                 // Handle HTTP response from a callback
>                 callBack
>             );
> 
>         }
> 
> What I do here is to send 50 requests inside one loop.
> 
> However, each time I run the program the failed() callback method is called
> a several times with the exception being
> java.util.ConcurrentModificationException.
> 
> There seems to be a concurrency problem somewhere?
> 

Oh, where could only that be? Let me look at the stack trace. Hmm. No
stack trace. Let me try mind reading.

What do you think should happen when you attempt to execute the _same_
request object 50 times using multiple CPU cores?

Oleg

> 
> 
> On Fri, Nov 22, 2013 at 2:37 PM, Oleg Kalnichevski <ol...@apache.org> wrote:
> 
> > On Fri, 2013-11-22 at 14:27 +0200, Petteri Hentilä wrote:
> > > A couple of comments.
> > >
> > > 1. I cannot pass the HttpContext to FutureCallback as a construction
> > > parameter, because that class has only one parameterless constructor. I
> > > also cannot put the HttpContext as the generic type for FutureCallback
> > > because it gives me a compile error.
> > >
> >
> > FutureCallback is an _interface_. One can have any constructor
> > parameters for a class that implements this interface.
> >
> > > 2. I derived my own class from BasicAsyncResponseConsumer to have more
> > > control over the response processing, but that class does not have the
> > > correct methods that I would need. It has a method
> > > responseReceived(HttpResponse) but it does not get HttpContext as a
> > > parameter. It also has a method failed(Exception) but it does not get
> > > HttpContext as a parameter either.
> > >
> >
> > Same deal as with FutureCallback. You can pass any additional contextual
> > parameters at the construction time.
> >
> > Oleg
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Apache CloseableHttpAsyncClient - way to access HttpContext in a callback method?

Posted by Petteri Hentilä <pe...@gmail.com>.
Thanks again.

I made my own implementation of the FutureCallback interface:

private static class FutureCallbackImpl implements
FutureCallback<HttpResponse> {

        private HttpCoreContext coreContext;
        public FutureCallbackImpl(HttpCoreContext coreContext) {
            this.coreContext = coreContext;
        }

        @Override
        public void cancelled() {

        }

        @Override
        public void completed(HttpResponse response) {
            System.out.println("completed" + "->" +
response.getStatusLine();

        }

        @Override
        public void failed(Exception cause) {
            System.out.println("failed" + "->" + cause);
        }
    }

And here is how I use it.

HttpAsyncRequester requester = new HttpAsyncRequester(httpproc);

        final HttpHost target = new HttpHost("www.google.fi", 80, "http");
        BasicHttpRequest request = new BasicHttpRequest("GET", "/");

        for(int i=0; i<50; i++) {
            HttpCoreContext coreContext = HttpCoreContext.create();
            coreContext.setAttribute("index", i);
            FutureCallbackImpl callBack = new
FutureCallbackImpl(coreContext);

            requester.execute(
                new BasicAsyncRequestProducer(target, request),
                new BasicAsyncResponseConsumer(),
                pool,
                coreContext,
                // Handle HTTP response from a callback
                callBack
            );

        }

What I do here is to send 50 requests inside one loop.

However, each time I run the program the failed() callback method is called
a several times with the exception being
java.util.ConcurrentModificationException.

There seems to be a concurrency problem somewhere?



On Fri, Nov 22, 2013 at 2:37 PM, Oleg Kalnichevski <ol...@apache.org> wrote:

> On Fri, 2013-11-22 at 14:27 +0200, Petteri Hentilä wrote:
> > A couple of comments.
> >
> > 1. I cannot pass the HttpContext to FutureCallback as a construction
> > parameter, because that class has only one parameterless constructor. I
> > also cannot put the HttpContext as the generic type for FutureCallback
> > because it gives me a compile error.
> >
>
> FutureCallback is an _interface_. One can have any constructor
> parameters for a class that implements this interface.
>
> > 2. I derived my own class from BasicAsyncResponseConsumer to have more
> > control over the response processing, but that class does not have the
> > correct methods that I would need. It has a method
> > responseReceived(HttpResponse) but it does not get HttpContext as a
> > parameter. It also has a method failed(Exception) but it does not get
> > HttpContext as a parameter either.
> >
>
> Same deal as with FutureCallback. You can pass any additional contextual
> parameters at the construction time.
>
> Oleg
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: Apache CloseableHttpAsyncClient - way to access HttpContext in a callback method?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Fri, 2013-11-22 at 14:27 +0200, Petteri Hentilä wrote:
> A couple of comments.
> 
> 1. I cannot pass the HttpContext to FutureCallback as a construction
> parameter, because that class has only one parameterless constructor. I
> also cannot put the HttpContext as the generic type for FutureCallback
> because it gives me a compile error.
> 

FutureCallback is an _interface_. One can have any constructor
parameters for a class that implements this interface.

> 2. I derived my own class from BasicAsyncResponseConsumer to have more
> control over the response processing, but that class does not have the
> correct methods that I would need. It has a method
> responseReceived(HttpResponse) but it does not get HttpContext as a
> parameter. It also has a method failed(Exception) but it does not get
> HttpContext as a parameter either.
> 

Same deal as with FutureCallback. You can pass any additional contextual
parameters at the construction time.

Oleg



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Apache CloseableHttpAsyncClient - way to access HttpContext in a callback method?

Posted by Petteri Hentilä <pe...@gmail.com>.
A couple of comments.

1. I cannot pass the HttpContext to FutureCallback as a construction
parameter, because that class has only one parameterless constructor. I
also cannot put the HttpContext as the generic type for FutureCallback
because it gives me a compile error.

2. I derived my own class from BasicAsyncResponseConsumer to have more
control over the response processing, but that class does not have the
correct methods that I would need. It has a method
responseReceived(HttpResponse) but it does not get HttpContext as a
parameter. It also has a method failed(Exception) but it does not get
HttpContext as a parameter either.

Here is a sample of my code:

HttpProcessor httpproc = HttpProcessorBuilder.create()
                .add(new RequestContent())
                .add(new RequestTargetHost())
                .add(new RequestConnControl())
                .add(new RequestUserAgent("Test/1.1"))
                .add(new RequestExpectContinue(true)).build();

HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor();
final IOEventDispatch ioEventDispatch = new
DefaultHttpClientIODispatch(protocolHandler,
                ConnectionConfig.DEFAULT);
final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
BasicNIOConnPool pool = new BasicNIOConnPool(ioReactor,
ConnectionConfig.DEFAULT);


final HttpHost target = new HttpHost("www.google.fi", 80, "http");
final CountDownLatch latch = new CountDownLatch(1);

BasicHttpRequest request = new BasicHttpRequest("GET", "/");
HttpCoreContext coreContext = HttpCoreContext.create();

requester.execute(
            new BasicAsyncRequestProducer(target, request),
            new BasicAsyncResponseConsumer(),
            pool,
            coreContext,
            // Handle HTTP response from a callback
            new FutureCallback<HttpResponse>() {

                public void completed(final HttpResponse response) {
                    latch.countDown();
                    System.out.println(target + "->" +
response.getStatusLine());
                }

                public void failed(final Exception ex) {
                    latch.countDown();
                    System.out.println(target + "->" + ex);
                }

                public void cancelled() {
                    latch.countDown();
                    System.out.println(target + " cancelled");
                }

            }
        );


On Fri, Nov 22, 2013 at 11:38 AM, Petteri Hentilä <petteri.hentila@gmail.com
> wrote:

> Yes, I figured that myself too. This is exactly why I am creating this
> client for performance test purposes. I already created a similar blocking
> HTTP client but that is not suitable for every scenario.
>
>
> On Fri, Nov 22, 2013 at 11:33 AM, Oleg Kalnichevski <ol...@apache.org>wrote:
>
>> On Fri, 2013-11-22 at 11:27 +0200, Petteri Hentilä wrote:
>> > Thanks. I will try your suggestion later.
>> >
>> > The reason I am using non-blocking HTTP client is that I will use the
>> > client in a test where there are lots of concurrent long-polling HTTP
>> > connections. In a blocking HTTP client one connection runs in one
>> thread.
>> > But in my test there will be hundreds or even thousands of concurrent
>> HTTP
>> > connections open so I was thinking that using a non-blocking HTTP client
>> > might be a better idea.
>> >
>>
>> Fair enough. A selector based I/O model is a better fit for scenarios
>> where one needs to handle thousands of concurrent connections that are
>> not constantly busy pumping data.
>>
>> Oleg
>>
>> >
>> > On Fri, Nov 22, 2013 at 11:13 AM, Oleg Kalnichevski <olegk@apache.org
>> >wrote:
>> >
>> > > On Fri, 2013-11-22 at 10:55 +0200, Petteri Hentilä wrote:
>> > > > Hello,
>> > > >
>> > > > I'm creating my own asynchronous HTTP client with for performance
>> testing
>> > > > purposes. I am thinking of using Apache's CloseableHttpAsyncClient
>> for
>> > > this.
>> > > >
>> > > > Here is an example how to use this client:
>> > > >
>> http://hc.apache.org/httpcomponents-asyncclient-4.0.x/quickstart.html
>> > > >
>> > > > With CloseableHttpAsyncClient there are three callback methods
>> > > completed(),
>> > > > failed() and cancelled(). The completed() -method gets the
>> HttpResponse
>> > > as
>> > > > a parameter and the failed() -method gets the Exception as the
>> parameter.
>> > > >
>> > > > However, I would also need an access to the HttpContext object in
>> all of
>> > > > these callbacks methods. HttpContext represents execution state of
>> an
>> > > HTTP
>> > > > process. You can for example store any user related objects to
>> > > HttpContext.
>> > > >
>> > > > Basically what I need is to have an access to HttpContext at least
>> in the
>> > > > following three different situations: 1. When the Http response is
>> fully
>> > > > received 2. When an exception occurs (for example the target host is
>> > > down)
>> > > > 3. When a connection timeout occurs
>> > > >
>> > > > Does anyone know a way to achieve this?
>> > >
>> > > There is nothing that prevents you from passing HttpContext instance
>> to
>> > > the FutureCallback as a constructor parameter. You might however want
>> to
>> > > use a custom HttpAsyncResponseConsumer implementation in order to
>> have a
>> > > complete control over the response processing.
>> > >
>> > > As a side note when comes to performance in terms of data throughput
>> you
>> > > might be better served by a blocking HTTP client:
>> > >
>> > >
>> http://wiki.apache.org/HttpComponents/HttpClient3vsHttpClient4vsHttpCore
>> > >
>> > > Oleg
>> > >
>> > >
>> > > ---------------------------------------------------------------------
>> > > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>> > > For additional commands, e-mail: httpclient-users-help@hc.apache.org
>> > >
>> > >
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>>
>>
>

Re: Apache CloseableHttpAsyncClient - way to access HttpContext in a callback method?

Posted by Petteri Hentilä <pe...@gmail.com>.
Yes, I figured that myself too. This is exactly why I am creating this
client for performance test purposes. I already created a similar blocking
HTTP client but that is not suitable for every scenario.


On Fri, Nov 22, 2013 at 11:33 AM, Oleg Kalnichevski <ol...@apache.org>wrote:

> On Fri, 2013-11-22 at 11:27 +0200, Petteri Hentilä wrote:
> > Thanks. I will try your suggestion later.
> >
> > The reason I am using non-blocking HTTP client is that I will use the
> > client in a test where there are lots of concurrent long-polling HTTP
> > connections. In a blocking HTTP client one connection runs in one thread.
> > But in my test there will be hundreds or even thousands of concurrent
> HTTP
> > connections open so I was thinking that using a non-blocking HTTP client
> > might be a better idea.
> >
>
> Fair enough. A selector based I/O model is a better fit for scenarios
> where one needs to handle thousands of concurrent connections that are
> not constantly busy pumping data.
>
> Oleg
>
> >
> > On Fri, Nov 22, 2013 at 11:13 AM, Oleg Kalnichevski <olegk@apache.org
> >wrote:
> >
> > > On Fri, 2013-11-22 at 10:55 +0200, Petteri Hentilä wrote:
> > > > Hello,
> > > >
> > > > I'm creating my own asynchronous HTTP client with for performance
> testing
> > > > purposes. I am thinking of using Apache's CloseableHttpAsyncClient
> for
> > > this.
> > > >
> > > > Here is an example how to use this client:
> > > >
> http://hc.apache.org/httpcomponents-asyncclient-4.0.x/quickstart.html
> > > >
> > > > With CloseableHttpAsyncClient there are three callback methods
> > > completed(),
> > > > failed() and cancelled(). The completed() -method gets the
> HttpResponse
> > > as
> > > > a parameter and the failed() -method gets the Exception as the
> parameter.
> > > >
> > > > However, I would also need an access to the HttpContext object in
> all of
> > > > these callbacks methods. HttpContext represents execution state of an
> > > HTTP
> > > > process. You can for example store any user related objects to
> > > HttpContext.
> > > >
> > > > Basically what I need is to have an access to HttpContext at least
> in the
> > > > following three different situations: 1. When the Http response is
> fully
> > > > received 2. When an exception occurs (for example the target host is
> > > down)
> > > > 3. When a connection timeout occurs
> > > >
> > > > Does anyone know a way to achieve this?
> > >
> > > There is nothing that prevents you from passing HttpContext instance to
> > > the FutureCallback as a constructor parameter. You might however want
> to
> > > use a custom HttpAsyncResponseConsumer implementation in order to have
> a
> > > complete control over the response processing.
> > >
> > > As a side note when comes to performance in terms of data throughput
> you
> > > might be better served by a blocking HTTP client:
> > >
> > >
> http://wiki.apache.org/HttpComponents/HttpClient3vsHttpClient4vsHttpCore
> > >
> > > Oleg
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > >
> > >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: Apache CloseableHttpAsyncClient - way to access HttpContext in a callback method?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Fri, 2013-11-22 at 11:27 +0200, Petteri Hentilä wrote:
> Thanks. I will try your suggestion later.
> 
> The reason I am using non-blocking HTTP client is that I will use the
> client in a test where there are lots of concurrent long-polling HTTP
> connections. In a blocking HTTP client one connection runs in one thread.
> But in my test there will be hundreds or even thousands of concurrent HTTP
> connections open so I was thinking that using a non-blocking HTTP client
> might be a better idea.
> 

Fair enough. A selector based I/O model is a better fit for scenarios
where one needs to handle thousands of concurrent connections that are
not constantly busy pumping data.

Oleg

> 
> On Fri, Nov 22, 2013 at 11:13 AM, Oleg Kalnichevski <ol...@apache.org>wrote:
> 
> > On Fri, 2013-11-22 at 10:55 +0200, Petteri Hentilä wrote:
> > > Hello,
> > >
> > > I'm creating my own asynchronous HTTP client with for performance testing
> > > purposes. I am thinking of using Apache's CloseableHttpAsyncClient for
> > this.
> > >
> > > Here is an example how to use this client:
> > > http://hc.apache.org/httpcomponents-asyncclient-4.0.x/quickstart.html
> > >
> > > With CloseableHttpAsyncClient there are three callback methods
> > completed(),
> > > failed() and cancelled(). The completed() -method gets the HttpResponse
> > as
> > > a parameter and the failed() -method gets the Exception as the parameter.
> > >
> > > However, I would also need an access to the HttpContext object in all of
> > > these callbacks methods. HttpContext represents execution state of an
> > HTTP
> > > process. You can for example store any user related objects to
> > HttpContext.
> > >
> > > Basically what I need is to have an access to HttpContext at least in the
> > > following three different situations: 1. When the Http response is fully
> > > received 2. When an exception occurs (for example the target host is
> > down)
> > > 3. When a connection timeout occurs
> > >
> > > Does anyone know a way to achieve this?
> >
> > There is nothing that prevents you from passing HttpContext instance to
> > the FutureCallback as a constructor parameter. You might however want to
> > use a custom HttpAsyncResponseConsumer implementation in order to have a
> > complete control over the response processing.
> >
> > As a side note when comes to performance in terms of data throughput you
> > might be better served by a blocking HTTP client:
> >
> > http://wiki.apache.org/HttpComponents/HttpClient3vsHttpClient4vsHttpCore
> >
> > Oleg
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Apache CloseableHttpAsyncClient - way to access HttpContext in a callback method?

Posted by Petteri Hentilä <pe...@gmail.com>.
Thanks. I will try your suggestion later.

The reason I am using non-blocking HTTP client is that I will use the
client in a test where there are lots of concurrent long-polling HTTP
connections. In a blocking HTTP client one connection runs in one thread.
But in my test there will be hundreds or even thousands of concurrent HTTP
connections open so I was thinking that using a non-blocking HTTP client
might be a better idea.


On Fri, Nov 22, 2013 at 11:13 AM, Oleg Kalnichevski <ol...@apache.org>wrote:

> On Fri, 2013-11-22 at 10:55 +0200, Petteri Hentilä wrote:
> > Hello,
> >
> > I'm creating my own asynchronous HTTP client with for performance testing
> > purposes. I am thinking of using Apache's CloseableHttpAsyncClient for
> this.
> >
> > Here is an example how to use this client:
> > http://hc.apache.org/httpcomponents-asyncclient-4.0.x/quickstart.html
> >
> > With CloseableHttpAsyncClient there are three callback methods
> completed(),
> > failed() and cancelled(). The completed() -method gets the HttpResponse
> as
> > a parameter and the failed() -method gets the Exception as the parameter.
> >
> > However, I would also need an access to the HttpContext object in all of
> > these callbacks methods. HttpContext represents execution state of an
> HTTP
> > process. You can for example store any user related objects to
> HttpContext.
> >
> > Basically what I need is to have an access to HttpContext at least in the
> > following three different situations: 1. When the Http response is fully
> > received 2. When an exception occurs (for example the target host is
> down)
> > 3. When a connection timeout occurs
> >
> > Does anyone know a way to achieve this?
>
> There is nothing that prevents you from passing HttpContext instance to
> the FutureCallback as a constructor parameter. You might however want to
> use a custom HttpAsyncResponseConsumer implementation in order to have a
> complete control over the response processing.
>
> As a side note when comes to performance in terms of data throughput you
> might be better served by a blocking HTTP client:
>
> http://wiki.apache.org/HttpComponents/HttpClient3vsHttpClient4vsHttpCore
>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: Apache CloseableHttpAsyncClient - way to access HttpContext in a callback method?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Fri, 2013-11-22 at 10:55 +0200, Petteri Hentilä wrote:
> Hello,
> 
> I'm creating my own asynchronous HTTP client with for performance testing
> purposes. I am thinking of using Apache's CloseableHttpAsyncClient for this.
> 
> Here is an example how to use this client:
> http://hc.apache.org/httpcomponents-asyncclient-4.0.x/quickstart.html
> 
> With CloseableHttpAsyncClient there are three callback methods completed(),
> failed() and cancelled(). The completed() -method gets the HttpResponse as
> a parameter and the failed() -method gets the Exception as the parameter.
> 
> However, I would also need an access to the HttpContext object in all of
> these callbacks methods. HttpContext represents execution state of an HTTP
> process. You can for example store any user related objects to HttpContext.
> 
> Basically what I need is to have an access to HttpContext at least in the
> following three different situations: 1. When the Http response is fully
> received 2. When an exception occurs (for example the target host is down)
> 3. When a connection timeout occurs
> 
> Does anyone know a way to achieve this?

There is nothing that prevents you from passing HttpContext instance to
the FutureCallback as a constructor parameter. You might however want to
use a custom HttpAsyncResponseConsumer implementation in order to have a
complete control over the response processing.

As a side note when comes to performance in terms of data throughput you
might be better served by a blocking HTTP client:

http://wiki.apache.org/HttpComponents/HttpClient3vsHttpClient4vsHttpCore

Oleg 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org