Job Orchestration - Cross Service
Introduction
This guide demonstrates how to run jobs on multiple GRIA Job Services, using the output from one job as input to the next.
This code requires the TutorialHelpers.java class previously discussed, and the job helper class.
The full Java file for job orchestration can be downloaded.
Job Orchestration Guide
Create a TutorialHelpers object and get a ProxyFactory object from it:
ProxyFactory proxyFactory = new TutorialHelpers().getFactory();
Create a Proxy to the Job Service:
RemoteJobService jobService1 = proxyFactory.createProxy(ConversationID.getEPR(JOB_SERVICE_ENDPOINT1), RemoteJobService.class);
Use the JobHelpers class to create a JSDL job description using the defaults for this type of job:
JobDescription jsdl = JobHelpers.getJobDescription("Job 1",jobService1.getApplicationMetadataDetailed(APPLICATION1));
Make a remote call to the Job Service to create a job resource using the JSDL we have created:
JobConversation jobConv1 = jobService1.createJobJSDL(jsdl.toXML());
Get Proxy to the inputs and outputs of the job as DataConversation objects:
DataConversation[] inputs1 = jobConv1.getInputs();
DataConversation[] outputs1 = jobConv1.getOutputs();
Upload an input image to the input data stager:
URL url = JobOrchestration.class.getClassLoader().getResource(INPUT_FILE);
inputs1[0].save(new DataHandler(url));
Tell the Job Service to start the job:
jobConv1.submitJobJSDL();
while(jobConv1.stillActive()) {
Thread.sleep(5000);
}
Check to see if the job completed sucessfully:
JobStatus jobStatus = jobConv1.checkJob();
if(jobStatus.getExitStatus() != 0) {
throw new RuntimeException("Swirl job failed. Here is the log:\n" + jobStatus.getLogText());
}
Create another proxy to a different jobservice:
RemoteJobService jobService2 = proxyFactory.createProxy(ConversationID.getEPR(JOB_SERVICE_ENDPOINT2), RemoteJobService.class);
Use the JobHelpers class to create a JSDL job description using the defaults for this type of job and follow the same process as before for job 1:
JobDescription jsdl2 = JobHelpers.getJobDescription("Job 2",jobService2.getApplicationMetadataDetailed(APPLICATION2));
JobConversation jobConv2 = jobService2.createJobJSDL(jsdl2.toXML());
DataConversation[] inputs2 = jobConv2.getInputs();
DataConversation[] outputs2 = jobConv2.getOutputs();
Add a rule on the output of job 1 allowing the second job service to read from the stager:
outputs1[0].addPolicyRule(new PolicyRule(new MatchPattern(jobService2.getServiceProviderID(),
jobService2.getServiceProviderIssuer()),"reader"));
Instruct the second Job Service to copy the output of job 1 into the input of job 2:
inputs2[0].copyFrom(outputs1[0]);
Tell the Job Service to start the job and monitor it:
jobConv2.submitJobJSDL();
while(jobConv2.stillActive()) {
Thread.sleep(5000);
}
JobStatus jobStatus2 = jobConv2.checkJob();
if(jobStatus2.getExitStatus() != 0) {
throw new RuntimeException("Job failed. Here is the log:\n" +jobStatus2.getLogText());
}
outputs2[0].read(new File(OUTPUT_FILE));
Clean up the jobs at each service:
jobConv1.destroy();
jobConv2.destroy();