Class SimpleTaskManager

java.lang.Object
org.dellroad.stuff.vaadin24.util.SimpleTaskManager

public class SimpleTaskManager extends Object
A simplified asynchronous task manager.

This class wraps an underlying AsyncTaskManager and provides a simplified interface, where each task has its own result type, and success and error handlers.

All of the safety guarantees provided by AsyncTaskManager are provided by this class. As with AsyncTaskManager, only one task can be executing at a time, and starting a new task automatically cancels any previous task that is still outstanding.

  • Field Details

  • Constructor Details

  • Method Details

    • getVaadinSession

      public VaadinSession getVaadinSession()
      Get the VaadinSession to which this instance is associated.
      Returns:
      this instance's VaadinSession, never null
    • setAsyncExecutor

      public void setAsyncExecutor(Function<? super Runnable,? extends Future<?>> executor)
      Configure the executor used for async tasks.

      The executor must execute tasks with this instance's VaadinSession unlocked.

      Note: when an in-progress task is canceled via cancelTask(), then Future.cancel() will be invoked on the Future returned by the executor.

      Parameters:
      executor - the thing that launches background tasks, or null for none
    • startTask

      public <T> void startTask(Callable<? extends T> action, Consumer<? super T> onSuccess, Consumer<? super Throwable> onError)
      Start a new task that returns some value.

      Any previous task that is still executing will be cancelled.

      Type Parameters:
      T - task result type
      Parameters:
      action - the task to perform
      onSuccess - callback when task is successful
      onError - callback when task fails or is interrupted
      Throws:
      IllegalStateException - if the current thread is not associated with this instance's session
    • startTask

      public void startTask(SimpleTaskManager.ThrowingRunnable action, Runnable onSuccess, Consumer<? super Throwable> onError)
      Start a new task that returns nothing.

      Any previous task that is still executing will be cancelled.

      Parameters:
      action - the task to perform
      onSuccess - callback when task is successful
      onError - callback when task fails or is interrupted
      Throws:
      IllegalStateException - if the current thread is not associated with this instance's session
    • isBusy

      public boolean isBusy()
      Determine whether there is an outstanding asynchronous task in progress.
      Returns:
      true if an asynchronous task is currently executing, otherwise false
      Throws:
      IllegalStateException - if the current thread is not associated with this instance's session
    • cancelTask

      public boolean cancelTask()
      Cancel current task, if any
      Returns:
      true if task was cancelled, otherwise false
      Throws:
      IllegalStateException - if the current thread is not associated with this instance's session