public abstract class AbstractAsyncExecutable<C extends ApplicationContext> extends AbstractApplicationObject<C> implements AsyncExecutable<C>
AsyncExecutable
used for subclassing.Modifier and Type | Field and Description |
---|---|
protected java.util.concurrent.atomic.AtomicReference<AsyncExecutable> |
childTaskRef |
protected boolean |
isTestMode |
protected TaskMonitorController |
monitorController |
protected java.util.concurrent.atomic.AtomicReference |
resultRef |
protected RunStateFlag |
stateFlag |
Modifier | Constructor and Description |
---|---|
protected |
AbstractAsyncExecutable(C ctx) |
protected |
AbstractAsyncExecutable(C ctx,
LogChannel logCh) |
protected |
AbstractAsyncExecutable(C ctx,
LogChannel logCh,
java.lang.String taskName,
TaskType taskType) |
protected |
AbstractAsyncExecutable(C ctx,
java.lang.String taskName) |
protected |
AbstractAsyncExecutable(C ctx,
java.lang.String taskName,
TaskType taskType) |
Modifier and Type | Method and Description |
---|---|
void |
addMonitor(TaskMonitor monitor)
Adds a
TaskMonitor which is notified about RunStateFlag changes |
void |
checkCancelled()
Checks whether the task was cancelled and throws a
CancellationException just in case. |
void |
checkForErrorAndRethrow(java.lang.Exception e)
Stores an Error (if any) or a cancellation in the
RunStateFlag of this task. |
void |
clearMonitors()
Removes all
TaskMonitor s |
protected RunStateFlag |
createRunStateFlag()
Creates the
RunStateFlag instance of this task. |
protected void |
doOnCancel()
This method is called after having detected cancellation.
|
protected void |
doOnFailure(java.lang.Exception error)
Is called when the task failed.
|
void |
execute()
This method just calls
executeAsync() synchronously within the same thread (not putting
the task into a thread-pool). |
abstract void |
executeAsync()
Contains the code to be executed asynchronously.
|
void |
executeSubTask(AsyncExecutable subTask)
Runs the subTask synchronously within the same execution thread.
|
java.lang.Exception |
getError() |
int |
getMaxProgressSteps() |
int |
getMonitorCount()
Tells how many
TaskMonitor s are registered |
TaskMonitor[] |
getMonitors()
Return the current
TaskMonitor s if any |
java.lang.String |
getName()
Return the task-name which is rendered by the RunStateFlag during run.
|
int |
getProgressStepCount()
Returns the number of progress steps notified during the last run.
|
<R> R |
getResult()
Returns the execution result of this task, if any.
|
RunStateFlag |
getRunStateFlag()
Returns the current
RunStateFlag . |
TaskId |
getTaskId()
Returns an identifier object for this task.
|
TaskType |
getType()
Return the task-type which is rendered by the RunStateFlag during run.
|
void |
handleCancellation()
This method is public as an implementation side effect.
|
void |
handleFailure(java.lang.Exception error)
This method is public as an implementation side effect.
|
protected void |
handleFinish() |
protected void |
handleStart() |
protected void |
handleSuccess() |
boolean |
hasError() |
boolean |
hasMonitor()
Tells whether there are
TaskMonitor s registered |
boolean |
isCancellable()
Tells whether this task can be cancelled.
|
void |
notifyProceeded(java.lang.Object result,
int step,
int maxSteps,
java.lang.String message)
Notifies progress to an attached task-monitor.
|
protected void |
registerTaskStateChange() |
void |
removeMonitor(TaskMonitor monitor)
Removes the given
TaskMonitor from the registration |
protected void |
resetProgress() |
void |
setCancellable(boolean isCancellable)
Tells whether this task is cancellable - is true by default.
|
void |
setLogLevel(LogLevel level)
Changes the
LogLevel of the task's LogChannel |
void |
setMaxProgressSteps(int expectedProgressSteps)
Sets the max number of progressSteps expected (i.e. for driving a progress bar).
|
void |
setMonitor(TaskMonitor monitor)
Clears all monitors and adds the given monitor as the only one.
|
void |
setName(NlsKey trKey,
java.lang.Object... args)
Convenience method for setting translated taskName with optional args.
|
void |
setName(java.lang.String taskName)
Sets the taskName for this executable which will also be reflected by the
TaskId . |
void |
setResult(java.lang.Object result)
If this task produces any result it uses this method to store it.
|
void |
setRunStateFlag(RunStateFlag stateFlag)
Used to set a
RunStateFlag if this task should run as a childTask. |
protected void |
setTaskId(TaskId taskId) |
void |
setType(TaskType taskType)
Sets the
TaskType for this task. |
protected boolean |
shouldFireMonitor() |
void |
sleepChecked(long millis)
Used to delay task execution.
|
boolean |
wasCancelled()
Tells whether ever an attempt has been made to cancel this task.
|
getApplicationContext, getLogChannel, isLogDebug, isLogVerbose, logDebug, logDump, logError, logError, logFatal, logInfo, logVerbose, logWarning, setLogChannel
configureDumpHeadlineLength, configureDumpMaxElementsPrinted, copyDumpStringValues, copyToStringValues, createDumpHeader, toDumpString, toDumpString, toString, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getApplicationContext
getLogChannel, isLogDebug, isLogVerbose, logDebug, logDump, logError, logError, logFatal, logInfo, logVerbose, logWarning, setLogChannel
protected boolean isTestMode
protected final java.util.concurrent.atomic.AtomicReference resultRef
protected final TaskMonitorController monitorController
protected final java.util.concurrent.atomic.AtomicReference<AsyncExecutable> childTaskRef
protected RunStateFlag stateFlag
protected AbstractAsyncExecutable(C ctx)
protected AbstractAsyncExecutable(C ctx, LogChannel logCh)
protected AbstractAsyncExecutable(C ctx, java.lang.String taskName)
protected AbstractAsyncExecutable(C ctx, java.lang.String taskName, TaskType taskType)
protected AbstractAsyncExecutable(C ctx, LogChannel logCh, java.lang.String taskName, TaskType taskType)
public void setLogLevel(LogLevel level)
LogLevel
of the task's LogChannel
public final void setName(NlsKey trKey, java.lang.Object... args)
setName(String)
public void setName(java.lang.String taskName)
TaskId
.
If taskName is null then the simple className of this instance will be used as a default.setName
in interface AsyncExecutable<C extends ApplicationContext>
public final java.lang.String getName()
AsyncExecutable
getName
in interface AsyncExecutable<C extends ApplicationContext>
public final void setType(TaskType taskType)
TaskType
for this task.
This type will also be reflected by the RunStateFlag
and the TaskId
.public TaskType getType()
AsyncExecutable
getType
in interface AsyncExecutable<C extends ApplicationContext>
public TaskId getTaskId()
AsyncExecutable
TaskId
is used to model the task-hierarchy in a RunStateFlag
.
For AsyncExecutable
s it's always an instance of PseudoTaskId
.getTaskId
in interface AsyncExecutable<C extends ApplicationContext>
protected void setTaskId(TaskId taskId)
public void setMaxProgressSteps(int expectedProgressSteps)
TaskMonitor
s.getMaxProgressSteps()
public final int getMaxProgressSteps()
setMaxProgressSteps(int)
public final int getProgressStepCount()
protected final void resetProgress()
protected RunStateFlag createRunStateFlag()
RunStateFlag
instance of this task.
Is only called once at construction time.public final void setRunStateFlag(RunStateFlag stateFlag)
RunStateFlag
if this task should run as a childTask.
By default this task has got its own RunStateFlag already set.setRunStateFlag
in interface AsyncExecutable<C extends ApplicationContext>
getRunStateFlag()
public RunStateFlag getRunStateFlag()
AsyncExecutable
RunStateFlag
. Note that there is exactly one
RunStateFlag
per running task. Child-tasks as well as sub-tasks share it with their
parents.getRunStateFlag
in interface AsyncExecutable<C extends ApplicationContext>
public void checkForErrorAndRethrow(java.lang.Exception e) throws java.lang.Exception
RunStateFlag
of this task.
Note that CancellationException
. InterruptedException
and StopException
are NOT considered to be an error. In any case the given Exception is rethrown.checkForErrorAndRethrow
in interface AsyncExecutable<C extends ApplicationContext>
java.lang.Exception
protected final void registerTaskStateChange()
public final void handleCancellation()
doOnCancel()
handleCancellation
in interface AsyncExecutable<C extends ApplicationContext>
public final void handleFailure(java.lang.Exception error)
doOnFailure(Exception)
handleFailure
in interface AsyncExecutable<C extends ApplicationContext>
protected final boolean shouldFireMonitor()
public void executeSubTask(AsyncExecutable subTask) throws java.lang.Exception
RunStateFlag
with the main-task. During subTask execution the monitor will receive
CHILD_STARTED CHILD_SUCCEEDED and CHILD_FINISHED notifications. Any PROGRESS notifications in between are
associated to the current subTask.executeSubTask
in interface AsyncExecutable<C extends ApplicationContext>
java.lang.Exception
public void addMonitor(TaskMonitor monitor)
TaskMonitorSupport
TaskMonitor
which is notified about RunStateFlag
changesaddMonitor
in interface TaskMonitorSupport
public void removeMonitor(TaskMonitor monitor)
TaskMonitorSupport
TaskMonitor
from the registrationremoveMonitor
in interface TaskMonitorSupport
public TaskMonitor[] getMonitors()
TaskMonitorSupport
TaskMonitor
s if anygetMonitors
in interface TaskMonitorSupport
public boolean hasMonitor()
TaskMonitorSupport
TaskMonitor
s registeredhasMonitor
in interface TaskMonitorSupport
public int getMonitorCount()
TaskMonitorSupport
TaskMonitor
s are registeredgetMonitorCount
in interface TaskMonitorSupport
public void clearMonitors()
TaskMonitorSupport
TaskMonitor
sclearMonitors
in interface TaskMonitorSupport
public void setMonitor(TaskMonitor monitor)
protected void doOnFailure(java.lang.Exception error)
protected void doOnCancel()
public final void checkCancelled()
CancellationException
just in case.
This methods should be called within loops in order to react promptly on cancellation.checkCancelled
in interface AsyncExecutable<C extends ApplicationContext>
public final boolean wasCancelled()
CancellableTask
wasCancelled
in interface CancellableTask
public final void sleepChecked(long millis)
CancellationException
is thrown just in case.sleepChecked
in interface AsyncExecutable<C extends ApplicationContext>
public final boolean hasError()
public final java.lang.Exception getError()
public final boolean isCancellable()
AsyncExecutable
isCancellable
in interface AsyncExecutable<C extends ApplicationContext>
AsyncExecutable.setCancellable(boolean)
public final void setCancellable(boolean isCancellable)
AsyncExecutable
setCancellable
in interface AsyncExecutable<C extends ApplicationContext>
public final void setResult(java.lang.Object result)
setResult
in interface AsyncExecutable<C extends ApplicationContext>
getResult()
public <R> R getResult()
AsyncExecutable
getResult
in interface AsyncExecutable<C extends ApplicationContext>
AsyncExecutable.setResult(Object)
public void notifyProceeded(java.lang.Object result, int step, int maxSteps, java.lang.String message)
TaskMonitor.taskProceeded(RunStateFlag, Object, int, int, String)
IMPORTANT: the 'result' object passed here MUST NOT be changed any more by the
running task. If this cannot be guaranteed make sure that you pass a copy here! A
copy is not made automatically because in many cases this would not be necessary.
The progressCount value is typically the same as returned by
getMaxProgressSteps()
. Similar the step value is typically the same as
returned by getProgressStepCount()
. Anyway you are allowed to pass these
values explicitly because a running task may have complex behavior during runtime.
For example: a task may not know at start time how many steps it will do - at that
time the ProgressBar shows up floating. But later on it has calculated the steps it
will need and then it performs step notifications for driving the progress bar on a
percentage value.notifyProceeded
in interface AsyncExecutable<C extends ApplicationContext>
result
- any intermediate result that may be achieved so farstep
- the count of progress stepsmaxSteps
- the expected maximum number of progress stepsmessage
- any textual message explaining what the progress issetMaxProgressSteps(int)
to a negative value. A notification with a
negative value for the 'step' parameter will be ignored by the progress bar.
That way you can send for instance a 'result' value without modifying the
current progress.protected void handleStart() throws java.lang.Exception
java.lang.Exception
protected void handleSuccess()
protected void handleFinish()
public abstract void executeAsync() throws java.lang.Exception
AsyncExecutable
Runnable.run()
for async execution.executeAsync
in interface AsyncExecutable<C extends ApplicationContext>
java.lang.Exception
for synchronous execution
public void execute() throws java.lang.Exception
executeAsync()
synchronously within the same thread (not putting
the task into a thread-pool). Note that during sync execution the task is NOT registered
at the TaskManager
and no PROGRESS is fired (it would not be repainted anyway by
the ET because the ET itself is the executor).execute
in interface Executable
java.lang.Exception
- on failure (but not on cancellation)executeAsync()
Copyright © 2014 EsprIT-Systems. All Rights Reserved.