Class UiAsyncTasks
Use UiAsyncTasks when you don't need to handle asynchronous task progress or display a modal dialog with a
progress indicator. Otherwise, consider using BackgroundTaskManager and
BackgroundTask.
The class wraps actions that perform tasks (for example, loading data) into a special wrapper
(DelegatingSecuritySupplier or DelegatingSecurityRunnable). These wrappers set the original
SecurityContext before executing the action. After that, all
services invoked in the asynchronous task will be executed with the permissions of the current user.
Actions that process the result can update Vaadin UI components because they are wrapped by the
UI.access(Command) method.
Usage example:
uiAsyncTasks.supplierConfigurer(() -> customerService.loadCustomers())
.withResultHandler(customers -> {
customersDc.getMutableItems().addAll(customers);
notifications.create("Customers loaded: " + customers.size()).show();
})
.withExceptionHandler(ex -> {
errorTextField.setValue(ex.getMessage());
})
.supplyAsync();
By default, asynchronous tasks are executed with the default timeout configured by the
UiAsyncTaskProperties.defaultTimeoutSec. After this timeout, the TimeoutException is thrown.
If asynchronous task doesn't return any result, use the runnableConfigurer(Runnable) builder for configuring
and running the task.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classAbstract base class for asynchronous task builders.classA builder class for configuring and executing asynchronous tasks that do not return a result.classA builder class for configuring and executing asynchronous tasks that return a result. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ExecutorServiceprotected voidonInit()protected voidrunnableConfigurer(Runnable asyncTask) Creates a builder for an asynchronous task that does not return a result.voidsetDefaultExceptionHandler(Function<Throwable, Void> defaultExceptionHandler) supplierConfigurer(Supplier<T> asyncTask) Creates a builder for an asynchronous task that returns a result.
-
Constructor Details
-
UiAsyncTasks
-
-
Method Details
-
onInit
@PostConstruct protected void onInit() -
preDestroy
@PreDestroy protected void preDestroy() -
supplierConfigurer
Creates a builder for an asynchronous task that returns a result. The task is executed on a separate thread. The result can be handled with aConsumerthat is set using theUiAsyncTasks.SupplierConfigurer.withResultHandler(Consumer)or left unhandled to get the result using theCompletableFutureAPI.- Type Parameters:
T- the result type- Parameters:
asyncTask- the task to execute- Returns:
- a builder for an asynchronous task that returns a result
-
runnableConfigurer
Creates a builder for an asynchronous task that does not return a result. The task is executed on a separate thread. The action that must be performed after the asynchronous task is completed may be set using theUiAsyncTasks.RunnableConfigurer.withResultHandler(Runnable)method.- Parameters:
asyncTask- the task to execute- Returns:
- a builder for an asynchronous task that does not return a result
-
createExecutorService
-
createDefaultExceptionHandler
-
setDefaultExceptionHandler
-