public abstract class JobThread
extends java.lang.Thread
An instance of this class is a thread dedicated to a job execution.
This thread is necessary associated with a UWSJob
instance. Thus the execution of this thread is mainly done in
the jobWork()
method.
However its execution is possible only if the job phase is EXECUTING
. The job phase constraint (=EXECUTING) is
already checked so it is useless to check it again in jobWork()
.
jobWork()
) according to the execution conclusion:
publishResult(Result)
and getResultOutput(Result)
),UWSJob.abort()
must be called or an InterruptedException
can be thrownsetError(ErrorSummary)
or setError(UWSException)
.In both cases the startTime and the endTime fields are already managed by UWSJob
so it is useless to change them.
Just after the job work the job phase is set to COMPLETED
if no interruption has been detected,
ABORTED
otherwise.
jobWork()
throws:
UWSException
: the method setError(UWSException)
is called.
Besides the exception is kept in the lastError
and is available thanks to the UWSJob.getWorkError()
.InterruptedException
: the method UWSJob.abort()
is called.UWSJob.start()
,
UWSJob.abort()
,
UWSJob.getFileManager()
,
UWSJob.getWorkError()
Modifier and Type | Field and Description |
---|---|
protected ServiceErrorWriter |
errorWriter
Object to use in order to write the content of an error/exception in any output stream.
|
protected boolean |
fatalError
Indicate whether the exception stored in the attribute
lastError should be considered as a grave error or not. |
protected boolean |
finished
Indicates whether the
jobWork() has been called and finished, or not. |
protected UWSJob |
job
The job which contains all parameters for its execution and which must be filled at the end of the execution.
|
protected UWSException |
lastError
The last error which has occurred during the execution of this thread.
|
protected java.lang.String |
taskDescription
Description of what is done by this thread.
|
static java.lang.ThreadGroup |
tg
Group of threads in which this job thread will run.
|
Constructor and Description |
---|
JobThread(UWSJob j)
Builds the JobThread instance which will be used by the given job to execute its task.
|
JobThread(UWSJob j,
ServiceErrorWriter errorWriter)
Builds the JobThread instance which will be used by the given job to execute its task.
|
JobThread(UWSJob j,
java.lang.String task)
Builds the JobThread instance which will be used by the given job to execute its task.
|
JobThread(UWSJob j,
java.lang.String task,
ServiceErrorWriter errorWriter)
Builds the JobThread instance which will be used by the given job to execute its task.
|
Modifier and Type | Method and Description |
---|---|
Result |
createResult()
Creates a default result description.
|
Result |
createResult(java.lang.String name)
Creates a default result description but by precising its name/ID.
|
protected static java.lang.String |
getDefaultTaskDescription(UWSJob job)
Gets a default description of the task executed by a
JobThread . |
UWSException |
getError()
Gets the last error which has occurred during the execution of this thread.
|
UWSFileManager |
getFileManager()
Gets the manager of the UWS files (particularly: the error and results file).
|
UWSJob |
getJob()
Gets the job instance associated to this thread.
|
java.io.OutputStream |
getResultOutput(Result resultToWrite)
Gets an output stream for the given result.
|
long |
getResultSize(Result result)
Gets the size of the corresponding result file.
|
boolean |
isFinished()
Indicates whether the
jobWork() method has been called or not. |
protected abstract void |
jobWork()
Does the job work (i.e.
|
void |
publishResult(Result result)
Publishes the given result in the job.
|
void |
run()
|
void |
setError(ErrorSummary error)
Published the given error in the job.
|
void |
setError(UWSException ue)
Publishes the given exception as an error summary.
|
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
protected final UWSJob job
protected UWSException lastError
protected boolean fatalError
lastError
should be considered as a grave error or not.
By default, lastError
is a "normal" error.protected boolean finished
jobWork()
has been called and finished, or not.protected final java.lang.String taskDescription
protected final ServiceErrorWriter errorWriter
UWSToolBox.writeErrorFile(Exception, ErrorSummary, UWSJob, OutputStream)
(in text/plain with stack-trace).
Otherwise the content and the MIME type are determined by the error writer.public static final java.lang.ThreadGroup tg
public JobThread(UWSJob j) throws java.lang.NullPointerException
j
- The associated job.java.lang.NullPointerException
- If the given job or the given file manager is null.getDefaultTaskDescription(UWSJob)
public JobThread(UWSJob j, ServiceErrorWriter errorWriter) throws java.lang.NullPointerException
j
- The associated job.errorWriter
- Object to use in case of error in order to format the details of the error for the .../error/details parameter.java.lang.NullPointerException
- If the given job is null.getDefaultTaskDescription(UWSJob)
public JobThread(UWSJob j, java.lang.String task) throws java.lang.NullPointerException
j
- The associated job.task
- Description of the task executed by this thread.java.lang.NullPointerException
- If the given job is null.public JobThread(UWSJob j, java.lang.String task, ServiceErrorWriter errorWriter) throws java.lang.NullPointerException
j
- The associated job.task
- Description of the task executed by this thread.errorWriter
- Object to use in case of error in order to format the details of the error for the .../error/details parameter.java.lang.NullPointerException
- If the given job is null.protected static final java.lang.String getDefaultTaskDescription(UWSJob job)
JobThread
.job
- A UWS job.public final UWSJob getJob()
public final UWSFileManager getFileManager()
public final UWSException getError()
public final boolean isFinished()
jobWork()
method has been called or not.public void setError(ErrorSummary error) throws UWSException
Published the given error in the job.
note: This thread will be stopped.
error
- The error to publish.UWSException
- If there is an error while publishing the error.UWSJob.error(ErrorSummary)
public void setError(UWSException ue) throws UWSException
Publishes the given exception as an error summary.
Doing that also stops the job, sets the phase to ExecutionPhase.ERROR
and sets the end time.
By default, this function tries to write the stack trace of the given exception thanks to
UWSFileManager.getErrorOutput(ErrorSummary, UWSJob)
. If it fails or if this job is not connected to
a job list or the connected job list is not connected to a UWS, setError(ErrorSummary)
is called and
no error file is written.
ue
- The exception that has interrupted this job.UWSException
- If there is an error while publishing the given exception.setError(ErrorSummary)
,
UWSToolBox.writeErrorFile(Exception, ErrorSummary, UWSJob, OutputStream)
public Result createResult()
createResult(String)
public Result createResult(java.lang.String name)
name
- The name/ID of the result to create.Result.Result(UWSJob, String)
public void publishResult(Result result) throws UWSException
result
- The result to publish.UWSException
- If there is an error while publishing the result.UWSJob.addResult(Result)
public java.io.OutputStream getResultOutput(Result resultToWrite) throws java.io.IOException, UWSException
Gets an output stream for the given result.
note: the result file will be created if needed.
resultToWrite
- The description of the result to write.java.io.IOException
- If there is an error while creating the file or the output stream.UWSException
- If an error occurs in the UWSFileManager.getResultOutput(Result, UWSJob)
.public long getResultSize(Result result) throws java.io.IOException
result
- Description of the Result whose the size is wanted.java.io.IOException
- If there is an error while getting the result file size.UWSFileManager.getResultSize(Result, UWSJob)
protected abstract void jobWork() throws UWSException, java.lang.InterruptedException
Does the job work (i.e. making a long computation or executing a query on a Database).
Important:
Thread.isInterrupted()
) and then to send an InterruptedException
.
Otherwise the UWSJob.stop()
method will have no effect, as for UWSJob.abort()
and setError(ErrorSummary)
.
Notes:
UWSException
is thrown the JobThread
will automatically publish the exception in this job
thanks to the UWSJob.error(ErrorSummary)
method or the UWSJob.setErrorSummary(ErrorSummary)
method,
and so it will set its phase to ExecutionPhase.ERROR
.InterruptedException
is thrown the JobThread
will automatically set the phase to ExecutionPhase.ABORTED
UWSException
- If there is any kind of error which must be propagated.java.lang.InterruptedException
- If the thread has been interrupted or if any method throws this exception.public final void run()
EXECUTING
, nothing is done...the thread ends immediately.jobWork()
method.COMPLETED
if not interrupted, else ABORTED
.
If any InterruptedException
occurs the job phase is only set to ABORTED
.
If any UWSException
occurs while the phase is EXECUTING
the job phase
is set to ERROR
and an error summary is created.
Whatever is the exception, it will always be available thanks to the getError()
after execution.
run
in interface java.lang.Runnable
run
in class java.lang.Thread
jobWork()
,
UWSJob.setPhase(ExecutionPhase)
,
setError(UWSException)
,
setError(ErrorSummary)