2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import jalview.datamodel.*;
27 public abstract class WSThread
31 * Generic properties for Web Service Client threads.
33 AlignFrame alignFrame = null;
34 WebserviceInfo wsInfo = null;
35 AlignmentView input = null;
36 boolean jobComplete = false;
40 * Generic properties for an individual job within a Web Service Client thread
42 int jobnum = 0; // WebServiceInfo pane for this job
43 String jobId; // ws job ticket
44 boolean cancelled = false;
45 int allowedServerExceptions = 3; // job dies if too many exceptions.
46 boolean submitted = false;
47 boolean subjobComplete = false;
50 * @return true if job has completed and valid results are available
52 abstract boolean hasResults();
56 * @return boolean true if job can be submitted.
58 abstract boolean hasValidInput();
60 vamsas.objects.simple.Result result;
72 void updateJobPanelState(WebserviceInfo wsInfo, String OutputHeader,
77 String progheader = "";
78 // Parse state of job[j]
79 if (j.result.isRunning())
82 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
84 else if (j.result.isQueued())
87 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
89 else if (j.result.isFinished())
92 j.subjobComplete = true;
97 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
99 else if (j.result.isFailed())
101 progheader += "Job failed.\n";
102 j.subjobComplete = true;
103 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
106 else if (j.result.isServerError())
109 j.subjobComplete = true;
110 wsInfo.setStatus(j.jobnum,
111 WebserviceInfo.STATE_STOPPED_SERVERERROR);
113 else if (j.result.isBroken() || j.result.isFailed())
116 j.subjobComplete = true;
117 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
119 // and pass on any sub-job messages to the user
120 wsInfo.setProgressText(j.jobnum, OutputHeader);
121 wsInfo.appendProgressText(j.jobnum, progheader);
122 if (j.result.getStatus() != null)
124 wsInfo.appendProgressText(j.jobnum, j.result.getStatus());
129 if (j.submitted && j.subjobComplete)
131 if (j.allowedServerExceptions == 0)
135 else if (j.result == null)
145 String WebServiceName = null;
148 abstract void pollJob(WSJob job)
153 JobStateSummary jstate = null;
160 jstate = new JobStateSummary();
161 for (int j = 0; j < jobs.length; j++)
164 if (!jobs[j].submitted && jobs[j].hasValidInput())
169 if (jobs[j].submitted && !jobs[j].subjobComplete)
174 if (jobs[j].result == null)
176 throw (new Exception(
177 "Timed out when communicating with server\nTry again later.\n"));
179 jalview.bin.Cache.log.debug("Job " + j + " Result state " +
180 jobs[j].result.getState()
182 jobs[j].result.isServerError() + ")");
186 // Deal with Transaction exceptions
187 wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName
188 + " Server exception!\n" + ex.getMessage());
189 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
190 + ") Server exception: " + ex.getMessage());
192 if (jobs[j].allowedServerExceptions > 0)
194 jobs[j].allowedServerExceptions--;
195 Cache.log.debug("Sleeping after a server exception.");
200 catch (InterruptedException ex1)
206 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
207 jobs[j].subjobComplete = true;
208 wsInfo.setStatus(jobs[j].jobnum,
209 WebserviceInfo.STATE_STOPPED_SERVERERROR);
212 catch (OutOfMemoryError er)
215 jobs[j].subjobComplete = true;
216 jobs[j].result = null; // may contain out of date result object
217 wsInfo.setStatus(jobs[j].jobnum,
218 WebserviceInfo.STATE_STOPPED_ERROR);
220 .showInternalMessageDialog(
222 "Out of memory handling result for job !!"
224 "\nSee help files for increasing Java Virtual Machine memory.",
225 "Out of memory", JOptionPane.WARNING_MESSAGE);
226 Cache.log.error("Out of memory when retrieving Job " + j + " id:" +
227 WsUrl + "/" + jobs[j].jobId, er);
231 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
233 // Decide on overall state based on collected jobs[] states
234 if (jstate.running > 0)
236 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
238 else if (jstate.queuing > 0)
240 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
245 if (jstate.finished > 0)
247 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
249 else if (jstate.error > 0)
251 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
253 else if (jstate.serror > 0)
255 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
264 catch (InterruptedException e)
266 Cache.log.debug("Interrupted sleep waiting for next job poll.", e);
268 // System.out.println("I'm alive "+alTitle);
271 if (jobComplete && jobs != null)
273 parseResult(); // tidy up and make results available to user
277 Cache.log.debug("WebServiceJob poll loop finished with no jobs created.");
281 abstract void StartJob(WSJob job);
283 abstract void parseResult();