Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling multiple HTTP connections

I finished coding a java application that uses 25 different threads, each thread is an infinite loop where an http request is sent and the json object(small one) that is returned is processed. It is crucial that the time between two requests sent by a specific thread is less than 500ms. However, I did some benchmark on my program and that time is well over 1000ms. SO my question is: Is there a better way to handle multiple connections other than creating multiple threads ? I am in desperate need for help so I'm thankful for any advice you may have !

PS: I have a decent internet connection ( my ping to the destination server of the requests is about 120ms).

like image 272
user1776576 Avatar asked Nov 04 '22 08:11

user1776576


1 Answers

I'd suggest looking at Apache HttpClient:

Specifically, you'll be interested in constructing a client that has a pooling connection manager. You can then leverage the same client.

PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
connectionManager.setMaxTotal(number);

HttpClient client = new DefaultHttpClient(connectionManager);

Here's a specific example that handles your use-case:

  • PoolingConnectionManager example
like image 187
dreadwail Avatar answered Nov 11 '22 04:11

dreadwail