using apache httpclient efficiently in multithreaded environment

2016-05-12 Thread Check Peck
I have a library which is being used by customer and they are passing
DataRequest object which has userid, various timeouts and some other fields
in it. Now I use this DataRequest object to make a URL and then I make an
HTTP call using Apache HttpClient and my service returns back a JSON
response which I use it to make a DataResponse object and return this
DataResponse object back to them.

Below is my DataClient class used by customer by passing DataRequest object
to it.

https://gist.github.com/TechGeeky/250be2d9cdef3fa5107a17058a265d4c

And here is DataFetcherTask class:

https://gist.github.com/TechGeeky/c1b21025e0f81d222b792dedac0a817d

Customer within our company will use my library like this as shown below by
using my factory in their code base -

// if they are calling getSyncData() method
DataResponse response =
DataClientFactory.getInstance().getSyncData(key);

// and if they want to call getAsyncData() method
Future response =
DataClientFactory.getInstance().getAsyncData(key);

I am implementing "sync call as async + waiting" since I want to throttle
them with the number of threads otherwise they can bombard our service
without any control. My library will be used by lot of customers within our
company and their applications won't ever shutdown, they will keep running
always. The only thing will happen is their machines will get restarted,
that's all.

Is this the right way to use Apache HttpClient in production in
multithreaded environment? Or there is any better/efficient way?

I have to use various timeout values present in my DataRequest class in my
Apache HttpClient calls so that's  why I am creating RequestConfig and
using it in my call method. I have simplified the code so that idea gets
clear what I am trying to do.


Re: Apache HttpClient TCP Keep-Alive (socket keep-alive)

2016-05-12 Thread Oleg Kalnichevski
On Thu, 2016-05-12 at 13:46 +0100, Baratali Izmailov wrote:
> > Please consider upgrading. I am not entirely sure if HC 4.2 supports TCP 
> > keepalive
> setting.
> 
> Unfortunately, to upgrade HC we need to upgrade Spring to the latest
> version which requires Java 8. But, we cannot force our clients to use Java
> 8 yet.

There is no need to upgrade Spring. You can upgrade HC dependency to
something more recent without upgrading Spring itself. 

In this case however you should pass a custom instance of HttpClient to
ClientHttpRequestFactory

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.html#HttpComponentsClientHttpRequestFactory-org.apache.http.client.HttpClient-

> However, I don't see SO_KEEPALIVE parameter in the lastest Apache HC
> javadocs in "The following parameters can be used to customize the behavior
> of this class:" section:
> https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpClient.html
> 
> Could you please show me example how to set SO_KEEPALIVE parameter in new
> versions of Apache HC?
> 

---
SocketConfig socketConfig = SocketConfig.custom()
.setSoKeepAlive(true)
.build();
CloseableHttpClient client = HttpClientBuilder.create()
.setDefaultSocketConfig(socketConfig)
.build();
ClientHttpRequestFactory clientfactory = new 
HttpComponentsClientHttpRequestFactory(client); 
---

Hope this helps

Oleg


> Thanks,
> Baratali Izmailov.
> 
> On 12 May 2016 at 09:53, Oleg Kalnichevski  wrote:
> 
> > On Thu, 2016-05-12 at 09:23 +0100, Baratali Izmailov wrote:
> > > Hello. Thanks for the quick response.
> > >
> > > > Is there any way you can turn this into 2 requests?
> > > For now we cannot split this into 2 HTTP requests, because we have to
> > > change client-server protocol communication and re-implement some parts
> > of
> > > our application, which will take much time.
> > > I understand that it is not that effective to keep connection open too
> > > long, but it's only simple solution I see for now.
> > >
> > > > What version of HttpClient are you using? It looks like something
> > fairly
> > > old.
> > > It is 4.2.2. Actually I use Spring 3.2.1 httpInvoker which uses
> > > DefaultHttpClient.
> > >
> >
> > Please consider upgrading. I am not entirely sure if HC 4.2 supports TCP
> > keepalive setting.
> >
> > Oleg
> >
> > > > Using TCP keepalive wont help you with default OS settings, it would
> > not
> > > start to send them in the first 20 idle minutes.
> > > Can I configure Apache httpd server to do this (set keep-alive timeouts)?
> > >
> > > Thanks,
> > > Baratali.
> >
> >
> >
> > -
> > To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org
> > For additional commands, e-mail: httpclient-users-h...@hc.apache.org
> >
> >



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



