0

In my application, I am consuming an API: localhost:8080/data/date?12-12-2019&&nextBatch=200. I need to call this API in a while loop by updating 'nextBatch', something like nextBatch+=200, variable until I get a response from the API. When I call API with the updated value I add the response to the list after every call. I need help to call the same API with updated value so that it improves performance? Below is a sample code for the same.

while (checkMoreData) {

            ResponseEntity<Data[]> responseEntity = restTemplate
                    .exchange("localhost:8080/data/date?12-12-2019&&nextBatch=200", HttpMethod.GET, entity, Data[].class);

            if (responseEntity.hasBody() && responseEntity.getBody() != null
                    && responseEntity.getBody().length > 0) {

                entityList.addAll(Arrays.asList(responseEntity.getBody()));
                nextBatch+= 200;
            } else {

                checkMoreData = false;
            }
Sandeep Kumar
  • 2,397
  • 5
  • 30
  • 37
sam N
  • 83
  • 1
  • 8
  • Maybe this is what you are looking for: `https://stackoverflow.com/q/31572475/3519504` – Sandeep Kumar Dec 14 '19 at 15:35
  • Thanks for sharing this link @SandeepKumar. I did implement the solution provided in the answer of above link. Now my application is able to make asynchronous calls and performance is improved significantly. However,what I observed is this solution makes same API calls multiple times. For eg. "localhost:8080/data/date?12-12-2019&&nextBatch=200" gets called ~20times , "localhost:8080/data/date?12-12-2019&&nextBatch=400" gets called ~35times and so on for each 'nextBatch' parameter change. What I want to implement is for each 'nextBatch' parameter change a single call to be made to the API. – sam N Dec 15 '19 at 12:49
  • To add to above comment, what I need to implement is, unless the earlier API has responseBody, I don't want to make next API call. So even though I try to call APIs asynchronously, there is a check for responseBody before making next API call, which makes my application slow. – sam N Dec 15 '19 at 13:23

0 Answers0