Class SimpleTaskManager
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.
-
Nested Class Summary
-
Field Summary
-
Constructor Summary
ConstructorDescriptionDefault constructor.SimpleTaskManager
(Function<? super Runnable, ? extends Future<?>> executor) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Cancel current task, if anyGet theVaadinSession
to which this instance is associated.boolean
isBusy()
Determine whether there is an outstanding asynchronous task in progress.void
setAsyncExecutor
(Function<? super Runnable, ? extends Future<?>> executor) Configure the executor used for async tasks.<T> void
startTask
(Callable<? extends T> action, Consumer<? super T> onSuccess, Consumer<? super Throwable> onError) Start a new task that returns some value.void
startTask
(SimpleTaskManager.ThrowingRunnable action, Runnable onSuccess, Consumer<? super Throwable> onError) Start a new task that returns nothing.
-
Field Details
-
taskManager
-
-
Constructor Details
-
SimpleTaskManager
public SimpleTaskManager()Default constructor.Caller must configure an async executor via
setAsyncExecutor()
.- Throws:
IllegalStateException
- if there is noVaadinSession
associated with the current thread
-
SimpleTaskManager
Constructor.- Parameters:
executor
- the executor used to execute async tasks, or null for none- Throws:
IllegalStateException
- if there is noVaadinSession
associated with the current thread
-
-
Method Details
-
getVaadinSession
Get theVaadinSession
to which this instance is associated.- Returns:
- this instance's
VaadinSession
, never null
-
setAsyncExecutor
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()
, thenFuture.cancel()
will be invoked on theFuture
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 performonSuccess
- callback when task is successfulonError
- 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 performonSuccess
- callback when task is successfulonError
- 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
-