I want to start a microflow with a delay from another microflow, but I want to keep the webpage useable during the delay time. This is why I had the idea to start the new microflow in a separate thread. The first microflow calls a JavaAction. In the Java code, a new Runnable is created and this runnable starts the 2nd microflow in a separate thread with a delay of 5 seconds. The new microflow just logs a message to the console and shows a message to the user. The console logging works as expected. The page remains useable during the 5 second delay and then a message appears in the console. However the message to the user does not appear. In general, no client action ever appears to the user. I also tried starting the 2nd microflow from the same thread with a delay. In this case the page is blocked for the 5 seconds, just as expected, but then the user message is also shown. So it seems the problem with the webpage not showing any changes happens only because the 2nd microflow is started from another thread. I am using the Core.execute function to run the 2nd microflow and I am passing the original IContext (this.context()), so it should have the same userdata and session, but it seems that somehow the new thread cannot run in the same context. Here’s my Java source code:
package myfirstmodule.actions;
import com.mendix.core.Core;
import com.mendix.core.CoreException;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import java.util.HashMap;
import java.util.Map;
public class JavaCallMicroflow extends CustomJavaAction<java.lang.Boolean>
{
private java.lang.String MicroflowName;
public JavaCallMicroflow(IContext context, java.lang.String MicroflowName)
{
super(context);
this.MicroflowName = MicroflowName;
}
@java.lang.Override
public java.lang.Boolean executeAction() throws Exception
{
// BEGIN USER CODE
Core.getLogger("Logger").info("Starting JavaCallMicroflow");
// single-threaded
// Thread.sleep(5000);
// runMicroflow(MicroflowName, this.context());
// multi-threaded
Thread thread = new Thread(new MicroflowRunnable(MicroflowName, this.context()));
thread.start();
return true;
// END USER CODE
}
/**
* Returns a string representation of this action
*/
@java.lang.Override
public java.lang.String toString()
{
return "JavaCallMicroflow";
}
// BEGIN EXTRA CODE
private class MicroflowRunnable implements Runnable {
private IContext context;
private String microflowName;
public MicroflowRunnable(String microflowName, IContext context) {
this.context = context;
this.microflowName = microflowName;
}
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Core.getLogger("Logger").error(e.getStackTrace());
}
try {
Core.getLogger("Logger").info("Starting microflow from separate thread.");
runMicroflow(microflowName, context);
} catch (CoreException e) {
Core.getLogger("Logger").error(e.getStackTrace());
}
}
}
private static void runMicroflow(String microflowName, IContext context) throws CoreException
{
Map<String,Object> parameters = new HashMap<String, Object>();
Core.execute(context, "MyFirstModule." + microflowName, parameters);
}
// END EXTRA CODE
}
↧