Class AsyncDataProvider<T>

All Implemented Interfaces:
ConfigurableFilterDataProvider<T, SerializablePredicate<T>, SerializablePredicate<T>>, DataProvider<T, SerializablePredicate<T>>, InMemoryDataProvider<T>, Serializable

public class AsyncDataProvider<T> extends ListDataProvider<T>
A ListDataProvider whose contents are gathered in an asynchronous query.

Instances are just a ListDataProvider with support for asynchronous (re)loading of the underlying data in a background thread using an AsyncTaskManager. As a result, the UI never "locks up" while a backend data query executes.

The associated AsyncTaskManager provides status change notifications when a load operation starts, completes, fails, or is canceled. These updates can be used to drive GUI loading spinners, etc.

Load operations are initiated via load(), and may be canceled in progress via cancel(). Initiating a new load operation will cancel and replace any previous load operation still in progress.

All operations are atomic and race free. See AsyncTaskManager for details.

Queries

The DataProvider's fetch() and size() methods take a Query parameter, which includes basic filtering, sorting, and offset/limit information for the query. However, because these methods are synchronous, they not compatible with this class' aynchronous loading model. That's why this class extends ListDataProvider, which applies the Query to a list of objects already loaded into memory by other means. The purpose of this class is therefore simply to refresh that list, aynchronously, when requested.

However, there may be scenarios where the aynchronous load operation can benefit from knowing the Query currently being used, i.e., most recently passed to fetch() or size(). For such scenarios, subclasses could override these methods and trigger a new load operation if the Query has changed, etc.

See Also:
  • Constructor Details

  • Method Details

    • getAsyncTaskManager

      public AsyncTaskManager<Stream<? extends T>> getAsyncTaskManager()
      Get the AsyncTaskManager that manages background load operations.
      Returns:
      this instance's AsyncTaskManager, never null
    • load

      public long load(AsyncDataProvider.Loader<? extends T> loader)
      Trigger a new asynchronous load of this instance.

      If/when the given load task completes successfully, its results will completely replace the contents of this data provider.

      See Loader for important requirements for the behavior of loader.

      Parameters:
      loader - performs the load operation
      Returns:
      unique ID for this load attempt
      Throws:
      IllegalStateException - if the current thread is not associated with the task manager's session
      IllegalStateException - if this instance's AsyncTaskManager has no executor configured
      IllegalArgumentException - if loader is null
      See Also:
    • cancel

      public long cancel()
      Cancel any outstanding asynchronous load of this instance.
      Returns:
      the unique ID of the canceled task, if any, or zero if there is no task outstanding
      Throws:
      IllegalStateException - if the current thread is not associated with the task manager's session
      See Also:
    • updateFromLoad

      protected void updateFromLoad(long id, Stream<? extends T> stream)
      Update this data provider's internal list of items using the new data from a successful load operation and fire a refresh notification.

      This is invoked with the task manager's session locked.

      Parameters:
      id - task ID
      stream - load results
      Throws:
      IllegalStateException - if the current thread is not associated with the task manager's session
      IllegalArgumentException - if stream is null