Package io.jmix.ui.executor
Class BackgroundTask<T,V>
java.lang.Object
io.jmix.ui.executor.BackgroundTask<T,V>
- Type Parameters:
T
- task progress measurement unitV
- result type
- Direct Known Subclasses:
ImapMessageEdit.InitAttachmentTask
,ImapMessageEdit.InitBodyTask
,LocalizedTaskWrapper
,MBeanOperationResultScreen.OperationBackgroundTask
Background task for execute by
If the task is associated with a screen through ownerFrame constructor parameter, it will be canceled when the screen is closed.
If timeout passed to constructor is exceeded, the task is canceled by special
Simplest usage example:
BackgroundWorker
. If the task is associated with a screen through ownerFrame constructor parameter, it will be canceled when the screen is closed.
If timeout passed to constructor is exceeded, the task is canceled by special
WatchDog
thread.
Simplest usage example:
BackgroundTask<Integer, Void> task = new BackgroundTask<Integer, Void>(10, this) {
public Void run(TaskLifeCycle<Integer> taskLifeCycle) throws Exception {
for (int i = 0; i < 5; i++) {
TimeUnit.SECONDS.sleep(1);
}
return null;
}
};
BackgroundTaskHandler taskHandler = backgroundWorker.handle(task);
taskHandler.execute();
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Listener of the task life cycle events, complementary to the tasks own methods:progress(List)
,done(Object)
,canceled()
.static class
-
Constructor Summary
ModifierConstructorDescriptionprotected
BackgroundTask
(long timeoutSeconds) Create a task with timeout in default SECONDS unit.protected
BackgroundTask
(long timeoutSeconds, Screen screen) Create a task with timeout in default SECONDS unit.protected
BackgroundTask
(long timeout, TimeUnit timeUnit) Create a task with timeout.protected
BackgroundTask
(long timeout, TimeUnit timeUnit, Screen screen) Create a task with timeout. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
addProgressListener
(BackgroundTask.ProgressListener<T, V> progressListener) Add additional progress listener.void
canceled()
Called by the execution environment in UI thread if the task is canceled byBackgroundTaskHandler.cancel()
invocation.void
Called by the execution environment in UI thread when the task is completed.final Screen
Deprecated.final Screen
Called by the execution environment in UI thread to prepare some execution parameters.final List<BackgroundTask.ProgressListener<T,
V>> Additional progress listeners.final long
final long
boolean
Called by the execution environment in UI thread if the taskrun(TaskLifeCycle)
method raised an exception.boolean
Called by the execution environment in UI thread if the task timeout is exceeded.void
Called by the execution environment in UI thread on progress change.final void
removeProgressListener
(BackgroundTask.ProgressListener<T, V> progressListener) Remove a progress listener.abstract V
run
(TaskLifeCycle<T> taskLifeCycle) Main method that performs a task.
-
Constructor Details
-
BackgroundTask
Create a task with timeout.- Parameters:
timeout
- timeouttimeUnit
- timeout time unitscreen
- owner frame
-
BackgroundTask
Create a task with timeout.
The task will not be associated with any window.- Parameters:
timeout
- timeouttimeUnit
- timeout time unit
-
BackgroundTask
protected BackgroundTask(long timeoutSeconds) Create a task with timeout in default SECONDS unit.
The task will not be associated with any window.- Parameters:
timeoutSeconds
- timeout in seconds
-
BackgroundTask
Create a task with timeout in default SECONDS unit.- Parameters:
timeoutSeconds
- timeout in secondsscreen
- owner frame
-
-
Method Details
-
run
Main method that performs a task.
Called by the execution environment in a separate working thread.
Implementation of this method should support interruption:- In long loops check
TaskLifeCycle.isInterrupted()
and return if it is true - Don't swallow
InterruptedException
- return from the method or don't catch it at all
- Parameters:
taskLifeCycle
- lifecycle object that allows the main method to interact with the execution environment- Returns:
- task result
- Throws:
Exception
- exception in working thread
- In long loops check
-
done
Called by the execution environment in UI thread when the task is completed.- Parameters:
result
- result of execution returned byrun(TaskLifeCycle)
method
-
canceled
public void canceled()Called by the execution environment in UI thread if the task is canceled byBackgroundTaskHandler.cancel()
invocation.
This method is not called in case of timeout expiration or owner window closing. -
handleTimeoutException
public boolean handleTimeoutException()Called by the execution environment in UI thread if the task timeout is exceeded.- Returns:
- true if this method implementation actually handles this event. Used for chaining handlers.
-
handleException
Called by the execution environment in UI thread if the taskrun(TaskLifeCycle)
method raised an exception.- Parameters:
ex
- exception- Returns:
- true if this method implementation actually handles the exception. Used for chaining handlers.
-
progress
Called by the execution environment in UI thread on progress change.- Parameters:
changes
- list of changes since previous invocation
-
getParams
Called by the execution environment in UI thread to prepare some execution parameters. These parameters can be requested by the working thread inside therun(TaskLifeCycle)
method by callingTaskLifeCycle.getParams()
.- Returns:
- parameters map or null if parameters are not needed
-
getOwnerFrame
Deprecated.UsegetOwnerScreen()
- Returns:
- owner screen
-
getOwnerScreen
- Returns:
- owner screen
-
getTimeoutMilliseconds
public final long getTimeoutMilliseconds()- Returns:
- timeout in ms
-
getTimeoutSeconds
public final long getTimeoutSeconds()- Returns:
- timeout in sec
-
addProgressListener
Add additional progress listener.- Parameters:
progressListener
- listener
-
getProgressListeners
Additional progress listeners.- Returns:
- copy of the progress listeners collection
-
removeProgressListener
Remove a progress listener.- Parameters:
progressListener
- listener
-
getOwnerScreen()