Class AsyncTaskManager<R>

java.lang.Object
org.dellroad.stuff.vaadin24.util.AsyncTaskManager<R>
Type Parameters:
R - asynchronous task result type

public class AsyncTaskManager<R> extends Object
Allows applications to safely manage asynchronous tasks that execute in the background (without holding the VaadinSession lock) entirely within the context of a locked VaadinSession.

Instances of this class manage some background activity or task that is initiated from within a VaadinSession. The task runs asynchronously in the background without holding the VaadinSession lock. Once completed, the result is reported back to the locked VaadinSession and the configured result consumer, if any.

Only one such task is allowed to be executing at any given time: if a second task is started while an existing task is still in progress, the first task is automatically canceled. Tasks are initiated via startTask() and may be canceled at any time via cancelTask().

Results returned from successful task executions are delivered to the configured result consumer, if any.

Safety Guarantees

This class handles all required synchronization and locking. It guarantees that at most one background task can be executing at a time, that all operations are atomic, that listener notifications are delivered in proper order, and that no race conditions can occur. For example, if a background task tries to report back at the same time a Vaadin thread invokes cancelTask(), then the task will always appear to have either completed successfully or been canceled.

Instances bind to the current VaadinSession at construction time and may only be used with that session. If any method is invoked with the wrong VaadinSession locking state, an immediate exception is thrown. Therefore, thread safety is not only provided but enforced.

If a current UI exists when startTask(org.dellroad.stuff.vaadin24.util.AsyncTask<? extends R>) is invoked, it will also be restored (along with the current VaadinSession) when any corresponding callbacks are invoked, unless it has since been detached.

Event Notifications

Instances support event notification via addAsyncTaskStatusChangeListener(). All notifications are delivered within the context of the locked VaadinSession.

On task start, a STARTED notification is generated. When the task finishes, the outcome - one of: COMPLETED, CANCELED, or FAILED - is reported.

Proper ordering of event notifications is guaranteed:

  • Exactly one STARTED notification and exactly one COMPLETED, CANCELED, or FAILED notification will be delivered for each task initiated by startTask().
  • STARTED notifications are always delivered before the corresponding COMPLETED, CANCELED, or FAILED notification for the same task.
  • The COMPLETED, CANCELED, or FAILED notification for a task is always delivered before the STARTED notification for any subsequent task.
  • Tasks are executed, and corresponding notifications are delivered, in the same order that they are started.
See Also: