public ClientId( Long id )
{
this.id = id;
}
}
public JobId( Long id )
{
this.id = id;
}
}
public Task( ClientId clientId, JobId jobId )
{
this.clientId = clientId;
this.jobId = jobId;
}
public void execute( TaskServerProxy taskServer
) {}
}
public class HelloworldTask extends Task
{
public void execute( TaskServerProxy taskServer
) // result: host's domain name
{
String result = null;
try
{
result = InetAddress.getLocalHost().getHostName();
taskServer.putTask( new HelloworldResult( result ) );
}
catch (UnknownHostException
exception)
{
System.err.println("Exception: " + exception.toString());
}
catch (RemoteException
e)
{
System.err.println("Exception: " + exception.toString());
}
}
}
public class HelloworldResult extends Task
{
String result;
public HelloworldResult( ClientId clientId, JobId
jobId, String result )
{
super( clientId, jobId
);
this.result = result;
}
}
public class TaskServerProxy
{
TaskServer taskServer;
public TaskServerProxy()
{
// Find a TaskServer;
get its remote reference.
//
This may involve a Jini lookup, or some other method.
}
public Task getTask( Task template )
{
Task task;
try
{
return task = taskServer.getTask( template );
}
catch ( RemoteException
e )
{
}
}
public void putTask( Task task )
{
//store task in local
cache
//store task in (remote)
TaskServer
try
{
taskServer.putTask( task );
}
catch ( RemoteException
e )
{
}
}
}
public class TaskServer
{
JavaSpace javaSpace;
public TaskServer()
{
javaSpace = new JavaSpace();
}
public Task getTask( Task template )
{
try
{
return Task = (Task) javaSpace.take( template, ... );
}
catch ( RemoteException
e )
{
}
}
public void putTask( Task task )
{
try
{
javaSpace.write( task, ... );
}
catch ( RemoteException
e )
{
}
}
}
public class Client
{
JobId jobId;
ClientId clientId;
TaskServerProxy taskServer;
HelloworldResult result;
public Client()
{
clientId = new ClientId(
7 ); // Something more sophisticated to come.
jobId = new JobId( 3
);
HelloworldResult resultTemplate
= new HelloworldResult();
try
{
taskServer = new TaskServerProxy();
for (int i = 0; i < 10; i++)
taskServer.putTask( new HelloWorldTask( clientId, jobId ) );
for (int i = 0; i < 10; i++)
{
result = taskServer.getTask( resultTemplate );
System.out.println("Result: " + result.result);
}
}
catch (Exception e)
{
System.out.println("Host: TaskServerProxy construction: failed.");
e.printStackTrace();
System.exit(-1);
}
}
public static void main(String[] args)
{
Client client
= new Client();
}
}
public class Host implements Runnable
{
TaskServerProxy taskServer;
Task task, result;
Transaction txn;
public Host()
{
try
{
taskServer = new TaskServerProxy();
}
catch (Exception e)
{
System.out.println("Host: TaskServerProxy construction: failed.");
e.printStackTrace();
System.exit(-1);
}
}
public static void main(String[] args)
{
Host host
= new Host();
}
public void run()
{
while (true)
{
txn = getTransaction();
if ( txn == null )
throw new RuntimeException( "Host: Can't obtain a transaction.");
try
{
task = taskServer.getTask( null ); // template _really_ should only allow
non-Result type tasks.
result = task.execute( taskServer ); //reference to proxy, so task can
putTask & putArg
if ( result != null )
taskServer.putResult( result );
txn.commit();
}
catch (RemoteException e )
{
e.printStackTrace();
}
catch (TransactionException e )
{
e.printStackTrace();
}
catch (InterupptedException e )
{
try
{
txn.abort();
}
catch (Exception ex)
{
//RemoteException of BadTransactionException
//lease expiration takes care of transaction
}
System.out.println("Task cancelled.");
}
}
}
}