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
TaskMonitors |
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
TaskMonitors are registered |
TaskMonitor[] |
getMonitors()
Return the current
TaskMonitors 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
TaskMonitors 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, setLogChannelconfigureDumpHeadlineLength, configureDumpMaxElementsPrinted, copyDumpStringValues, copyToStringValues, createDumpHeader, toDumpString, toDumpString, toString, toStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitgetApplicationContextgetLogChannel, isLogDebug, isLogVerbose, logDebug, logDump, logError, logError, logFatal, logInfo, logVerbose, logWarning, setLogChannelprotected 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 LogChannelpublic 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()
AsyncExecutablegetName 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()
AsyncExecutablegetType in interface AsyncExecutable<C extends ApplicationContext>public TaskId getTaskId()
AsyncExecutableTaskId is used to model the task-hierarchy in a RunStateFlag.
For AsyncExecutables it's always an instance of PseudoTaskId.getTaskId in interface AsyncExecutable<C extends ApplicationContext>protected void setTaskId(TaskId taskId)
public void setMaxProgressSteps(int expectedProgressSteps)
TaskMonitors.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()
AsyncExecutableRunStateFlag. 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.Exceptionprotected 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.Exceptionpublic void addMonitor(TaskMonitor monitor)
TaskMonitorSupportTaskMonitor which is notified about RunStateFlag changesaddMonitor in interface TaskMonitorSupportpublic void removeMonitor(TaskMonitor monitor)
TaskMonitorSupportTaskMonitor from the registrationremoveMonitor in interface TaskMonitorSupportpublic TaskMonitor[] getMonitors()
TaskMonitorSupportTaskMonitors if anygetMonitors in interface TaskMonitorSupportpublic boolean hasMonitor()
TaskMonitorSupportTaskMonitors registeredhasMonitor in interface TaskMonitorSupportpublic int getMonitorCount()
TaskMonitorSupportTaskMonitors are registeredgetMonitorCount in interface TaskMonitorSupportpublic void clearMonitors()
TaskMonitorSupportTaskMonitorsclearMonitors in interface TaskMonitorSupportpublic 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()
CancellableTaskwasCancelled in interface CancellableTaskpublic 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()
AsyncExecutableisCancellable in interface AsyncExecutable<C extends ApplicationContext>AsyncExecutable.setCancellable(boolean)public final void setCancellable(boolean isCancellable)
AsyncExecutablesetCancellable 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()
AsyncExecutablegetResult 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.Exceptionprotected void handleSuccess()
protected void handleFinish()
public abstract void executeAsync()
throws java.lang.Exception
AsyncExecutableRunnable.run() for async execution.executeAsync in interface AsyncExecutable<C extends ApplicationContext>java.lang.Exceptionfor synchronous executionpublic 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 Executablejava.lang.Exception - on failure (but not on cancellation)executeAsync()Copyright © 2014 EsprIT-Systems. All Rights Reserved.