Multithreaded API calls

0
Dear all,   We are facing an issue where a quite extensive number of API calls may have to be made to get the required information. The endpoint can only supply product info for one product (at a time). This means that for a form, I may have to do 20 calls to get all necessary information, which is going to take a lot of time if done sequentially. Only once all the information is present, can we proceed with filling out the form.   Question: do any of you have an idea about how we could do all 20 calls simultaneously (multi-threaded) and synch up after that? I was thinking that maybe it is possible to use some Java to achieve this. Looking forward to any and all suggestions and advice – is this something to be wished for or a no go?    Best regards,   Wouter
asked
3 answers
2

One way of doing this in Mendix is as follows:

  • Create a Product entity with two attributes endpoint and result
  • Create a microflow which takes a Product object, performs the API call, and updates the object with the result
  • Create a Java action which takes a list of Products and a microflow as input parameters and returns a boolean

Use the following code in the Java action.

 

// BEGIN USER CODE
//This execute a microflow for each input object
//The input microflow is expected to update its input object with the results of the API call

HashMap<String, Object> parameters = new HashMap<String, Object>();
ArrayList<Future<Object>> futures = new ArrayList<Future<Object>>();

for (Product product : productList) {
  parameters.put("Product", product);
  Future<Object> future = Core.executeAsync(this.getContext(), microflow, false, parameters);
  futures.add(future);
}
//future.get() is a blocking call. Only resume when all executions have been completed.
for (Future<Object> future : futures) {
  future.get();
}

return true;
// END USER CODE

The idea is to execute a microflow for each input object. This microflow should result in an updated object (so you only return a boolean, not some list of objects, as that increases complexity of the Java code). You call these microflows asynchronously through Java (Core.executeAsync()). These microflows result in futures. You iterate over the futures, and future.get() waits until the microflow has finished. This means you only continue once all microflows have finished.

 

Note: I have not actually tested this code, I recreated it from other projects where I have used it.

answered
1

More a stackoverflow question: https://stackoverflow.com/questions/51461491/calling-multiples-webservices-at-same-time-using-java

 

Regards,

Ronald

 

answered
0

Hi Wouter,

 

I have used this App store Module for one of the project to make Parallel execute

https://marketplace.mendix.com/link/component/116433

It worked as expected running 500 REST API calls multithreaded at a time.

answered