Class BackgroundTaskExecutor<IDType,RightType>
- Type Parameters:
IDType- the type of the request ID of the underlying access managerRightType- the type of the rights handled by the underlying access manager
TaskExecutor specified at
construction time. The background tasks will also have a chance to report
their progress (or whatever else they want) and update components on the
UI thread (as defined by the provided UiExecutorProvider).
Every BackgroundTaskExecutor has an associated AccessManager
and tasks are executed in the context of an AccessToken of this
access manager. Therefore these tasks may only execute if a given right is
available. This allows for a convenient way to deny executing background
tasks if, for example, an other conflicting task is currently being executed
or simply allow to disable a button while the task is executing.
In case a background task throws an exception, the exception will be logged
in a SEVERE log message (unless the exception is an instance of
OperationCanceledException).
Thread safety
All of the methods of this class are allowed to be accessed from multiple threads concurrently.Synchronization transparency
Methods of this class are not synchronization transparent.- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionBackgroundTaskExecutor(AccessManager<IDType, RightType> accessManager, TaskExecutor executor, UiExecutorProvider uiExecutorProvider) Creates a newBackgroundTaskExecutorwith the given access manager andTaskExecutor. -
Method Summary
Modifier and TypeMethodDescriptionscheduleToExecute(AccessRequest<? extends IDType, ? extends RightType> request, BackgroundTask task) Submits the givenBackgroundTaskto theTaskExecutorspecified at construction time after all the access tokens currently blocking access has been released.scheduleToExecute(CancellationToken cancelToken, AccessRequest<? extends IDType, ? extends RightType> request, BackgroundTask task) Submits the givenBackgroundTaskto theTaskExecutorspecified at construction time after all the access tokens currently blocking access has been released.tryExecute(AccessRequest<? extends IDType, ? extends RightType> request, BackgroundTask task) Submits the givenBackgroundTaskto theTaskExecutorspecified at construction time if the given access rights can successfully acquired from the underlying access manager.tryExecute(CancellationToken cancelToken, AccessRequest<? extends IDType, ? extends RightType> request, BackgroundTask task) Submits the givenBackgroundTaskto theTaskExecutorspecified at construction time if the given access rights can successfully acquired from the underlying access manager.
-
Constructor Details
-
BackgroundTaskExecutor
public BackgroundTaskExecutor(AccessManager<IDType, RightType> accessManager, TaskExecutor executor, UiExecutorProvider uiExecutorProvider) Creates a newBackgroundTaskExecutorwith the given access manager andTaskExecutor.The specified
TaskExecutoris recommended to execute tasks on a separate thread instead of the calling thread, however for debugging purposes it may be beneficial to use theSyncTaskExecutor. The executor should execute tasks on a separate thread to allow methods of this class to be called from the UI thread without actually blocking the UI.- Parameters:
accessManager- theAccessManagerfrom which access tokens are requested to execute tasks in their context. This argument cannot benull.executor- theTaskExecutorwhich actually executes submitted tasks. This argument cannot benull.uiExecutorProvider- a factory to create executors running tasks on the UI thread of the associated UI framework. This argument cannot benull.- Throws:
NullPointerException- thrown if any of the arguments isnull
-
-
Method Details
-
scheduleToExecute
public Collection<AccessToken<IDType>> scheduleToExecute(AccessRequest<? extends IDType, ? extends RightType> request, BackgroundTask task) Submits the givenBackgroundTaskto theTaskExecutorspecified at construction time after all the access tokens currently blocking access has been released. This method differs from thetryExecutemethod only by invoking thegetScheduledAccessmethod of the underlying access token instead of thetryGetAccessmethod.This method will first attempt to acquire an
AccessTokenfrom the underlying access manager using the specifiedAccessRequest. If the request cannot be granted immediately, then the underlying access manager should execute the background task right after the specified request becomes available.The submitted background task can be canceled by releasing the
AccessTokenacquired by this method. Releasing the acquired access token will cause theCancellationTokenof the submitted task to signal a cancellation request (or it is possible that the task will not even be executed). not necessarily support it). This argument cannot benull.- Parameters:
request- theAccessRequestused to acquire theAccessTokenin whose context the task is to be executed. This argument cannot benull.task- theBackgroundTaskto be submitted to the underlyingTaskExecutor. This argument cannot benull.- Returns:
- the tokens preventing the background task from being executed
immediately. This method never returns
nulland if it returns an empty collection it should mean that the background task is scheduled to be executed. - Throws:
NullPointerException- thrown if any of the arguments isnull
-
scheduleToExecute
public Collection<AccessToken<IDType>> scheduleToExecute(CancellationToken cancelToken, AccessRequest<? extends IDType, ? extends RightType> request, BackgroundTask task) Submits the givenBackgroundTaskto theTaskExecutorspecified at construction time after all the access tokens currently blocking access has been released. This method differs from thetryExecutemethod only by invoking thegetScheduledAccessmethod of the underlying access token instead of thetryGetAccessmethod.This method will first attempt to acquire an
AccessTokenfrom the underlying access manager using the specifiedAccessRequest. If the request cannot be granted immediately, then the underlying access manager should execute the background task right after the specified request becomes available.There are in general two ways to cancel the submitted background task: One way is to request cancellation through the specified
CancellationTokenand the other is to release theAccessTokenacquired by this method. Releasing the acquired access token will cause theCancellationTokenof the submitted task to signal a cancellation request (or it is possible that the task will not even be executed).- Parameters:
cancelToken- theCancellationTokenwhich is used to signal that the background task should be canceled. If the background task has not yet started, it may not be executed at all (the underlyingTaskExecutormay not necessarily support it). This argument cannot benull.request- theAccessRequestused to acquire theAccessTokenin whose context the task is to be executed. This argument cannot benull.task- theBackgroundTaskto be submitted to the underlyingTaskExecutor. This argument cannot benull.- Returns:
- the tokens preventing the background task from being executed
immediately. This method never returns
nulland if it returns an empty collection it should mean that the background task is scheduled to be executed. - Throws:
NullPointerException- thrown if any of the arguments isnull
-
tryExecute
public Collection<AccessToken<IDType>> tryExecute(AccessRequest<? extends IDType, ? extends RightType> request, BackgroundTask task) Submits the givenBackgroundTaskto theTaskExecutorspecified at construction time if the given access rights can successfully acquired from the underlying access manager.This method will first attempt to acquire an
AccessTokenfrom the underlying access manager using the specifiedAccessRequest. If the access manager refuses this request this method will immediately returnnull. If access was granted for this method, the specified background task will be submitted to theTaskExecutorspecified at construction time in the context of the access token. Therefore theexecutemethod of the task will only be executed while the acquired was not withdrawn.The submitted background task can be canceled by releasing the
AccessTokenacquired by this method. Releasing the acquired access token will cause theCancellationTokenof the submitted task to signal a cancellation request (or it is possible that the task will not even be executed).- Parameters:
request- theAccessRequestused to acquire theAccessTokenin whose context the task is to be executed. This argument cannot benull.task- theBackgroundTaskto be submitted to the underlyingTaskExecutor. This argument cannot benull.- Returns:
nullif the access was granted to execute the background task and it was submitted to the underlyingTaskExecutoror if access was refused, the access tokens blocking access the specified request. Note that if this method returns an empty collection, it means that access was refused but the underlying access manager did not return the reason of refusal.- Throws:
NullPointerException- thrown if any of the arguments isnull
-
tryExecute
public Collection<AccessToken<IDType>> tryExecute(CancellationToken cancelToken, AccessRequest<? extends IDType, ? extends RightType> request, BackgroundTask task) Submits the givenBackgroundTaskto theTaskExecutorspecified at construction time if the given access rights can successfully acquired from the underlying access manager.This method will first attempt to acquire an
AccessTokenfrom the underlying access manager using the specifiedAccessRequest. If the access manager refuses this request this method will immediately returnnull. If access was granted for this method, the specified background task will be submitted to theTaskExecutorspecified at construction time in the context of the access token. Therefore theexecutemethod of the task will only be executed while the acquired was not withdrawn.There are in general two ways to cancel the submitted background task: One way is to request cancellation through the specified
CancellationTokenand the other is to release theAccessTokenacquired by this method. Releasing the acquired access token will cause theCancellationTokenof the submitted task to signal a cancellation request (or it is possible that the task will not even be executed).- Parameters:
cancelToken- theCancellationTokenwhich is used to signal that the background task should be canceled. If the background task has not yet started, it may not be executed at all (the underlyingTaskExecutormay not necessarily support it). This argument cannot benull.request- theAccessRequestused to acquire theAccessTokenin whose context the task is to be executed. This argument cannot benull.task- theBackgroundTaskto be submitted to the underlyingTaskExecutor. This argument cannot benull.- Returns:
nullif the access was granted to execute the background task and it was submitted to the underlyingTaskExecutoror if access was refused, the access tokens blocking access the specified request. Note that if this method returns an empty collection, it means that access was refused but the underlying access manager did not return the reason of refusal.- Throws:
NullPointerException- thrown if any of the arguments isnull
-