Jeeri
Jeeri

Reputation: 504

How to make multiple API calls in a efficient way in java

I have a list of 100k users. I have to loop through the list and make an API call to the server to get the result. Every time I create a new URL connections and making the APi call then closing the connection once I read the input stream, but it is taking too much time.

Is there any optimized way to do it, like using the same instance of URL connection multiple times instead of closing it? or going for another third-party library will improve the speed of execution?

I am calling the below method in my loop to get the output.

private String getOutput(String loginName) {
    String responseStatus = null;
    HttpURLConnection connection = null;

    try {
        URL url= new URL(<<https://api.junk.123.com/output>>);

        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("apikey", "authentication key");
        connection.setUseCaches(false);
        connection.setDoOutput(true);

        //Send request
        try(DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())){
            JsonObject jsonParam = new JsonObject();
            jsonParam.putString("loginName", "loginName");
            outputStream.writeBytes(jsonParam.toString());
            outputStream.flush();
        }

        //Get response

        InputStream inputStream;
        if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
            inputStream = connection.getInputStream();
        } else {
            inputStream = connection.getErrorStream();
        }
        if(null == inputStream){
            return String.valueOf(connection.getResponseCode());
        }

        StringBuilder response = new StringBuilder();
        try (BufferedReader inputBuffer = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while (null != (line = inputBuffer.readLine())) {
                response.append(line);
                response.append("\r");
            }
        }

        JsonObject jsonObject = new JsonObject(response.toString());
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            responseStatus = "success";
        } else {
            responseStatus = String.valueOf(connection.getResponseCode()) + jsonObject.getString("errorMessage") ;
        }
    } catch (MalformedURLException e) {
        logger.error("Malformed URL exception occurred while calling the API", entry.getKey(), e);
    } catch (IOException e) {
        logger.error("Creation of connection failed while calling the API", entry.getKey(), e);
    } catch (Exception e) {
        logger.error("Error occurred  while calling the API", entry.getKey(),  e);
    } finally {
        if (null != connection){
            connection.disconnect();
        }
    }
    return responseStatus;
}

Upvotes: 3

Views: 7386

Answers (1)

Stephen C
Stephen C

Reputation: 718886

This Q&A explains that HTTP persistent connections are implemented behind the scenes by HttpURLConnection:

However, that may not be sufficient. If you use a single client-side thread to do the fetching you are limited by the round trip time for the requests; i.e. you can't start a second request until the result of the first one has been returned to you. You can remedy this ... up to a point ... by using multiple client-side threads.

However (#2) sending multiple requests in parallel also has its limits. Beyond a certain point you will saturate the client, the server or the network. In addition, some servers have throttling mechanisms to cap the number of requests that a client can make.

The way to get maximum throughput would be to redesign the API so that a single request can get information for multiple users.

Upvotes: 2

Related Questions