Call microflow with delay with java

1
I have a microflow which commit data of an object. But for performance issues I call this microflow with a delay. The delay is created with a custom Java thread. The thread will sleep for a time and then calls the microflow. The problem is that the changes in the object not will saved and the applicatie runs down. Is there any faults in mine code or is there a better solution? public class ThreadDelay implements Runnable { private IContext context; private Header header; private Thread thread; ThreadDelay(IContext context, Header header) { this.context = context; this.header = header; thread = new Thread(this); thread.start(); } public void run() { try { // Sleep the thead. Thread.sleep(5000); // Add the objects to the parameters to start a microflow. Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("Header", header.getMendixObject()); // Start the Microflow Core.execute(context, "ABC.Call", parameters); thread.interrupt(); } catch (CoreException e) { } catch (InterruptedException e) { // The thread was already started. } } }
asked
3 answers
2

The transaction might have already ended before this delay is triggered. You could also try adding some logging before the try to see if it's actually executed.

I'd also suggest not leaving the catches empty, at least add some logging to signal that something went wrong.

answered
2

Thread.sleep(5000) is enough to delay. If you want to run the MF in another thread (so the original calling continues), you should the functions the Core provides for this (Core.executeASync). No need to implement your own thread.

I guess the problem now is that the original microflow continues, and ends transaction, causing your action to make changes in a transaction that will never be committed (since that has been done already).

answered
2

Achiel you were right with the transaction. After the delay the transaction is still available and usable, but is not very good in using that transaction. The delay will be 15 minutes.

The point what went wrong is that after executing the microflow, the transaction was still open and not saved to the database. The thread went down and all information in the transaction was lost. The solution is, before that the thread runs down, end the transaction so that all changes will be saved.

Core.execute(context, "ABC.Call", parameters);
context.endTransaction();
answered