After stating the problem at the overview,
and constructing the decompose task in step
1, let's continue with the construction of the compose task.
This task should implement the following functionality:
"take the two input arguments, that correspond to fib(num-1) and
fib(num-2) and return the combined result, i.e. in our case the sum fib(num-1)+fib(num-2)"
Before you proceed any further, make sure you have read the CX
API, that explains how a task interacts with the CX environment throughout
the TaskContainer interface.
Here is how it looks like :
public class FibTaskCombine
extends CXTask {
// required constructor!
public
FibTaskCombine(TaskHeader taskHeader) {
super(taskHeader);
}
// custom constructor(s)
public FibTaskCombine()
{
//the rest parameters will be set by your task container
inputArguments=new Integer[2];
missingInputArguments=2;
outputDestination=new DestinationRecord[1]; // the destination for the
result of this task's execution
}
// that's the only
method you must override
public
void executeTask(TaskContainer ts)
{
// get input arguments
//in our case, input arguments correspond to the output of the fib(num-1)
and fib(num-2) tasks
Integer fibNumber1= (Integer) inputArguments[0];
Integer fibNumber2= (Integer) inputArguments[1];
int fnum1=fibNumber1.intValue();
int fnum2=fibNumber2.intValue();
System.out.println("FibTaskcombine:"+fnum1+","+fnum2+" ...");
TaskInfo taskInfo;
Sequence computedBy;
long argPos;
long jobId=0;
Integer[] result=new Integer[1];
if (outputDestination[0].destinationTask!=null)
// this means that the results are intermediate - will be send to another
task
{
taskInfo=new TaskInfo(outputDestination[0].destinationTask);
argPos=outputDestination[0].inArgPos;
jobId=taskInfo.jobId;
}
else
{
// this means that the results are final - will be send to the originator
taskInfo=null;
jobId=myTaskInfo.jobId;
argPos=0;
}
result[0]=new Integer(fnum1+fnum2); // since fib(num)=fib(num-1)+f(num-2)
// fnum1--->fib(num-1)
// fnum2--->fib(num-2)
computedBy=getComputedBy();
Result res=new Result(taskInfo,computedBy,argPos,result);
res.jobId=jobId;
//finally, return the results to your task container
ts.storeResults(res);
}
}
It is much simpler than the decompose methods, because it only returns
results to its Task Container, it doesn't create new tasks. Like we mentioned
in the decompose case, make sure that you always:
-
include the required constructor, with TaskHeader as an input parameter
-
you overwrite the executeTask method, in order to do something useful.
Of course, in addition to those, you can add as many constructors/methods
as you like.