1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.engine.cluster.drmaa;
\r
20 import java.util.Collections;
\r
21 import java.util.Iterator;
\r
22 import java.util.LinkedList;
\r
23 import java.util.List;
\r
25 import org.ggf.drmaa.DrmCommunicationException;
\r
26 import org.ggf.drmaa.DrmaaException;
\r
27 import org.ggf.drmaa.JobInfo;
\r
28 import org.ggf.drmaa.JobTemplate;
\r
29 import org.ggf.drmaa.Session;
\r
30 import org.ggf.drmaa.SessionFactory;
\r
32 class _DrmaaExample {
\r
34 private static int NBULKS = 3;
\r
35 private static int JOB_CHUNK = 8;
\r
36 private static Session session = null;
\r
38 public static void main(String[] args) throws Exception {
\r
39 String jobPath = args[0];
\r
41 SessionFactory factory = SessionFactory.getFactory();
\r
43 session = factory.getSession();
\r
46 JobTemplate jt = createJobTemplate(jobPath, 5, true);
\r
48 List allJobIds = new LinkedList();
\r
50 boolean retry = true;
\r
52 for (int count = 0; count < NBULKS; count++) {
\r
55 jobIds = session.runBulkJobs(jt, 1, JOB_CHUNK, 1);
\r
57 } catch (DrmCommunicationException e) {
\r
58 System.err.println("runBulkJobs() failed - retry: "
\r
65 allJobIds.addAll(jobIds);
\r
67 System.out.println("submitted bulk job with jobids:");
\r
69 Iterator i = jobIds.iterator();
\r
71 while (i.hasNext()) {
\r
72 System.out.println("\t \"" + i.next() + "\"");
\r
76 session.deleteJobTemplate(jt);
\r
78 /* submit some sequential jobs */
\r
79 jt = createJobTemplate(jobPath, 5, false);
\r
81 String jobId = null;
\r
84 for (int count = 0; count < JOB_CHUNK; count++) {
\r
87 jobId = session.runJob(jt);
\r
89 } catch (DrmCommunicationException e) {
\r
90 System.err.println("runBulkJobs() failed - retry: "
\r
97 System.out.println("\t \"" + jobId + "\"");
\r
98 allJobIds.add(jobId);
\r
101 session.deleteJobTemplate(jt);
\r
103 /* synchronize with all jobs */
\r
104 session.synchronize(allJobIds, Session.TIMEOUT_WAIT_FOREVER, false);
\r
105 System.out.println("synchronized with all jobs");
\r
107 /* wait all those jobs */
\r
108 Iterator i = allJobIds.iterator();
\r
110 while (i.hasNext()) {
\r
111 JobInfo status = null;
\r
112 jobId = (String) i.next();
\r
114 status = session.wait(jobId, Session.TIMEOUT_WAIT_FOREVER);
\r
116 /* report how job finished */
\r
117 if (status.wasAborted()) {
\r
118 System.out.println("job \"" + jobId + "\" never ran");
\r
119 } else if (status.hasExited()) {
\r
120 System.out.println("job \"" + jobId
\r
121 + "\" finished regularly with exit status "
\r
122 + status.getExitStatus());
\r
123 } else if (status.hasSignaled()) {
\r
124 System.out.println("job \"" + jobId
\r
125 + "\" finished due to signal "
\r
126 + status.getTerminatingSignal());
\r
128 System.out.println("job \"" + jobId
\r
129 + "\" finished with unclear conditions");
\r
134 private static JobTemplate createJobTemplate(String jobPath, int seconds,
\r
135 boolean isBulkJob) throws DrmaaException {
\r
136 JobTemplate jt = session.createJobTemplate();
\r
138 jt.setWorkingDirectory("$drmaa_hd_ph$");
\r
139 jt.setRemoteCommand(jobPath);
\r
140 jt.setArgs(Collections.singletonList(Integer.toString(seconds)));
\r
141 jt.setJoinFiles(true);
\r
144 jt.setOutputPath(":$drmaa_hd_ph$/DRMAA_JOB");
\r
146 jt.setOutputPath(":$drmaa_hd_ph$/DRMAA_JOB$drmaa_incr_ph$");
\r