Class UiAsyncTasks

java.lang.Object
io.jmix.flowui.asynctask.UiAsyncTasks

@Component("flowui_UiAsyncTasks") public class UiAsyncTasks extends Object
The class provides methods for executing asynchronous tasks from UI views. The class may be used when, in the UI view, you need to execute some long-running task in a separate thread, for example, loading data from a database, and when the task is completed (data is loaded), update the UI using the returned data.

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.

See Also:
  • Constructor Details

  • Method Details

    • onInit

      @PostConstruct protected void onInit()
    • preDestroy

      @PreDestroy protected void preDestroy()
    • supplierConfigurer

      public <T> UiAsyncTasks.SupplierConfigurer<T> supplierConfigurer(Supplier<T> asyncTask)
      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 a Consumer that is set using the UiAsyncTasks.SupplierConfigurer.withResultHandler(Consumer) or left unhandled to get the result using the CompletableFuture API.
      Type Parameters:
      T - the result type
      Parameters:
      asyncTask - the task to execute
      Returns:
      a builder for an asynchronous task that returns a result
    • runnableConfigurer

      public UiAsyncTasks.RunnableConfigurer runnableConfigurer(Runnable asyncTask)
      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 the UiAsyncTasks.RunnableConfigurer.withResultHandler(Runnable) method.
      Parameters:
      asyncTask - the task to execute
      Returns:
      a builder for an asynchronous task that does not return a result
    • createExecutorService

      protected ExecutorService createExecutorService()
    • createDefaultExceptionHandler

      protected Function<Throwable,Void> createDefaultExceptionHandler()
    • setDefaultExceptionHandler

      public void setDefaultExceptionHandler(Function<Throwable,Void> defaultExceptionHandler)