Workflow Tutorial 4 - Using SLA's
Introduction
The learning objects from this excercise are:
- Learn how to use an SLA to gain access to a service.
Step 1: Create a Proxy to the SLA Service
StateRepository repository = new MemoryStateRepository(); RemoteSLAService slaService = (RemoteSLAService) repository.getOrCreateObject(RemoteSLAService.class,ConversationID.getEPR(SLA_SERVICE_ENDPOINT));
Step 2: Find the available resources on the SLA Service
The SLA Service will return a list of Endpoint References for resources. Some of these will be SLA templates and some will be SLA's. You need to check the type of the Endpoint Reference using ConversationID.getType(EndpointReference epr).
EndpointReferenceType[] eprs = slaService.getResources();
SLAConversation sla = null;
for(EndpointReferenceType epr : eprs){
if(ConversationID.getType(epr).equals(SLAConversation.class.getName()))
sla = (SLAConversation)repository.getOrCreateObject(SLAConversation.class ,epr);
}
if(sla == null){
System.out.println("No SLA Found");
System.exit(1);
}
Step 3: Create a client proxy to the Job Service and invoke createJob
The second parameter to the createJob request should be the EPR of the SLA you wish to use. Then start the job in the usual way.
RemoteJobService jobService =
(RemoteJobService) repository.getOrCreateObject(RemoteJobService.class,ConversationID.getEPR(JOB_SERVICE_ENDPOINT));
// Create a Job
JobConversation jobConv = jobService.createJob(APPLICATION, sla.getEndpointRef() , "");
DataConversation[] inputs = jobConv.getInputs();
DataConversation[] outputs = jobConv.getOutputs();
inputs[0].save(new DataHandler(new FileDataSource(INPUT_FILE)));
jobConv.submitJob(null, new String[]{});
while(jobConv.stillActive()) {
System.out.println(".");
Thread.sleep(2000);
}
JobStatus jobStatus = jobConv.checkJob();
if(jobStatus.getExitStatus() != 0) {
// handle the failure
throw new RuntimeException("Job failed. Here is the log:\n" +
jobStatus.getLogText());
}
outputs[0].read(new File(OUTPUT_FILE));
jobConv.finish();
Completed Java
import java.io.File;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import uk.ac.soton.ecs.iam.grid.comms.client.StateRepository;
import uk.ac.soton.ecs.iam.grid.client.staterepos.MemoryStateRepository;
import uk.ac.soton.ecs.iam.grid.comms.client.DataConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.JobConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.SLAConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteJobService;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteSLAService;
import uk.ac.soton.itinnovation.grid.service.types.JobStatus;
import uk.ac.soton.itinnovation.grid.types.ConversationID;
import org.apache.axis.message.addressing.EndpointReferenceType;
public class UsingSLAsCompleted {
/** Job Service Endpoint */
private static final String JOB_SERVICE_ENDPOINT = "https://.../gria-basic-app-services/services/JobService";
/** SLA Service Endpoint */
private static final String SLA_SERVICE_ENDPOINT = "https://.../gria-service-provider-mgt/services/SLAService";
/** The application to execute, identified by its URI */
private static final String APPLICATION = "http://it-innovation.soton.ac.uk/grid/imagemagick/paint";
/** The input file to upload to the job */
private static final String INPUT_FILE = "data/input-image.png";
/** The ouptut file to download the results to */
private static final String OUTPUT_FILE = "data/output-image-ex4.png";
public static void main(String[] args) throws Exception {
StateRepository repository = new MemoryStateRepository();
// Create the First Job Service Proxy
RemoteJobService jobService = (RemoteJobService)
repository.getOrCreateObject(RemoteJobService.class,ConversationID.getEPR(JOB_SERVICE_ENDPOINT));
RemoteSLAService slaService = (RemoteSLAService)
repository.getOrCreateObject(RemoteSLAService.class,ConversationID.getEPR(SLA_SERVICE_ENDPOINT));
EndpointReferenceType[] eprs = slaService.getResources();
SLAConversation sla = null;
System.out.println("Searching for SLA's to use");
for(EndpointReferenceType epr : eprs){
if(ConversationID.getType(epr).equals(SLAConversation.class.getName()))
sla = (SLAConversation)repository.getOrCreateObject(SLAConversation.class,epr);
}
if(sla == null){
System.out.println("No SLA's Found");
System.exit(1);
}else{
System.out.println("SLA Found '"+sla+"'");
}
// Create a Job
JobConversation jobConv = jobService.createJob(APPLICATION, sla.getEndpointRef(), "");
DataConversation[] inputs = jobConv.getInputs();
DataConversation[] outputs = jobConv.getOutputs();
System.out.println("Uploading input '"+INPUT_FILE+"'");
inputs[0].save(new DataHandler(new FileDataSource(INPUT_FILE)));
jobConv.submitJob(null, new String[]{});
System.out.print("Waiting for Job ");
while(jobConv.stillActive()) {
System.out.print(".");
Thread.sleep(2000);
}
System.out.println(" Done!");
JobStatus jobStatus = jobConv.checkJob();
if(jobStatus.getExitStatus() != 0) {
// handle the failure
throw new RuntimeException("Job failed. Here is the log:\n" +
jobStatus.getLogText());
}
System.out.println("Downloading output to '"+OUTPUT_FILE+"'");
outputs[0].read(new File(OUTPUT_FILE));
jobConv.finish();
}
}
