Creating a background task
The com.psddev.dari.util.Task class is the base task class. It contains basic execution control and status methods. For example, you can schedule start times, run once or repeatedly with periodic delays, pause and resume execution, and display status.
You can create a background task by extending the abstract Task
class and implementing your task logic in the Task#doTask
method. For example, in the following snippet for an anonymous class, a new MigrationTask
is created, appearing in the Migration category of the Task Status tool. For each execution of the doTask
method, the system increments the task’s run count by one.
public MigrationTask() {
task.submit();
}
Task task = new Task("Migration", "Migration Task") {
@Override
public void doTask() throws Exception {
boolean done = false;
while(!done && shouldContinue()) {
/* Do processing here */
}
}
}
-
Constructor that runs the
submit
method. This method starts the task immediately and runs it one time. -
Anonymous class that implements the
doTask
method. It is a best practice to callshouldContinue
on each iteration of the loop to determine if the task has stopped or paused in the Task Status tool.
As an alternative to the submit method, you can schedule a task to run repeatedly in periodic intervals. For details, see Schedule tasks.