How to use dexecutor to model your requirements in terms of object-oriented graphs in order to define execution order.
Join the DZone community and get the full member experience.Join For Free
From the dexecutor website:
Executing dependent/independent tasks in a reliable way is made so easy that even grandma can do it.
That is indeed true with dexecutor, especially considering the complexity involved in writing error-free programs that use dependent and independent tasks. Without dexecutor, you would end up writing tons of plumbing code rather than concentrating on the business requirements.
With dexecutor, you model your requirements in terms of graphs in an object-oriented way. The rest is taken care of by the framework in a reliable way. Dynamically built graphs define the execution order. For example if the following graph is built:
Then it means, task#1, task#12, and task#11 would run in parallel. Once one of them finishes their execution, its child nodes would begin their executions. For example, lets say if task#1 finishes, then task#2 and task#3 would begin, similarly with task#12 and task#11, until all the tasks are executed or if a task ends up in an error (if the execution behavior is terminating).
That’s great indeed, but how it is done ?
DefaultDependentTasksExecutor<Integer, Integer> executor = newTaskExecutor(); //Build the graph executor.addDependency(1, 2); executor.addDependency(1, 3); executor.addDependency(3, 4); executor.addDependency(3, 5); executor.addDependency(3, 6); //executor.addDependency(10, 2); // cycle executor.addDependency(2, 7); executor.addDependency(2, 9); executor.addDependency(2, 8); executor.addDependency(9, 10); executor.addDependency(12, 13); executor.addDependency(13, 4); executor.addDependency(13, 14); executor.addIndependent(11); // Execute executor.execute(ExecutionBehavior.NON_TERMINATING);
Above code shows, dexecutor expose two kind of APIs:
- An API to construct the graph (addDependency, addIndependent)
- An API to start execution (execute)
That’s simple indeed, you may be wondering, how the tasks are mapped?
TaskProvider to the rescue. TaskProvider maps a graph node to a task, and does this when a Dexecutor instance is created. Refer to the Java doc for dexecutor implementation, and for an example of how to do it.
Well, the example is very simple, so can we get any real-time examples of how dexecutor can be used?
Yes indeed, dexecutor does have a sample application which provides a real time scenario, refer to it for more details.
Finally I would like to quote the features of dexecutor, here is a snapshot from the website:
Published at DZone with permission of Mohammad Nadeem , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.