Is it possible to call multiple microflows in parallel to call different webservices?

5
Hi, Currently, when creating an item in a to-do-list, some information is retrieved using 2 webservices. Each webservice may return: I have information about that item or I don't have information about that item. Each webservice takes some seconds, and because it's directly invoked after a microflow-button, the user has to wait for both results to be received. Having future development in view, we might expand the number of webservices (to 3 or 4) and the end-user is already impatient waiting for 2 webservices. Therefore, the question arises whether it would be possible to call those webservices (in submicroflows) in parallel (ASync)? And then continue when all submicroflows are finished. Something like: In my view, ProcessQueue is not a solution, as I want to process the result Synchronized to the end-user. I am not afraid to use Java Actions, as long as the stability is not compromised. Thanks in advance!
asked
3 answers
7

I do this in quite a few projects. You cannot do this in Mendix, so you need to revert to Java. I created a custom Java action called ExecuteMicroflowInParallel. It has the following inputs:

 * microflowName: String

 * inputName: String

 * objectList: List of AnyObject

(Aditionally, you could add String/Object pairs if your microflows require more objects. Add these to your parameters hashmap)

 

Then, use the following Java code

 

// BEGIN USER CODE
List<Future<Object>> futures = new ArrayList<Future<Object>>();
HashMap<String, Object> parameters = new HashMap<String,Object>();
IContext context = this.getContext();
for (IMendixObject object : objectList) {
	parameters.put(inputName, object);
	Future<Object> future = Core.executeAsync(context, microflowName, true, parameters);
	futures.add(future);
	parameters.clear();
}
		
for (Future<Object> future : futures) {
	future.get();
}
return true;
// END USER CODE

The result is parallel execution of microflows, which only continues once all calls have completed.

 

Obviously, you will have to rewrite the code if you want to call *different* microflows. I would suggest a list of objects which contain microflowNames strings. Then, loop over them and call the microflow in the string.

 

Furthermore, if you are concerned about stability, you could validate empty values and if the microflow actually exists with a call like Core.getMicroflowNames().contains(microflowName).

answered
1

I think the process queue can be a solution but you have to implement a timer on the user's interface to check if the webservice calls are finished. 

answered
0

I would worry if it's really a good idea to do this (maybe both calls are in fact handled by the same process in the external system, and it won't help at all).

If you're sure it is a good idea to do this it can be done. But be aware that concurrency is Hard. I would try to find a light weight Java library that provides specifically what you're looking for (trigger two threads at once, continue when both have finished).

answered