Return a value from a task


You want to return a task result to the client.


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.

package stkscalabilitysdk.howto;

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 {

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

            // 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 {

         * 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 {
        public void execute() {

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

    private static class ReturnsJavaObject extends Task {
        public void execute() {
            AnotherJavaObject object = new AnotherJavaObject();
            object.setB(new GregorianCalendar());

    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;

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

STK Scalability 1.4 API for Java