I have a Java program that sends hundreds of GET requests to a server using Java 11 HttpClient, and it needs to do this 2-3 times every minute. Currently, it does the following (just describing the logic):
for each request that needs to be sent:
//build GET request
CompletableFuture<HttpResponse<String>> future = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
futures.add(future)
// for every 100 request, sleep for 2500 milliseconds (to wait for responses)
for each future in futures:
// parse JSON response
The code works mostly fine, however, it is seriously impacted by the strength of the internet connection, as if it is bad, 80% of the requests don't get their responses, as the waiting time wasn't enough.
The question is if this is the correct way to do this, and if not, what would it be. Also, should I rather wait for all requests to get a response (like with sending a requests sinchronously and using .join()), and if yes, how can I do that.