Interface CentralWorkQueue


@Beta public interface CentralWorkQueue
The CentralWorkQueue provides an api to submit and coordinate long-running or resource intensive tasks. The tasks are executed in parallel, but if some tasks access the same resource this can become a problem. To avoid this problem a task can be enqueued with a lock e.g.:

   queue.append().locks("my-resources").enqueue(MyTask.class)
 
No tasks with the same lock will run in parallel. The example above locks a whole group of resources. It is possible to assign the lock to a more specific resource by adding the id parameter to the lock e.g.:

   queue.append().locks("my-resources", "42").enqueue(MyTask.class)
 
This will ensure, that no task with a lock for the my-resource 42 or for my-resources will run at the same time as the task which is enqueued in the example above. But this will also allow a task for my-resources with an id other than 42 can run in parallel. All tasks are executed with the permissions of the user which enqueues the task. If the task should run as admin, the CentralWorkQueue.Enqueue.runAsAdmin() method can be used. Tasks which could not be finished, before a restart of shutdown of the server, will be restored and executed on startup. In order to achieve the persistence of tasks, the enqueued task must be provided as a class or it must be serializable. This could become unhandy if the task has parameters and dependencies which must be injected. The injected objects should not be serialized with the task. In order to avoid this, dependencies should be declared as transient and injected via setters instead of the constructor parameters e.g.:

   public class MyTask implements Task {

     private final Repository repository;
     private transient RepositoryServiceFactory repositoryServiceFactory;

     public MyTask(Repository repository) {
       this.repository = repository;
     }

     @Inject
     public void setRepositoryServiceFactory(RepositoryServiceFactory repositoryServiceFactory) {
       this.repositoryServiceFactory = repositoryServiceFactory;
     }

     @Override
     public void run() {
       // do something with the repository and the repositoryServiceFactory
     }

   }
 
The CentralWorkQueue will inject the requested members before the run method of the task is executed.
Since:
2.23.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Builder interface for the enqueueing of a new task.
  • Method Summary

    Modifier and Type
    Method
    Description
    Append a new task to the central work queue.
    int
    Returns the count of pending or running tasks.
  • Method Details

    • append

      Append a new task to the central work queue. The method will return a builder interface to configure how the task will be enqueued.
      Returns:
      builder api for enqueue a task
    • getSize

      int getSize()
      Returns the count of pending or running tasks.