Class BackgroundTask<T,V>
- Type Parameters:
T- task progress measurement unitV- result type
- Direct Known Subclasses:
LocalizedTaskWrapper,MBeanOperationResultView.OperationBackgroundTask,NotificationsIndicator.ReloadCountTask,ResetPasswordView.ResetPasswordBackgroundTask,SupersetDashboard.FetchGuestTokenTask
BackgroundWorker.
If the task is associated with a view through "view" constructor parameter, it will be canceled when the view is closed.
If timeout passed to constructor is exceeded, the task is canceled by special
BackgroundTaskWatchDog 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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceListener of the task life cycle events, complementary to the tasks own methods:progress(List),done(Object),canceled().static classA default implementation of theBackgroundTask.ProgressListenerinterface with empty method bodies. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBackgroundTask(long timeoutSeconds) Creates a task with timeout in defaultTimeUnit.SECONDSunit.protectedBackgroundTask(long timeoutSeconds, View<?> view) Create a task with timeout in defaultTimeUnit.SECONDSunit.protectedBackgroundTask(long timeout, TimeUnit timeUnit) Creates a task with timeout.protectedBackgroundTask(long timeout, TimeUnit timeUnit, View<?> view) Creates a task with timeout. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddProgressListener(BackgroundTask.ProgressListener<T, V> progressListener) Add additional progress listener.voidcanceled()Provides exclusive access to this UI from outside a request handling thread when the task is canceled byBackgroundTaskHandler.cancel()invocation.voidProvides exclusive access to this UI from outside a request handling thread when the task is completed.final View<?>Called by the execution environment in UI thread to prepare some execution parameters.final List<BackgroundTask.ProgressListener<T,V>> Additional progress listeners.final longfinal longbooleanProvides exclusive access to this UI from outside a request handling thread if the taskrun(TaskLifeCycle)method raised an exception.booleanProvides exclusive access to this UI from outside a request handling thread if the task timeout is exceeded.voidProvides exclusive access to this UI from outside a request handling thread on progress change.final voidremoveProgressListener(BackgroundTask.ProgressListener<T, V> progressListener) Removes a progress listener.abstract Vrun(TaskLifeCycle<T> taskLifeCycle) Main method that performs a task.
-
Constructor Details
-
BackgroundTask
Creates a task with timeout.- Parameters:
timeout- timeouttimeUnit- timeout time unitview- owner view
-
BackgroundTask
Creates a task with timeout.The task will not be associated with any
View.- Parameters:
timeout- timeouttimeUnit- timeout time unit
-
BackgroundTask
protected BackgroundTask(long timeoutSeconds) Creates a task with timeout in defaultTimeUnit.SECONDSunit.
The task will not be associated with anyView.- Parameters:
timeoutSeconds- timeout in seconds
-
BackgroundTask
Create a task with timeout in defaultTimeUnit.SECONDSunit.- Parameters:
timeoutSeconds- timeout in secondsview- owner view
-
-
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 a working thread
- In long loops check
-
done
Provides exclusive access to this UI from outside a request handling thread when the task is completed.Please note that the command might be invoked on a different thread or later on the current thread, which means that custom thread locals might not have the expected values when the command is executed.
UI.getCurrent(),VaadinSession.getCurrent()andVaadinService.getCurrent()are set according to this UI before executing the command. Other standard CurrentInstance values such asVaadinService.getCurrentRequest()andVaadinService.getCurrentResponse()will not be defined.- Parameters:
result- result of execution returned byrun(TaskLifeCycle)method- See Also:
-
UI.access(Command)
-
canceled
public void canceled()Provides exclusive access to this UI from outside a request handling thread when the task is canceled byBackgroundTaskHandler.cancel()invocation.This method is not called in case of timeout expiration or owner view closing.
Please note that the command might be invoked on a different thread or later on the current thread, which means that custom thread locals might not have the expected values when the command is executed.
UI.getCurrent(),VaadinSession.getCurrent()andVaadinService.getCurrent()are set according to this UI before executing the command. Other standard CurrentInstance values such asVaadinService.getCurrentRequest()andVaadinService.getCurrentResponse()will not be defined.- See Also:
-
UI.access(Command)
-
handleTimeoutException
public boolean handleTimeoutException()Provides exclusive access to this UI from outside a request handling thread if the task timeout is exceeded.Please note that the command might be invoked on a different thread or later on the current thread, which means that custom thread locals might not have the expected values when the command is executed.
UI.getCurrent(),VaadinSession.getCurrent()andVaadinService.getCurrent()are set according to this UI before executing the command. Other standard CurrentInstance values such asVaadinService.getCurrentRequest()andVaadinService.getCurrentResponse()will not be defined.- Returns:
- true if this method implementation actually handles this event. Used for chaining handlers.
- See Also:
-
UI.access(Command)
-
handleException
Provides exclusive access to this UI from outside a request handling thread if the taskrun(TaskLifeCycle)method raised an exception.Please note that the command might be invoked on a different thread or later on the current thread, which means that custom thread locals might not have the expected values when the command is executed.
UI.getCurrent(),VaadinSession.getCurrent()andVaadinService.getCurrent()are set according to this UI before executing the command. Other standard CurrentInstance values such asVaadinService.getCurrentRequest()andVaadinService.getCurrentResponse()will not be defined.- Parameters:
ex- exception- Returns:
- true if this method implementation actually handles the exception. Used for chaining handlers.
- See Also:
-
UI.access(Command)
-
progress
Provides exclusive access to this UI from outside a request handling thread on progress change.Please note that the command might be invoked on a different thread or later on the current thread, which means that custom thread locals might not have the expected values when the command is executed.
UI.getCurrent(),VaadinSession.getCurrent()andVaadinService.getCurrent()are set according to this UI before executing the command. Other standard CurrentInstance values such asVaadinService.getCurrentRequest()andVaadinService.getCurrentResponse()will not be defined.- Parameters:
changes- list of changes since previous invocation- See Also:
-
UI.access(Command)
-
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
-
getOwnerView
- Returns:
- owner view
-
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
Removes a progress listener.- Parameters:
progressListener- listener
-