Problem

You want to return a task result to the client.

Solution

Set the task's Result property with your user defined result object. The task's Result property is serialized and returned back to the client. The client can access the task's result by using the same Result property of the same task after the task completes.

Java  Copy imageCopy
package stkscalabilitysdk.howto;


import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

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

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

            Job job = scheduler.createJob();
            job.addTask(new ReturnsSimplevalue());
            job.addTask(new SetsValueOfProperties());
            job.addTask(new ReturnsJavaObject());
            job.submit();
            job.waitUntilDone();

            // Once the task completes, the task gets updated with the result object.
            System.out.println(String.format("Simple Value: Result=%s", job.getTasks().get(0).getResult()));
            System.out.println(String.format("Sets Property: A=%s, B=%s, C=%s", job.getTasks().get(1).getProperty("A"), job.getTasks().get(1).getProperty("B"), job.getTasks().get(1).getProperty("C")));

            AnotherJavaObject obj = (AnotherJavaObject) job.getTasks().get(2).getResult();
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss aaa");
            System.out.println(String.format("Complex Value: A=%s, B=%s", obj.getA(), dateFormat.format(obj.getB().getTime())));
        } finally {
            scheduler.dispose();
        }

        /*
         * The output of the application should resemble:
         * Simple Value: Result=3
         * Sets Property: A=1, B=2, C=3.14
         * Complex Value: A=String, B=2/28/2013 3:06:18 PM
         */
    }

    private static class ReturnsSimplevalue extends Task {
        @Override
        public void execute() {
            this.setResult(3);
        }
    }

    private static class SetsValueOfProperties extends Task {
        @Override
        public void execute() {
            this.setProperty("A", 1);
            this.setProperty("B", "2");
            this.setProperty("C", 3.14);
        }
    }

    private static class ReturnsJavaObject extends Task {
        @Override
        public void execute() {
            AnotherJavaObject object = new AnotherJavaObject();
            object.setA("String");
            object.setB(new GregorianCalendar());
            this.setResult(object);
        }
    }

    private static class AnotherJavaObject implements Serializable {
        public final String getA() {
            return a;
        }

        public final void setA(String value) {
            a = value;
        }

        public final GregorianCalendar getB() {
            return b;
        }

        public final void setB(GregorianCalendar value) {
            b = value;
        }

        private String a;
        private GregorianCalendar b;
    }
}

Discussion

Returning a value after the task executes is necessary if you need to process the result. You can return different types of results from a task including user defined objects. To read more about all these options, read Working with task results.

See Also