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, wasCancelled
getApplicationContext, getLogChannel, isLogDebug, isLogVerbose, logDebug, logDump, logError, logError, logFatal, logInfo, logVerbose, logWarning, setLogChannel
configureDumpHeadlineLength, configureDumpMaxElementsPrinted, copyDumpStringValues, copyToStringValues, createDumpHeader, toDumpString, toDumpString, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
checkCancelled, checkForErrorAndRethrow, executeSubTask, getName, getResult, getRunStateFlag, getTaskId, getType, handleCancellation, handleFailure, isCancellable, setCancellable, setName, setResult, setRunStateFlag, sleepChecked
getApplicationContext
execute
addMonitor, clearMonitors, getMonitorCount, getMonitors, hasMonitor, removeMonitor
getLogChannel, isLogDebug, isLogVerbose, logDebug, logDump, logError, logError, logFatal, logInfo, logVerbose, logWarning, setLogChannel
wasCancelled
getTaskId
toDumpString, toDumpString, toString
protected 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
AsyncTask
submit
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
AsyncTask
submit
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()
AsyncTask
getProcessingDuration
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.Exception
public final void setReleaseTimeout(long releaseTimeoutMillis)
AsyncTask
AsyncTask.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()
AsyncTask
releaseStart
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.InterruptedException
protected 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.Exception
for synchronous execution
public 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.Exception
public final void executeChildTask(AsyncTask childTask) throws java.lang.Exception
ApplicationContext
.executeChildTask
in interface AsyncTask<C extends ApplicationContext>
java.lang.Exception
executeChildTask(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 LocalCancellableTask
public final void run()
executeAsync()
method. It manages the RunStateFlag
and drives the
TaskMonitor
.run
in interface java.lang.Runnable
protected final void handleStart() throws java.lang.Exception
handleStart
in class AbstractAsyncExecutable<C extends ApplicationContext>
java.lang.Exception
protected 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.Exception
protected 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()
TaskManager
protected void deregisterTask()
TaskManager
public void notifyProceeded(java.lang.Object result, int step, int maxSteps, java.lang.String message)
AbstractAsyncExecutable
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
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()
AsyncTask
isRunning
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
AsyncTask
awaitFinished
in interface AsyncTask<C extends ApplicationContext>
java.util.concurrent.TimeoutException
AsyncTask.awaitFinished()
public ToString toString(ToString s)
Dumpable
toString
in interface Dumpable
toString
in class DumpableObject
Copyright © 2014 EsprIT-Systems. All Rights Reserved.