The new API is intended to be used instead of Background Task in simple cases. Background tasks are powerful, but sometimes users don't need all their featues like showing a modal dialog or handling the task execution progress.
For a problem that sounds like "load some data in the background and after the data is loaded update some UI component" the new UiAsyncTasks service may be used.
Both supplyAsync() and runAsync() method return a CompletableFuture.
For the thread that executes the main asynchronous task the current authentication is copied to the SecurityContext, so the code will be executed with the permissions of the user who is working with the UI View.
The code that handles the result may access Vaadin components because the UiAsyncTasks service wraps it into the UI.access(() -> ...).
Exception Handling
The default exception handler will write a stacktrace to the application log. If you need a custom behavior, use the withExceptionHandler() method.
When the timeout is exceeded the TimeoutException is thrown. The TimeoutException can be processed in the withExceptionHandler() method.
If no explicit timeout is defined, then the default timeout value from the application properties is used.
Configuration Properties
# default timeout value for CompletableFutures
jmix.ui.async-task.default-timeout-sec = 300
# Maximum thread pool size for the executor service used for asynchronous tasks execution
jmix.ui.async-task.executor-service.maximum-pool-size = 10
Overview
The new API is intended to be used instead of Background Task in simple cases. Background tasks are powerful, but sometimes users don't need all their featues like showing a modal dialog or handling the task execution progress.
For a problem that sounds like "load some data in the background and after the data is loaded update some UI component" the new
UiAsyncTasks
service may be used.Usage examples:
When the asynchronous task returns no result, the
runAsyncBuilder
may be used:Both
supplyAsync()
andrunAsync()
method return aCompletableFuture
.For the thread that executes the main asynchronous task the current authentication is copied to the SecurityContext, so the code will be executed with the permissions of the user who is working with the UI View.
The code that handles the result may access Vaadin components because the
UiAsyncTasks
service wraps it into theUI.access(() -> ...)
.Exception Handling
The default exception handler will write a stacktrace to the application log. If you need a custom behavior, use the
withExceptionHandler()
method.Timeout
Timeout may be set for the individual CompletableFuture:
When the timeout is exceeded the
TimeoutException
is thrown. TheTimeoutException
can be processed in thewithExceptionHandler()
method.If no explicit timeout is defined, then the default timeout value from the application properties is used.
Configuration Properties