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 Konrad Windszus <kw...@apache.org> on 2023/08/31 08:08:17 UTC

org.apache.http.concurrent.FutureCallback: Exception handling

Hi,
I can pass a FutureCallback implementation to HttpAsyncClient.execute(…).
However the javadoc at https://hc.apache.org/httpcomponents-core-5.1.x/current/httpcore5/apidocs/org/apache/hc/core5/concurrent/FutureCallback.html is very sparse.
Particularly the following questions are not answered:

1) Is "completed(HttpResponse)" only called for success status codes or for all kinds server of responses? Would be good to mention that in https://hc.apache.org/httpcomponents-client-5.2.x/current/httpclient5/apidocs/org/apache/hc/client5/http/impl/async/CloseableHttpAsyncClient.html and other mentions of the execute method leveraging the parameter FutureCallback (because it seems to be specific to the FutureCallback being used from HttpAsyncClient.execute(…))

2) What (unchecked) exception to throw in the FutureCallback method for the future to fail with ExecutionException.html. Does it matter? Is there any recommendation, e.g. if the HTTP endpoint returned a 500 status? Maybe also the examples e.g. in https://github.com/apache/httpcomponents-client/blob/5.2.x/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttp1Pipelining.java could be extended in this regard.

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


Re: org.apache.http.concurrent.FutureCallback: Exception handling

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2023-08-31 at 11:32 +0200, Konrad Windszus wrote:
> Hi Oleg,
> Thanks for the input.
> 
> > On 31. Aug 2023, at 11:17, Oleg Kalnichevski <ol...@apache.org>
> > wrote:
> > 
> > On Thu, 2023-08-31 at 10:08 +0200, Konrad Windszus wrote:
> > > Hi,
> > > I can pass a FutureCallback implementation to
> > > HttpAsyncClient.execute(…).
> > > However the javadoc at
> > > https://hc.apache.org/httpcomponents-core-5.1.x/current/httpcore5/apidocs/org/apache/hc/core5/concurrent/FutureCallback.html
> > >  is very sparse.
> > > Particularly the following questions are not answered:
> > > 
> > > 1) Is "completed(HttpResponse)" only called for success status
> > > codes
> > > or for all kinds server of responses? Would be good to mention
> > > that
> > > in
> > 
> > It is called for all valid final HTTP response messages (status
> > code =>
> > 200).
> 
> Then I guess
> https://hc.apache.org/httpcomponents-core-5.1.x/current/httpcore5/apidocs/org/apache/hc/core5/concurrent/FutureCallback.html#failed(java.lang.Exception)
>  is called for status code >= 300? How to access the response body
> there?
> > 

You guess it wrong. 3xx either get handled internally or get propagated
to the caller. 


> > > https://hc.apache.org/httpcomponents-client-5.2.x/current/httpclient5/apidocs/org/apache/hc/client5/http/impl/async/CloseableHttpAsyncClient.html
> > >  and other mentions of the execute method leveraging the
> > > parameter
> > > FutureCallback (because it seems to be specific to the
> > > FutureCallback
> > > being used from HttpAsyncClient.execute(…))
> > > 
> > > 2) What (unchecked) exception to throw in the FutureCallback
> > > method
> > > for the future to fail with ExecutionException.html. Does it
> > > matter?
> > > Is there any recommendation, e.g. if the HTTP endpoint returned a
> > > 500
> > > status? Maybe also the examples e.g. in
> > > https://github.com/apache/httpcomponents-client/blob/5.2.x/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttp1Pipelining.java
> > >  could be extended in this regard.
> > > 
> > 
> > You should consider returning a object with error information as a
> > result of the operation instead of throwing an unchecked exception
> > from
> > the callback. 
> Why is that? I want Future.get() to throw an exception in case of
> unexpected responses, otherwise callers you have to call Future.get()
> and in addition check that additional object.
> And I guess by "consider returning a object with error” you meant
> populating some error object, because none of the FutureCallback
> methods allow to return anything.
> 

