What is the most efficient way to call rest service multiple times in a loop

77 views Asked by At

I need to call an api endpoint multiple times in a loop. While the connection path remains same one parameter changes in every call. I have written it like below. I am opening the connection at the beginning of each iteration and closing it in the end because of that param. This is opening and closing sockets each time. I want to know if there is a better way to do this? Also, if I want to measure the connection overhead, how would I do that? I am using HttpURLConnection but I can opt for any other library if that serves my purpose better.

try {
        
        String dataUrl = serviceBaseURL + "some_path";
        
        BigDecimal count = new BigDecimal(0);
        BigDecimal offset = new BigDecimal(0);
        BigDecimal limit = new BigDecimal(20);
        
        do {
            // getting the params required for this connection. only the offset param changes in each call. 
            String query = getQuery(offset, limit);
                            
            URL url = new URL(dataUrl + query);
            URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
            String encodedURL = uri.toASCIIString();
            
            
            URL connUrl = new URL(encodedURL);

            HttpURLConnection conn = (HttpURLConnection)connUrl.openConnection();
            conn.setRequestMethod("GET");

            int status = conn.getResponseCode();

            if (status != 200) {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
                String inputLine;
                StringBuffer content = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    content.append(inputLine);
                }
                in.close();
                throw new Exception("Error: "+content.toString());
            } else {

                // processing response
                ....
                ....
                count = (BigDecimal)contentJson.get("count");
                
            }

            conn.disconnect();
            offset = offset.add(limit);
        } while (count.compareTo(offset) >= 0);
        
        
        
    } catch (Exception exception) {
        System.out.println(exception.getMessage());
        exception.printStackTrace();
    }
1

There are 1 answers

0
Partha Sarathi On

You can use CompletableFuture for your requirement.

for (int i = 0; i < noOfLoop; i++) { 
    CompletableFuture<Void> future = CompletableFuture.runAsync(() -> yourMethod()); 
}

Now, you can modify the code based on your need. You can use supplyAsync method as well. Reffer to - runAsync vs supplyAsync

Parallel Stream can be a good choice too.

IntStream.range(0, noOfLoop).parallel().forEach(i -> yourMethod());

You can also use simple thread creation mechanism as well.