3 import jalview.gui.AlignFrame;
4 import jalview.gui.WebserviceInfo;
5 import jalview.datamodel.AlignmentView;
6 import jalview.gui.Desktop;
7 import javax.swing.JOptionPane;
8 import jalview.bin.Cache;
10 public abstract class WSThread extends Thread
13 * Generic properties for Web Service Client threads.
15 AlignFrame alignFrame;
16 WebserviceInfo wsInfo = null;
18 boolean jobComplete = false;
19 abstract class WSJob {
21 * Generic properties for an individual job within a Web Service Client thread
23 int jobnum = 0; // WebServiceInfo pane for this job
24 String jobId; // ws job ticket
25 boolean cancelled = false;
26 int allowedServerExceptions = 3; // job dies if too many exceptions.
27 boolean submitted = false;
28 boolean subjobComplete = false;
31 * @return true if job has completed and valid results are available
33 abstract boolean hasResults();
36 * @return boolean true if job can be submitted.
38 abstract boolean hasValidInput();
39 vamsas.objects.simple.Result result;
41 class JobStateSummary {
49 void updateJobPanelState(WebserviceInfo wsInfo, String OutputHeader,
54 String progheader = "";
55 // Parse state of job[j]
56 if (j.result.isRunning())
59 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
61 else if (j.result.isQueued())
64 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
66 else if (j.result.isFinished())
69 j.subjobComplete = true;
72 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
74 else if (j.result.isFailed())
76 progheader += "Job failed.\n";
77 j.subjobComplete = true;
78 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
81 else if (j.result.isServerError())
84 j.subjobComplete = true;
85 wsInfo.setStatus(j.jobnum,
86 WebserviceInfo.STATE_STOPPED_SERVERERROR);
88 else if (j.result.isBroken() || j.result.isFailed())
91 j.subjobComplete = true;
92 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
94 // and pass on any sub-job messages to the user
95 wsInfo.setProgressText(j.jobnum, OutputHeader);
96 wsInfo.appendProgressText(j.jobnum, progheader);
97 if (j.result.getStatus() != null)
99 wsInfo.appendProgressText(j.jobnum, j.result.getStatus());
104 if (j.submitted && j.subjobComplete)
106 if (j.allowedServerExceptions == 0)
110 else if (j.result == null)
120 String WebServiceName = null;
123 abstract void pollJob(WSJob job) throws Exception;
126 JobStateSummary jstate=null;
131 jstate = new JobStateSummary();
132 for (int j = 0; j < jobs.length; j++)
135 if (!jobs[j].submitted && jobs[j].hasValidInput())
140 if (jobs[j].submitted && !jobs[j].subjobComplete)
145 if (jobs[j].result == null)
147 throw (new Exception(
148 "Timed out when communicating with server\nTry again later.\n"));
150 jalview.bin.Cache.log.debug("Job " + j + " Result state " +
151 jobs[j].result.getState()
153 jobs[j].result.isServerError() + ")");
157 // Deal with Transaction exceptions
158 wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName
159 + " Server exception!\n" + ex.getMessage());
160 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
161 + ") Server exception: " + ex.getMessage());
163 if (jobs[j].allowedServerExceptions > 0)
165 jobs[j].allowedServerExceptions--;
166 Cache.log.debug("Sleeping after a server exception.");
171 catch (InterruptedException ex1)
177 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
178 jobs[j].subjobComplete = true;
179 wsInfo.setStatus(jobs[j].jobnum,
180 WebserviceInfo.STATE_STOPPED_SERVERERROR);
183 catch (OutOfMemoryError er)
186 jobs[j].subjobComplete = true;
187 jobs[j].result = null; // may contain out of date result object
188 wsInfo.setStatus(jobs[j].jobnum,
189 WebserviceInfo.STATE_STOPPED_ERROR);
191 .showInternalMessageDialog(
193 "Out of memory handling result for job !!"
195 "\nSee help files for increasing Java Virtual Machine memory.",
196 "Out of memory", JOptionPane.WARNING_MESSAGE);
197 Cache.log.error("Out of memory when retrieving Job " + j + " id:" +
198 WsUrl + "/" + jobs[j].jobId, er);
202 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
204 // Decide on overall state based on collected jobs[] states
205 if (jstate.running > 0)
207 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
209 else if (jstate.queuing > 0)
211 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
216 if (jstate.finished > 0)
218 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
220 else if (jstate.error > 0)
222 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
224 else if (jstate.serror > 0)
226 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
235 catch (InterruptedException e)
237 Cache.log.debug("Interrupted sleep waiting for next job poll.", e);
239 // System.out.println("I'm alive "+alTitle);
244 parseResult(); // tidy up and make results available to user
247 abstract void StartJob(WSJob job);
248 abstract void parseResult();