Re: Apache HttpClient TCP Keep-Alive (socket keep-alive)

2016-05-12 Thread Baratali Izmailov
> Please consider upgrading. I am not entirely sure if HC 4.2 supports TCP 
> keepalive
setting.

Unfortunately, to upgrade HC we need to upgrade Spring to the latest
version which requires Java 8. But, we cannot force our clients to use Java
8 yet.
However, I don't see SO_KEEPALIVE parameter in the lastest Apache HC
javadocs in "The following parameters can be used to customize the behavior
of this class:" section:
https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpClient.html

Could you please show me example how to set SO_KEEPALIVE parameter in new
versions of Apache HC?

Thanks,
Baratali Izmailov.

On 12 May 2016 at 09:53, Oleg Kalnichevski  wrote:

> On Thu, 2016-05-12 at 09:23 +0100, Baratali Izmailov wrote:
> > Hello. Thanks for the quick response.
> >
> > > Is there any way you can turn this into 2 requests?
> > For now we cannot split this into 2 HTTP requests, because we have to
> > change client-server protocol communication and re-implement some parts
> of
> > our application, which will take much time.
> > I understand that it is not that effective to keep connection open too
> > long, but it's only simple solution I see for now.
> >
> > > What version of HttpClient are you using? It looks like something
> fairly
> > old.
> > It is 4.2.2. Actually I use Spring 3.2.1 httpInvoker which uses
> > DefaultHttpClient.
> >
>
> Please consider upgrading. I am not entirely sure if HC 4.2 supports TCP
> keepalive setting.
>
> Oleg
>
> > > Using TCP keepalive wont help you with default OS settings, it would
> not
> > start to send them in the first 20 idle minutes.
> > Can I configure Apache httpd server to do this (set keep-alive timeouts)?
> >
> > Thanks,
> > Baratali.
>
>
>
> -
> To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org
> For additional commands, e-mail: httpclient-users-h...@hc.apache.org
>
>


Re: Apache HttpClient TCP Keep-Alive (socket keep-alive)

2016-05-12 Thread Oleg Kalnichevski
On Thu, 2016-05-12 at 09:23 +0100, Baratali Izmailov wrote:
> Hello. Thanks for the quick response.
> 
> > Is there any way you can turn this into 2 requests?
> For now we cannot split this into 2 HTTP requests, because we have to
> change client-server protocol communication and re-implement some parts of
> our application, which will take much time.
> I understand that it is not that effective to keep connection open too
> long, but it's only simple solution I see for now.
> 
> > What version of HttpClient are you using? It looks like something fairly
> old.
> It is 4.2.2. Actually I use Spring 3.2.1 httpInvoker which uses
> DefaultHttpClient.
> 

Please consider upgrading. I am not entirely sure if HC 4.2 supports TCP
keepalive setting.

Oleg

> > Using TCP keepalive wont help you with default OS settings, it would not
> start to send them in the first 20 idle minutes.
> Can I configure Apache httpd server to do this (set keep-alive timeouts)?
> 
> Thanks,
> Baratali.



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



Re: Apache HttpClient TCP Keep-Alive (socket keep-alive)

2016-05-12 Thread Baratali Izmailov
Hello. Thanks for the quick response.

> Is there any way you can turn this into 2 requests?
For now we cannot split this into 2 HTTP requests, because we have to
change client-server protocol communication and re-implement some parts of
our application, which will take much time.
I understand that it is not that effective to keep connection open too
long, but it's only simple solution I see for now.

> What version of HttpClient are you using? It looks like something fairly
old.
It is 4.2.2. Actually I use Spring 3.2.1 httpInvoker which uses
DefaultHttpClient.

> Using TCP keepalive wont help you with default OS settings, it would not
start to send them in the first 20 idle minutes.
Can I configure Apache httpd server to do this (set keep-alive timeouts)?

Thanks,
Baratali.