1

I have requirement to build one API which will call 6 API asynchronous and collect all data into one object.

After collecting all data it will call other api for persist the data in database.

Please let me know which framework will be most suitable for this requirement.

Most prefer we want this in java.

Thanks In Advance.

ak2020
  • 85
  • 2
  • 10
  • This may help https://stackoverflow.com/questions/42504277/java-8-call-methods-async-in-parallel-and-combine-their-results – Sundararaj Govindasamy Jan 26 '19 at 00:56
  • if you can modify API to return a asynchronous result like Future the method provided by @bartius-nigel is a good way.if you can't modify API,you must do something with the callback of API – TongChen Jan 26 '19 at 14:50
  • Does this answer your question? [Java 8 - Call methods async in parallel and combine their results](https://stackoverflow.com/questions/42504277/java-8-call-methods-async-in-parallel-and-combine-their-results) – vahdet Dec 04 '19 at 17:01

1 Answers1

5

I'm not sure if this is what you're looking for, but if your 6 asynchronous calls can be made into classes that implement the Callable interface, you can create a list and pass it to an executor service function. See below:

  1. create the list and add callable tasks to that list

    List<Callable<String>> callableTasks = new ArrayList<>();
    
    callableTasks.add(callableTask);
    
    callableTasks.add(callableTask);
    
    callableTasks.add(callableTask);
    
  2. pass the list to the executor service. The executorService is an instantiation of the ExecutorService class.

    List<Future<String>> futures = executorService.invokeAll(callableTasks);
    
  3. The futures object stores certain information about the thread. Each of those 6 api calls can be stored in that futures object.

for further information, you can consult this article https://www.baeldung.com/java-executor-service-tutorial

Best of luck!

vahdet
  • 6,357
  • 9
  • 51
  • 106
bartius
  • 202
  • 2
  • 7