3

I am working on a Java server which needs to send multiple HTTP get requests to another server and process the responses and wrap them up. Currently I am using synchronous way to do this like in Java:

for(Request request: requestList){
    Response response = client.send(request);
}

This works, but it takes a long time if the requestList's size is big, since it is doing it sequentially: send a request, wait for the response and then send next.

I am wondering, is there a way to speed this up? I am thinking if using multiple thread to send multiple request will do the trick? Or maybe not, since the total response time is fixed for a certain amount of requests.

Any idea or explanation, why or why not this will work will be helpful. I am using Java.

chrisTina
  • 2,298
  • 9
  • 40
  • 74
  • Multiple threads will do the trick. I've done this before and it worked ok. However, was just like 4 multiple request, more than that I really don't know...How many items you have in that for loop? – Gabriel Rodriguez Sep 24 '15 at 02:16
  • Any explanation? Let's say I send 2 requests, the response time for one request is 10 seconds. Then no matter what strategy I am using, it always takes 20 seconds to get 2 response, right? – chrisTina Sep 24 '15 at 02:21
  • May be the size is 50. – chrisTina Sep 24 '15 at 02:22
  • Not necessarily, it depends on how long your program is sitting waiting for the responses. Let's say setting up and sending the request takes 2 seconds, the other server takes 6 seconds to process and respond, and your server takes 2 seconds to read the response. Sequentially, that's 2 + 6 + 2 + 2 + 6 + 2 = 20 seconds, but for both 6 second intervals, your server's just sitting there. It could be using some of that time setting up and sending additional requests, so in this simple example, the 2 requests could take 12 seconds when done in parallel. Also multiple threads can use multiple cores. – blm Sep 24 '15 at 02:28
  • @blm not sure why the answer for your example is 12 seconds. But all in all, let's say that for a size of 50 requestList, it takes 10 seconds, will I see a noticeable time decrease if I do this using parallel? – chrisTina Sep 24 '15 at 02:45
  • The 12 comes because both threads can send the request almost at the same time, so instead of waiting 6 seconds for the first response then later 6 seconds for the second response, there's only a single 6 second wait for both responses. As to the 50 in 10 seconds part, it's hard to say, there are a lot of variables, it depends on how long it takes to build and send the request, how well the other server is at handling multiple requests at the same time, etc. The best I could say without a lot more information is "probably". It's fairly easy to code up though, so I'd just try it and see. – blm Sep 24 '15 at 02:56
  • asynchronous programming immediately comes to mind and [this thread](http://stackoverflow.com/questions/3142915/how-do-you-create-an-asynchronous-http-request-in-java) might be of use to you. – ThisClark Sep 24 '15 at 03:05
  • Q: What do you mean by "Java server"? Is this an application server? Which app server: JBoss? WebSphere? Other? Q: Are you using SOAP web services? Q: Are you limited to HTTP requests/responses, or can you use other transports (for example, IBM MQ, or a custom protocol)? You haven't given any details ... but it sounds like your biggest bottleneck is the transport (HTTP over multiple different TCP connections) – paulsm4 Sep 28 '15 at 05:38

1 Answers1

1

If you use Java 8, you can try lambdas for parallel stream.

requestList.parallelStream().forEach(....);

But it is not good idea to use them, when the requests depends on one another. Also if your app is used by multiple users, I don't know how your server will handle that many requests.

Other solutions is to use threads. But you should limit the number of threads(thread pool) for every loop(user). Of course this depends on how many resources you have.

Tony
  • 72
  • 1
  • 8