Javadoc fixes
[jabaws.git] / engine / compbio / engine / cluster / drmaa / _DrmaaExample.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \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
7  * \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
11  * \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
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.engine.cluster.drmaa;\r
19 \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
24 \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
31 \r
32 class _DrmaaExample {\r
33 \r
34         private static int NBULKS = 3;\r
35         private static int JOB_CHUNK = 8;\r
36         private static Session session = null;\r
37 \r
38         public static void main(String[] args) throws Exception {\r
39                 String jobPath = args[0];\r
40 \r
41                 SessionFactory factory = SessionFactory.getFactory();\r
42 \r
43                 session = factory.getSession();\r
44                 session.init(null);\r
45 \r
46                 JobTemplate jt = createJobTemplate(jobPath, 5, true);\r
47 \r
48                 List allJobIds = new LinkedList();\r
49                 List jobIds = null;\r
50                 boolean retry = true;\r
51 \r
52                 for (int count = 0; count < NBULKS; count++) {\r
53                         do {\r
54                                 try {\r
55                                         jobIds = session.runBulkJobs(jt, 1, JOB_CHUNK, 1);\r
56                                         retry = false;\r
57                                 } catch (DrmCommunicationException e) {\r
58                                         System.err.println("runBulkJobs() failed - retry: "\r
59                                                         + e.getMessage());\r
60 \r
61                                         Thread.sleep(1000);\r
62                                 }\r
63                         } while (retry);\r
64 \r
65                         allJobIds.addAll(jobIds);\r
66 \r
67                         System.out.println("submitted bulk job with jobids:");\r
68 \r
69                         Iterator i = jobIds.iterator();\r
70 \r
71                         while (i.hasNext()) {\r
72                                 System.out.println("\t \"" + i.next() + "\"");\r
73                         }\r
74                 }\r
75 \r
76                 session.deleteJobTemplate(jt);\r
77 \r
78                 /* submit some sequential jobs */\r
79                 jt = createJobTemplate(jobPath, 5, false);\r
80 \r
81                 String jobId = null;\r
82                 retry = true;\r
83 \r
84                 for (int count = 0; count < JOB_CHUNK; count++) {\r
85                         while (retry) {\r
86                                 try {\r
87                                         jobId = session.runJob(jt);\r
88                                         retry = false;\r
89                                 } catch (DrmCommunicationException e) {\r
90                                         System.err.println("runBulkJobs() failed - retry: "\r
91                                                         + e.getMessage());\r
92 \r
93                                         Thread.sleep(1000);\r
94                                 }\r
95                         }\r
96 \r
97                         System.out.println("\t \"" + jobId + "\"");\r
98                         allJobIds.add(jobId);\r
99                 }\r
100 \r
101                 session.deleteJobTemplate(jt);\r
102 \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
106 \r
107                 /* wait all those jobs */\r
108                 Iterator i = allJobIds.iterator();\r
109 \r
110                 while (i.hasNext()) {\r
111                         JobInfo status = null;\r
112                         jobId = (String) i.next();\r
113 \r
114                         status = session.wait(jobId, Session.TIMEOUT_WAIT_FOREVER);\r
115 \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
127                         } else {\r
128                                 System.out.println("job \"" + jobId\r
129                                                 + "\" finished with unclear conditions");\r
130                         }\r
131                 }\r
132         }\r
133 \r
134         private static JobTemplate createJobTemplate(String jobPath, int seconds,\r
135                         boolean isBulkJob) throws DrmaaException {\r
136                 JobTemplate jt = session.createJobTemplate();\r
137 \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
142 \r
143                 if (!isBulkJob) {\r
144                         jt.setOutputPath(":$drmaa_hd_ph$/DRMAA_JOB");\r
145                 } else {\r
146                         jt.setOutputPath(":$drmaa_hd_ph$/DRMAA_JOB$drmaa_incr_ph$");\r
147                 }\r
148 \r
149                 return jt;\r
150         }\r
151 \r
152 }\r