If you are using blocking Futures for anything serious you are doing it
very, very wrong. Then you might be massively better off using the
classic APIs. 

Oleg 


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


Re: org.apache.http.concurrent.FutureCallback: Exception handling

Posted by Konrad Windszus <kw...@apache.org>.
Hi Oleg,
Thanks for the input.

> On 31. Aug 2023, at 11:17, Oleg Kalnichevski <ol...@apache.org> wrote:
> 
> On Thu, 2023-08-31 at 10:08 +0200, Konrad Windszus wrote:
>> Hi,
>> I can pass a FutureCallback implementation to
>> HttpAsyncClient.execute(…).
>> However the javadoc at
>> https://hc.apache.org/httpcomponents-core-5.1.x/current/httpcore5/apidocs/org/apache/hc/core5/concurrent/FutureCallback.html
>>  is very sparse.
>> Particularly the following questions are not answered:
>> 
>> 1) Is "completed(HttpResponse)" only called for success status codes
>> or for all kinds server of responses? Would be good to mention that
>> in
> 
> It is called for all valid final HTTP response messages (status code =>
> 200).

Then I guess https://hc.apache.org/httpcomponents-core-5.1.x/current/httpcore5/apidocs/org/apache/hc/core5/concurrent/FutureCallback.html#failed(java.lang.Exception) is called for status code >= 300? How to access the response body there?
> 
>> https://hc.apache.org/httpcomponents-client-5.2.x/current/httpclient5/apidocs/org/apache/hc/client5/http/impl/async/CloseableHttpAsyncClient.html
>>  and other mentions of the execute method leveraging the parameter
>> FutureCallback (because it seems to be specific to the FutureCallback
>> being used from HttpAsyncClient.execute(…))
>> 
>> 2) What (unchecked) exception to throw in the FutureCallback method
>> for the future to fail with ExecutionException.html. Does it matter?
>> Is there any recommendation, e.g. if the HTTP endpoint returned a 500
>> status? Maybe also the examples e.g. in
>> https://github.com/apache/httpcomponents-client/blob/5.2.x/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttp1Pipelining.java
>>  could be extended in this regard.
>> 
> 
> You should consider returning a object with error information as a
> result of the operation instead of throwing an unchecked exception from
> the callback. 
Why is that? I want Future.get() to throw an exception in case of unexpected responses, otherwise callers you have to call Future.get() and in addition check that additional object.
And I guess by "consider returning a object with error” you meant populating some error object, because none of the FutureCallback methods allow to return anything.

Konrad

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

Re: org.apache.http.concurrent.FutureCallback: Exception handling

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2023-08-31 at 10:08 +0200, Konrad Windszus wrote:
> Hi,
> I can pass a FutureCallback implementation to
> HttpAsyncClient.execute(…).
> However the javadoc at
> https://hc.apache.org/httpcomponents-core-5.1.x/current/httpcore5/apidocs/org/apache/hc/core5/concurrent/FutureCallback.html
>  is very sparse.
> Particularly the following questions are not answered:
> 
> 1) Is "completed(HttpResponse)" only called for success status codes
> or for all kinds server of responses? Would be good to mention that
> in

It is called for all valid final HTTP response messages (status code =>
200).

> https://hc.apache.org/httpcomponents-client-5.2.x/current/httpclient5/apidocs/org/apache/hc/client5/http/impl/async/CloseableHttpAsyncClient.html
>  and other mentions of the execute method leveraging the parameter
> FutureCallback (because it seems to be specific to the FutureCallback
> being used from HttpAsyncClient.execute(…))
> 
> 2) What (unchecked) exception to throw in the FutureCallback method
> for the future to fail with ExecutionException.html. Does it matter?
> Is there any recommendation, e.g. if the HTTP endpoint returned a 500
> status? Maybe also the examples e.g. in
> https://github.com/apache/httpcomponents-client/blob/5.2.x/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttp1Pipelining.java
>  could be extended in this regard.
> 

You should consider returning a object with error information as a
result of the operation instead of throwing an unchecked exception from
the callback. 

Oleg


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