1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.jes.jobscheduler.dispatcher;
12
13 import org.astrogrid.applications.delegate.CommonExecutionConnectorClient;
14 import org.astrogrid.applications.delegate.DelegateFactory;
15 import org.astrogrid.component.descriptor.ComponentDescriptor;
16 import org.astrogrid.jes.JesException;
17 import org.astrogrid.jes.delegate.JesDelegateFactory;
18 import org.astrogrid.jes.delegate.JobMonitor;
19 import org.astrogrid.jes.jobscheduler.Dispatcher;
20 import org.astrogrid.jes.jobscheduler.Locator;
21 import org.astrogrid.jes.types.v1.cea.axis.JobIdentifierType;
22 import org.astrogrid.jes.util.JesUtil;
23 import org.astrogrid.workflow.beans.v1.Tool;
24 import org.astrogrid.workflow.beans.v1.Workflow;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import java.net.URI;
30 import java.net.URLConnection;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 import junit.framework.Test;
36 import junit.framework.TestCase;
37 import junit.framework.TestSuite;
38
39 /*** Implementation of a Dispactcher that calls an cea web service to execute a job step.
40 * @author Noel Winstanley nw@jb.man.ac.uk 25-Feb-2004
41 *
42 */
43 public class CeaApplicationDispatcher implements Dispatcher, ComponentDescriptor {
44 private static final Log logger =
45 LogFactory.getLog(CeaApplicationDispatcher.class);
46 /*** Configuration component for Application Controller Dispatcher
47 * @author Noel Winstanley nw@jb.man.ac.uk 07-Mar-2004
48 *
49 */
50 public interface Endpoints {
51 URI monitorEndpoint();
52 URI resultListenerEndpoint();
53 }
54 /*** Construct a new ApplicationControllerDispatcher
55 * @param locator tool locator component to use to resolve endpoints
56 * @param endpoint configuration component that specifies the endpoint of the JobMonitor service. This is used by the
57 ApplicationController to
58 * return execution information back to the JES server.
59 */
60 public CeaApplicationDispatcher(Locator locator, Endpoints endpoint) {
61 this.locator = locator;
62 this.monitorURI = endpoint.monitorEndpoint();
63 this.resultListenerURI = endpoint.resultListenerEndpoint();
64 assert monitorURI != null;
65 assert resultListenerURI != null;
66 logger.info("monitor URL set to " + monitorURI.toString());
67 logger.info("result URL set to " + resultListenerURI.toString());
68 }
69 /*** tool locator component */
70 protected final Locator locator;
71 /*** endpoint of local jobmonitor service - used as a callback */
72 protected final URI monitorURI;
73 /*** endpoint of local result listener servuce - again, used as a callback */
74 protected final URI resultListenerURI;
75
76
77 public void dispatchStep(Workflow job, Tool tool,String stepId) throws JesException {
78
79 String[] toolLocations = locator.locateTool(tool);
80 JobIdentifierType id = JesUtil.createJobId(job.getJobExecutionRecord().getJobId(), stepId);
81 boolean connected = false;
82 if (logger.isDebugEnabled()) {
83 logger.debug("Will attempt to connect to " + Arrays.asList(toolLocations));
84 }
85 List exceptions = new ArrayList();
86 for (int i = 0; i < toolLocations.length && ! connected ; i++) {
87 try {
88 logger.debug("Creating a job on CEA server at " + toolLocations[i] +
89 " for " + tool.getName() + ", "+ id.getValue());
90 CommonExecutionConnectorClient appController = DelegateFactory.createDelegate(toolLocations[i]);
91 String applicationId = appController.init(tool,id);
92 logger.debug("Registering results listener " + resultListenerURI + " with " + toolLocations[i]);
93 appController.registerResultsListener(applicationId,resultListenerURI);
94 logger.debug("Registering progress listener " + monitorURI + " with " + toolLocations[i]);
95 appController.registerProgressListener(applicationId,monitorURI);
96 logger.debug("Activating the job on " + toolLocations[i]);
97 appController.execute(applicationId);
98 logger.debug("The job on " + toolLocations[i] + " was dispatched successfully.");
99 connected = true;
100 }
101 catch (Throwable e) {
102 logger.warn("Failed to communicate or initialize application with CEA Server",e);
103 exceptions.add(e);
104
105 }
106 }
107 if (! connected) {
108 String str = "Failed to communicate or initialize application with any CEA Server that provides this application:" +
109 Arrays.asList(toolLocations);
110 Throwable cause = null;
111 if (exceptions.size() == 1) {
112 cause = (Throwable)exceptions.get(0);
113 } else if (exceptions.size() > 1) {
114 cause = new AggregateException((Throwable[])exceptions.toArray(new Throwable[]{}));
115 }
116 logger.warn(str,cause);
117 throw new JesException(str,cause);
118 }
119
120 }
121
122 /***
123 * @see org.astrogrid.jes.component.ComponentDescriptor#getName()
124 */
125 public String getName() {
126 return "ApplicationController Dispatcher";
127 }
128
129 /***
130 * @see org.astrogrid.jes.component.ComponentDescriptor#getDescription()
131 */
132 public String getDescription() {
133 return "Dispatcher that executes job steps by calling application controllers"
134 + " Configured to tell application controllers to call back to:\n"
135 + monitorURI.toString();
136 }
137
138 /***
139 * @see org.astrogrid.jes.component.ComponentDescriptor#getInstallationTest()
140 */
141 public Test getInstallationTest() {
142 TestSuite suite = new TestSuite("Tests for ApplicationControllerDispatcher");
143 suite.addTest(new InstallationTest("testCanConnectMonitorURL"));
144 suite.addTest(new InstallationTest("testCanCallMonitorURL"));
145 return suite;
146 }
147
148 protected class InstallationTest extends TestCase {
149 public InstallationTest(String s) {
150 super(s);
151 }
152 public void testCanConnectMonitorURL() throws Exception {
153 URLConnection conn = monitorURI.toURL().openConnection();
154 assertNotNull(conn);
155 conn.connect();
156
157 }
158 public void testCanCallMonitorURL() throws Exception {
159 JobMonitor mon = JesDelegateFactory.createJobMonitor(monitorURI.toString());
160 assertNotNull(mon);
161
162 mon.monitorJob(null, null);
163 }
164 }
165
166
167
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270