-1

I want to call the third party API multiple times using the RestTemplate(for each customer id I have to call REST API) currently I have written like below and its working fine but it's taking time because there are many customers I'd and calling API for each customer id, is there any way I can make this parallel.

 public List<Organization> getCustomeOrganizationInfo(){
   String url="https://url.net/core/v1/customers"
   List<Organization> organizationList = new ArrayList<>();

    for(Customer customer:CustomerList){

      String restUrlWithUserId=url+"/customer.getCustomerId"

        CustomerInfo customerInfo = restTemplate.exchange(
                restUrlWithUserId,
                HttpMethod.GET,
                request,
                String.class
        );
        
    Organization organization =new Organization();
    organization.setCustomerId(customer.getCustomerId())
    organization.setorganizationId(customerInfo.getCustomeOrganizationId())
    organization.setorganizationname(customerInfo.getCustomeOrganizationName())
        
   organizationList.add(organization)       
}

}
reddy
  • 79
  • 1
  • 11
  • It depends on the API of the third party. If there is an endpoint that receives a list of user ids, you can do only one call – omer blechman Feb 11 '22 at 11:24
  • the problem is ...third party API does not accept the list of user(I wanted some efficient way to achieve this) – reddy Feb 11 '22 at 11:31
  • Efficient way means.. currently it's taking time because there are many customers I'd and doing call for each customer id,Is there any way I can make this parallel – reddy Feb 11 '22 at 12:31
  • This is not feasible. You should ask for a api that accept multiple users list. What if your org is having 10000 of users, in that case you will be calling the api 10000 time, and if each time it is taking 1 sec, then 1000s, 166 minutes. So think about it. – Pirate Feb 11 '22 at 13:12
  • this is third party API we don't have control. For now we have to get each response by each customer id – reddy Feb 11 '22 at 13:33

1 Answers1

0

Is there any way I can make this parallel

For concurrency and clean code, you should separate your restTemplate call to another class(service), for example, ThirdPartyCustomerService.java. This class will be held responsible for calling outside.

@Service
public class ThirdPartyCustomerService {
   private final RestTemplate restTemplate;
   private final String url = '...';
   ...
   public CustomerInfo getCustomerInfo() {
       return this.restTemplate...
   }
}

Then you can inject this class into your service class. Now if you want to run it concurrency. You could try @Async and Future here. Just need a little bit of change on the new service and remember to call Future.get() on your main service.

@Async
public Future<CustomerInfo> getCustomerInfo() {
   return new AsyncResult<CustomerInfo>(this.restTemplate...);
}

Or you can use WebClient, an alternative for RestTemplate and AsyncRestTemplate.

Linh Vu
  • 736
  • 3
  • 7
  • For each call I also need to prepare the Organization object and add it to list but list is not thread safe ,we can use the Async directly? – reddy Feb 11 '22 at 13:31
  • I just gave you an idea for your statement "Is there any way I can make this parallel". You could wait for all future.get and after that loop over it to put it into the list, for example. – Linh Vu Feb 11 '22 at 13:40