public abstract class AbstractAsyncTask<C extends ApplicationContext> extends AbstractAsyncExecutable<C> implements AsyncTask<C>
ExecutorService. A task may have a TaskMonitor attached which is
being notified about progress whereas the notification methods are called directly by
the executing thread. The task can be cancelled any time with the cancel()
call. This only works, if the AbstractAsyncExecutable.isCancellable() returns true. The task itself
may decide not to be cancellable during critical phases. A task may produce a result
which can be set (by the task itself) using AbstractAsyncExecutable.setResult(Object). Any other
thread may block waiting for the result with awaitFinished().childTaskRef, isTestMode, monitorController, resultRef, stateFlag| Modifier | Constructor and Description |
|---|---|
protected |
AbstractAsyncTask(C ctx) |
protected |
AbstractAsyncTask(C ctx,
LogChannel logCh) |
protected |
AbstractAsyncTask(C ctx,
LogChannel logCh,
java.lang.String taskName,
TaskType taskType) |
protected |
AbstractAsyncTask(C ctx,
LogChannel logCh,
TaskType taskType) |
protected |
AbstractAsyncTask(C ctx,
java.lang.String taskName) |
protected |
AbstractAsyncTask(C ctx,
java.lang.String taskName,
TaskType taskType) |
protected |
AbstractAsyncTask(C ctx,
TaskType taskType) |
| Modifier and Type | Method and Description |
|---|---|
<R> R |
awaitFinished()
Awaits non-interruptibly the finish of this task.
|
<R> R |
awaitFinished(long timeout,
java.util.concurrent.TimeUnit unit)
Waits until the task has finished but runs in a TimeoutException if
it did not finish within the expected time.
|
void |
awaitStarted()
Waits until the task actually has started running.
|
void |
cancel()
Flags the task as being cancelled.
|
protected TaskId |
createTaskId()
Is called at submit time.
|
protected void |
deregisterTask()
Removes registration of the the task from the
TaskManager |
protected void |
doOnFinish()
Is called in ANY CASE when the task has finished.
|
protected void |
doOnStart()
Is called when the task actually has started.
|
protected void |
doOnSuccess()
Is called when the task has succeeded.
|
abstract void |
executeAsync()
Must be overridden to perform the async part.
|
void |
executeChildTask(AsyncTask childTask)
Runs the childTask in the default executor of the
ApplicationContext. |
void |
executeChildTask(AsyncTask childTask,
java.util.concurrent.ExecutorService pool)
Runs the childTask asynchronously as a task on it's own.
|
protected java.util.concurrent.Future |
getCurrentFuture()
Returns the
Future of this task as returned by the thread-pool. |
int |
getMaxChildTasks() |
int |
getPriority()
Returns the Tread execution priority.
|
long |
getProcessingDuration()
Tells how long the task has been running since STARTED
|
long |
getStartTime()
Returns the time in millis when this task started execution
|
StopWatch |
getStopWatch()
Returns the tasks
StopWatch for analyzing elapsed execution times. |
protected void |
handleFinish() |
protected void |
handleStart() |
protected void |
handleSuccess() |
boolean |
isRegisterForCancel()
Tells whether the task is being registered at the
TaskManager to be
accessible for cancellation. |
boolean |
isRunning()
Returns true from the time on the task has been submitted until it has finished.
|
void |
notifyProceeded(java.lang.Object result,
int step,
int maxSteps,
java.lang.String message)
Notifies progress to an attached task-monitor.
|
protected void |
registerTask()
Registers the task at the
TaskManager |
void |
releaseStart()
Releases the task for execution after submission.
|
void |
run()
This method defines a clean task body around the asynchronously called
executeAsync() method. |
protected void |
setCurrentFuture(java.util.concurrent.Future future) |
void |
setMaxChildTasks(int maxChildTasks)
Sets the max number of child-tasks that are being executed within this main task.
|
void |
setPriority(int priority)
Sets the thread-priority for execution.
|
void |
setRegisterForCancel(boolean isRegister) |
void |
setReleaseTimeout(long releaseTimeoutMillis)
Defines a timeout for waiting on the release gate after having started.
|
TaskId |
submit()
Submits this task for execution to the default application thread pool
|
TaskId |
submit(java.util.concurrent.ExecutorService pool)
Submits this task for execution to the given thread pool
|
protected TaskId |
submitInternal(java.util.concurrent.ExecutorService pool) |
ToString |
toString(ToString s)
This method allows subclasses to add values to the existing ToString of the superclass.
|
boolean |
wasSucceeded()
Tells whether this task has succeeded
|
addMonitor, checkCancelled, checkForErrorAndRethrow, clearMonitors, createRunStateFlag, doOnCancel, doOnFailure, execute, executeSubTask, getError, getMaxProgressSteps, getMonitorCount, getMonitors, getName, getProgressStepCount, getResult, getRunStateFlag, getTaskId, getType, handleCancellation, handleFailure, hasError, hasMonitor, isCancellable, registerTaskStateChange, removeMonitor, resetProgress, setCancellable, setLogLevel, setMaxProgressSteps, setMonitor, setName, setName, setResult, setRunStateFlag, setTaskId, setType, shouldFireMonitor, sleepChecked, wasCancelledgetApplicationContext, getLogChannel, isLogDebug, isLogVerbose, logDebug, logDump, logError, logError, logFatal, logInfo, logVerbose, logWarning, setLogChannelconfigureDumpHeadlineLength, configureDumpMaxElementsPrinted, copyDumpStringValues, copyToStringValues, createDumpHeader, toDumpString, toDumpString, toStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitcheckCancelled, checkForErrorAndRethrow, executeSubTask, getName, getResult, getRunStateFlag, getTaskId, getType, handleCancellation, handleFailure, isCancellable, setCancellable, setName, setResult, setRunStateFlag, sleepCheckedgetApplicationContextexecuteaddMonitor, clearMonitors, getMonitorCount, getMonitors, hasMonitor, removeMonitorgetLogChannel, isLogDebug, isLogVerbose, logDebug, logDump, logError, logError, logFatal, logInfo, logVerbose, logWarning, setLogChannelwasCancelledgetTaskIdtoDumpString, toDumpString, toStringprotected AbstractAsyncTask(C ctx)
protected AbstractAsyncTask(C ctx, LogChannel logCh, TaskType taskType)
protected AbstractAsyncTask(C ctx, LogChannel logCh)
protected AbstractAsyncTask(C ctx, java.lang.String taskName)
protected AbstractAsyncTask(C ctx, LogChannel logCh, java.lang.String taskName, TaskType taskType)
public TaskId submit() throws java.lang.Exception
AsyncTasksubmit in interface AsyncTask<C extends ApplicationContext>java.lang.Exception - if the pool is busypublic TaskId submit(java.util.concurrent.ExecutorService pool) throws java.lang.Exception
AsyncTasksubmit in interface AsyncTask<C extends ApplicationContext>java.lang.Exception - if the pool is busypublic final void setMaxChildTasks(int maxChildTasks)
AbstractAsyncExecutable.executeSubTask(AsyncExecutable) will be
called maxChildTasks times.getMaxChildTasks()public final int getMaxChildTasks()
setMaxChildTasks(int)public final int getPriority()
setPriority(int)public final void setPriority(int priority)
priority - the thread-prioritysetPriority(int)public final StopWatch getStopWatch()
StopWatch for analyzing elapsed execution times.public final long getStartTime()
public final long getProcessingDuration()
AsyncTaskgetProcessingDuration in interface AsyncTask<C extends ApplicationContext>protected java.util.concurrent.Future getCurrentFuture()
Future of this task as returned by the thread-pool.protected final void setCurrentFuture(java.util.concurrent.Future future)
protected final TaskId submitInternal(java.util.concurrent.ExecutorService pool) throws java.lang.Exception
java.lang.Exceptionpublic final void setReleaseTimeout(long releaseTimeoutMillis)
AsyncTaskAsyncTask.releaseStart() is being called
explicitly. This allows for getting the TaskId on submit but having more control about the actual start
time of the task. By default the task does not wait on the release gate.setReleaseTimeout in interface AsyncTask<C extends ApplicationContext>AsyncTask.releaseStart()public final void releaseStart()
AsyncTaskreleaseStart in interface AsyncTask<C extends ApplicationContext>AsyncTask.setReleaseTimeout(long)public final void awaitStarted()
throws java.lang.InterruptedException
awaitStarted in interface AsyncTask<C extends ApplicationContext>java.lang.InterruptedExceptionprotected TaskId createTaskId()
TaskId which
is added to the RunStateFlag.public abstract void executeAsync()
throws java.lang.Exception
notifyProceeded(Object, int, int, String) in order to notify
about task progress. Note: the running task should periodically call
AbstractAsyncExecutable.checkCancelled() in order to detect cancellation.executeAsync in interface AsyncExecutable<C extends ApplicationContext>executeAsync in class AbstractAsyncExecutable<C extends ApplicationContext>java.lang.Exceptionfor synchronous executionpublic final void executeChildTask(AsyncTask childTask, java.util.concurrent.ExecutorService pool) throws java.lang.Exception
RunStateFlag with the
main-task. As the childTask runs in a different execution thread it has got it's
own TaskId which is added to the main-TaskId. canceling the main-task in
fact cancels the childTask. During childTask execution the monitor will receive
CHILD_STARTED, CHILD_SUCCEEDED and CHILD_FINISHED notifications. Any PROGRESS
notifications in between are associated to the current childTask.executeChildTask in interface AsyncTask<C extends ApplicationContext>java.lang.Exceptionpublic final void executeChildTask(AsyncTask childTask) throws java.lang.Exception
ApplicationContext.executeChildTask in interface AsyncTask<C extends ApplicationContext>java.lang.ExceptionexecuteChildTask(AsyncTask, ExecutorService)public void cancel()
AbstractAsyncExecutable.checkCancelled() will
result a CancellationException being thrown. As a result
AbstractAsyncExecutable.doOnCancel() is being called on the task.cancel in interface LocalCancellableTaskpublic final void run()
executeAsync() method. It manages the RunStateFlag and drives the
TaskMonitor.run in interface java.lang.Runnableprotected final void handleStart()
throws java.lang.Exception
handleStart in class AbstractAsyncExecutable<C extends ApplicationContext>java.lang.Exceptionprotected final void handleSuccess()
handleSuccess in class AbstractAsyncExecutable<C extends ApplicationContext>protected final void handleFinish()
handleFinish in class AbstractAsyncExecutable<C extends ApplicationContext>protected void doOnStart()
throws java.lang.Exception
java.lang.Exceptionprotected void doOnSuccess()
protected void doOnFinish()
public final boolean isRegisterForCancel()
TaskManager to be
accessible for cancellation.isRegisterForCancel in interface AsyncTask<C extends ApplicationContext>public final void setRegisterForCancel(boolean isRegister)
protected void registerTask()
TaskManagerprotected void deregisterTask()
TaskManagerpublic void notifyProceeded(java.lang.Object result,
int step,
int maxSteps,
java.lang.String message)
AbstractAsyncExecutableTaskMonitor.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
AbstractAsyncExecutable.getMaxProgressSteps(). Similar the step value is typically the same as
returned by AbstractAsyncExecutable.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>notifyProceeded in class AbstractAsyncExecutable<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 ispublic final boolean wasSucceeded()
public boolean isRunning()
AsyncTaskisRunning in interface AsyncTask<C extends ApplicationContext>public final <R> R awaitFinished()
awaitFinished in interface AsyncTask<C extends ApplicationContext>public final <R> R awaitFinished(long timeout,
java.util.concurrent.TimeUnit unit)
throws java.util.concurrent.TimeoutException
AsyncTaskawaitFinished in interface AsyncTask<C extends ApplicationContext>java.util.concurrent.TimeoutExceptionAsyncTask.awaitFinished()public ToString toString(ToString s)
DumpabletoString in interface DumpabletoString in class DumpableObjectCopyright © 2014 EsprIT-Systems. All Rights Reserved.