Using / Programming Pseudo Remote
Threads
Define a job
Write a class that implements the IJob
interface and hence the method execute()method. This
method is
the entry point
for the code that will get executed remotely. Instances of this class
will get moved around the network
using
serialization and hence its data members and the result returned should
be
serializable.
The source contains a sample job - aashish.prt.client.TestJob.
This job simply computes the
sum of square roots of the range of numbers inputted to it. Here
is the
source to it.
package aashish.prt.client;
import aashish.prt.common.IJob;
public class TestJob implements IJob
{
double start = 0;
double finish = 9;
double incr = 0.25;
public
Object execute()
{
double sum = 0;
for(double i = start;i <= finish; i = i + incr)
{
sum = sum + Math.sqrt(i);
}
return new Double(sum);
}
public void setPriority(int priority)
{
}
public int getPriority()
{
return 0;
}
public TestJob(double start,double finish,double
incr)
{
this.start = start;
this.finish = finish;
this.incr = incr;
}
}
The class constructor intializes the data members. The data members
specify the start and end numbers
between which the sum of square roots should be computed. The execute()
method is the one that actually
computes the sum. The execution of the job on the remote host will
begin from this method.
Create instances of the Job and hand them over to PRT.
TestJob job1 = new TestJob(10,100000,0.5);
TestJob job2 = new TestJob(100001,10000000,0.5);
Pseudo Remote Threads is abstracted in the interface
aashish.prt.client.IPrt. This interface has two
implementations -
synchronous (blocking) and asynchronous (non-blocking). The method to
perform remote execution of a job is
IPrt#executeJob(IJob). This method will pick the
best remote host to execute the job and execute it there.
For some reason, if its not possible to remotely execute the job, the
job is executed locally as a means of
failover.
For a synchronous (blocking) implementation of IPrt the executeJob()
method blocks till
the job is executed and the result returned to the caller.
//get blocking implementation of Prt.
IPrt blockPrt = PrtFactory.getBlockingPrt();
//call blocks
Double subresult1 = (Double) blockPrt.executeJob(job1);
//call blocks
Double subresult2 = (Double) blockPrt.executeJob(job2);
For an asynchronous (non-blocking) implementation of IPrt
the executeJob() method returns the control
immediately
to the caller. The caller should register a listener to be notified of
the completion
of the job execution.
The asynchronous gives true remote parallel execution. The synchronous
(blocking) implementation is
given for
completeness for developers that want to implement their own means of
parallelizing the remote job execution.
/*
* Listener class
*/
public class TestJobCompletionListener implements IJobCompleteListener
{
private double finalResult = 0;
int cntr = 0;
int maxNumJobs = 0;
public void jobComplete(JobCompleteEvent evtObj)
{
Double result = (Double)
evtObj.getResult();
finalResult +=
result.doubleValue();
System.out.println("TestScript:
jobComplete" + result);
cntr++
if(cntr == maxNumJobs)
//completed execution of all jobs
{
System.out.println("cntr: " + cntr);
System.out.println("Final result: " + finalResult);
}
}
public TestJobCompleteListener(int maxJobs)
{
maxNumJobs = maxJobs;
}
}// class TestJobCompleteListener
......
//caller program
IPrt prt = PrtFactory.getNonBlockingPrt();
TestJobCompleteListener listener = new TestJobCompleteListener(2);
//executeJob returns immediately and null since this is
non-blocking implementation.
prt.executeJob(job1);
prt.addJobCompleteListener(job1,listener);
prt.execute(job2);
prt.execute(job2,listener);
.....
Thats it!!!