Running Microflows Asynchronously

3
How does one run Microflows simultaneously instead of running them in serie? I've already asked a forum question here, but thought that it would be better to start afresh and state what my goal is. I need to run 2 Microflows in Parallel.
asked
3 answers
4

Not sure what you are looking for exactly but I build something like that for a project I worked on a while back (microflow has 1 inputparameter of type "object") to run 3 data intensive flows in parallel (with the aim of dividing the load over multiple cpu's) maybe it is of help to you. A simplified version would be:

    List<Future<Object>> futures = new ArrayList<Future<Object>>();

    for (int i = 0; i < objectList.size(); i++) {
        Future<Object> f = Core.executeAsync(context, microflowName, objectList.get(i).getMendixObject());
        futures.add(f);
    }

    //wait for all async actions to finish
    //get will also throw an exception if an exception was thrown during the execution of an async action
    for (Future<Object> f: futures)
        f.get();

    //all async actions are finished
answered
0

Events on the domain model run in parallel

answered
0

To successfully execute a truly concurrent microflow you need to build a java class as follows:

package your.tools;

import java.util.Map; import com.mendix.systemwideinterfaces.core.IContext;

public class StartMicroflow implements Runnable{ private IContext context; private Map<string,object> paramaters; private String microflowName;

public StartMicroflow(IContext context, String microflowName, Map<String, Object> parameters){
    this.context = context.getSession().createContext();
    this.microflowName = microflowName;
    this.paramaters = parameters;
}
@Override
public void run() {
    try{
        com.mendix.core.Core.execute(this.context, this.microflowName,this.paramaters);
    }
    catch(com.mendix.core.CoreException ex){

    }
}

}

To call this class all you need are the current context, the microflows name and the microflows parameters to be called from a Java Action. The example does not have parameters but these can be added easily;

public Boolean executeAction() throws Exception
{
    // BEGIN USER CODE
    ExecutorService exec = Executors.newCachedThreadPool();
    exec.execute(new StartMicroflow (getContext(),this.microflowName, null));
    return true;
    // END USER CODE
}
answered