Multi-tree abstract task dependency and cross-tree Abstraction
When the system is idle, it can run some tasks to perform some statistical operations. Most of them are data retrieval, data insertion, and statistical data. These tasks are sometimes dependent. For example, if task A is counted as dependent on Task B, Task B is scheduled to run at the beginning of the year, and task A runs, however, if Task B cannot be completed within one hour, A problem may occur when running task. You must ensure that task B is completed before running task.
Therefore, we can consider using a linked list data structure to concatenate tasks for execution, and ensure that the business of each task is separated on the Code layer.
For example:
Public abstract class BaseTask {private String id; public abstract void execute (); public String getId () {return id;} public void setId (String id) {this. id = id ;}}
SimpleBaseTask:
Public abstract class SimpleBaseTask extends BaseTask {public void execute () {beforeExecute (); doTask (); doNextTask (); afterExecute ();} public void doNextTask () {if (nextTasks! = Null) {for (BaseTask task: nextTasks) {task.exe cute () ;}} public abstract void beforeExecute (); public abstract void afterExecute (); public abstract void doTask (); private String name; private BaseTask parentTask; // each task can have multiple subsequent tasks. These tasks depend on the execution completion of this task. private List <BaseTask> nextTasks; public String getName () {return name ;} public void setName (String name) {this. name = name;} public BaseTask getParentTask () {return parentTask;} public void setParentTask (BaseTask parentTask) {this. parentTask = parentTask;} public List <BaseTask> getNextTasks () {return nextTasks;} public void setNextTasks (List <BaseTask> nextTasks) {this. nextTasks = nextTasks ;}}
ProcessGroup:
Public abstract class ProcessGroup <T extends BaseTask >{// the first task logic private BaseTask firstTask executed by each task process; public BaseTask getFirstTask () {return firstTask ;} public void setFirstTask (BaseTask firstTask) {this. firstTask = firstTask;} public abstract void beforeProcess (); public abstract void afterProcess (); public void doProcess () {beforeProcess (); firstTask.exe cute (); afterProcess ();}}
SimpleProcessGroup :
public class SimpleProcessGroup extends ProcessGroup<SimpleBaseTask>{ @Override public void beforeProcess() { System.out.println("beforeProcess"); } @Override public void afterProcess() { System.out.println("afterProcess"); }}
This abstraction has too many limitations. concatenating all tasks will cause tasks to be manually split, and dependencies are embedded into code, which is difficult to maintain. In addition, the multi-task dependency still does not meet the requirements.
Therefore, a more powerful task scheduling system is required to implement this flexible task dependency.