Click or drag to resize

Return a value from a task

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
package stkparallelcomputingserversdk.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) {
        try (IJobScheduler scheduler = new ClusterJobScheduler("localhost")) {
            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()));
            Task task1 = job.getTasks().get(1);
            System.out.format("Sets Property: A=%s, B=%s, C=%s%n",
                    task1.getProperty("A"),
                    task1.getProperty("B"),
                    task1.getProperty("C"));

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

        /*
         * 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 {
        private String a;
        private GregorianCalendar b;

        public final String getA() {
            return this.a;
        }

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

        public final GregorianCalendar getB() {
            return this.b;
        }

        public final void setB(GregorianCalendar value) {
            this.b = value;
        }
    }
}
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

STK Parallel Computing Server 2.9 API for Java