Problem

You want to report progress from the task to the client for monitoring purposes.

Solution

First, to receive the progress messages sent from the task to the client, subscribe to the job's addTaskProgressListener event or the task's addProgressUpdatedListener event.

Java  Copy imageCopy
job.addTaskProgressListener(new TaskProgressUpdatedListener() {
    public void onTaskProgressUpdated(Job job, TaskProgressEventArgs e) {
        System.out.println(String.format("%s %s", e.getDescription().getProgress(), e.getDescription().getAdditionalInformation()));
    }
});

Then, on the task, call setProgress(int, java.lang.Object). The values passed to SetProgress will be received on the client's progress event handling method.

Java  Copy imageCopy
this.setProgress(40, 4);
Java  Copy imageCopy
package stkscalabilitysdk.howto;


import agi.parallel.client.ClusterJobScheduler;
import agi.parallel.client.IJobScheduler;
import agi.parallel.client.Job;
import agi.parallel.client.TaskProgressEventArgs;
import agi.parallel.client.TaskProgressUpdatedListener;
import agi.parallel.infrastructure.Task;

public class TaskProgress {
    public static void main(String[] args) {
        IJobScheduler scheduler = new ClusterJobScheduler("localhost");
        try {
            scheduler.connect();

            Job job = scheduler.createJob();
            job.addTask(new ProgressTask());

            // Subscribe to TaskProgressUpdated
            job.addTaskProgressListener(new TaskProgressUpdatedListener() {
                public void onTaskProgressUpdated(Job job, TaskProgressEventArgs e) {
                    System.out.println(String.format("%s %s", e.getDescription().getProgress(), e.getDescription().getAdditionalInformation()));
                }
            });

            job.submit();
            job.waitUntilDone();
        } finally {
            scheduler.dispose();
        }

        /*
         * The output of the application should resemble:
         * 0 1
         * 20 B
         * 40 4
         * 60 D
         * 80 E
         */
    }

    public static class ProgressTask extends Task {
        @Override
        public void execute() {
            try {
                this.setProgress(0, 1);
                Thread.sleep(1000);
                this.setProgress(20, "B");
                Thread.sleep(1000);
                this.setProgress(40, 4);
                Thread.sleep(1000);
                this.setProgress(60, "D");
                Thread.sleep(1000);
                this.setProgress(80, "E");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Discussion

If a job could potentially take a long time to finish, reporting the progress of tasks while the task is running helps to notify the user how long the task could take. You can read more about working with task progress here.

To view the progress through the monitoring applications, open the task monitor (through the coordinator or agent tray application). Right click on the column headings and then click on "Status Description" on the drop down menu.

Show Status Description

Once the Status Description column is visible, the value of the task progress description can be seen. This is shown in the example above.

Monitor Task Progress UsingUI

See Also