Virtual Machinery logo Task Manager - Manual
Home Task Manager Simple example Manual Demo Other Products

Using the Task Manager classes

This page covers the available functionality in Virtual Machinery's Task manager classes -

Handling a sequence of tasks

Following from the code we implemented in the simple example we can add a second task to the set before running it. Create the TaskManager and myTask as per the simple example then run the following code -

Task myOtherTask = new Task();
myOtherTask.setSequenceNumber(2);
myOtherTask.setName("My Other Task");
Runnable otherCode = new Thread() {
  public void run() {
    System.out.println("Running code for My Other Task");
    try { Thread.sleep(5000);}
    catch (InterruptedException ie) {}
    System.out.println("Completed code for My Other Task");
  }
};
myOtherTask.setCode(otherCode);
myTaskManager.addTask(myTask);
myTaskManager.addTask(myOtherTask);

And now when we run the task manager

myTaskManager.startTasks(true);

We will see the following in the console -

Running code for My Task
Completed code for My Task
Running code for My Other Task
Completed code for My Other Task

For a slightly more complex example of this see the code in the NormalDemo class in com.virtualmachinery.taskmanager.demos.

When we want to handle an error

Sometimes we are in the situation when we don't want to run the second task if the first one fails. We do this by setting the 'fatal' flag on the first task i.e.

myTask.setFatal(true);

We can test this by setting the code for the task to some code that has an error in it e.g.

Runnable code = new Thread() {
  public void run() {
    System.out.println("Running code for My Task");
    String[] = new String[3];
    String[4] = "Error"; //This will generate an array out of bounds error
    System.out.println("Completed code for My Task");
  }
}

Now when we run the task manager we see -

Running code for My Task

i.e. the first task failed before it wrote that it was completed and the second task did not run.

Sometimes when there is an error we want to do something different. We can do this by setting the onFailureCode for the task e.g. -

Runnable failCode = new Thread() {
  public void run() {
    System.out.println("MyTask has failed");
  }
};
myTask.setOnFailureCode(failCode)

Our console output will now read -

Running code for My Task
MyTask has failed

This code will be run whether the task is marked as fatal or not.

We can also add code that will run if our task completes its code successfully -
Runnable successCode = new Thread() {
  public void run() {
    System.out.println("MyTask has been successful");
  }
};
myTask.setOnSuccessCode(failCode)

For examples of error handling see the code in the FatalErrorDemo and NonFatalErrorDemo class in com.virtualmachinery.taskmanager.demos.

Running tasks in parallel
Sometimes we have a number of tasks that it is safe enough to run sequentially - for example we might be printing reports on two different printers. Here is the code for the main method of the NormalDemo class in the com.virtualmachinery.taskmanager.demos.

/**
* Starts the application.
* @param args an array of command-line arguments
*/
public static void main(java.lang.String[] args) {

  TaskManagerInterface demoTaskManager = new TaskManager();
  show("Normal Demo - Demonstrates the features of the Task Manager using a normal scenario with 4 tasks, 1 with sequence number 1, 2 with 2 and 1 with 4.");

  try {
    demoTaskManager.addTask(createTask1());// Sequence 1, non-fatal, named TASK 1, wait 5ms
    demoTaskManager.addTask(createTask2a());// Sequence 2, non-fatal, named TASK 2a, wait 50ms
    demoTaskManager.addTask(createTask2b());// Sequence 2, non-fatal, named TASK 2b,wait 5ms
    demoTaskManager.addTask(createTask4());// Sequence 4, non-fatal, named TASK 4, wait 5ms
    demoTaskManager.startTasks(true);
  }
  catch (TaskManagerFatalFailureException te) {
    show("Fatal Failure - task execution halted "+te);
  }
  catch (TaskManagerException te) {
    show("Exception adding tasks in main "+te);
  }

  showStatus(demoTaskManager);
}

Other features

Getting the execution time
You can find out how long a task has been executing for (or how long it took if it has finished) by calling the getExecutionTime() method on the task.

Stopping tasks
When you stop the tasks the current task (or tasks if there is a group with the same sequence number) will complete. You can stop the tasks by calling the stopTasks() method No further processing will be carried out and the startTasks() method will exit. See
StopCodeDemo class in com.virtualmachinery.taskmanager.demos.

Pausing the tasks
When you pause the tasks the current task (or tasks if there is a group with the same sequence number) will complete. Further processing will then be paused. There are two ways to pause the tasks. One is to call the pauseTasks() method then when you want to restart call the reStartTasksAfterPause(). The other is to call pauseTasks(X) where X is the amount of time in milliseconds that you wish to pause the tasks for. See
PauseDemo and TimedPauseDemo classes in com.virtualmachinery.taskmanager.demos

Restarting the tasks
If the tasks have stopped for any reason (i.e. if you called stopTasks() or if a fatal task failed) you can restart the tasks by calling restartTasks(retryFailure). retryFailure should be true if you want to retry any failed tasks false if you don't. Tasks that have been successful completed will not be retried. See
RestartDemo class in com.virtualmachinery.taskmanager.demos.