Class PerunBeanProcessingPool<T extends PerunBean>

java.lang.Object
cz.metacentrum.perun.core.api.PerunBeanProcessingPool<T>

public class PerunBeanProcessingPool<T extends PerunBean> extends Object
Processing pool for processing PerunBeans as jobs.

This implementation is thread-safe.

You can put new waiting jobs in it and take waiting jobs to set them as running, and then remove them after work with them is finished. Jobs in processing pool are unique and can't be added if they are already in list of waiting jobs or in set of running jobs. New waiting job can skip order and be putted as first in the list of waiting jobs.

Note: this class doesn't run any task scheduler, it just provides structures and functionality to manipulate with these structures used for processing job from the pool of waiting jobs.

Author:
Michal Stava <stavamichal@gmail.com>
  • Constructor Details

    • PerunBeanProcessingPool

      public PerunBeanProcessingPool()
  • Method Details

    • getRunningJobs

      public Set<T> getRunningJobs()
      Get set of running jobs at this moment.
      Returns:
      set of running jobs at this moment
    • getWaitingJobs

      public List<T> getWaitingJobs()
      Get ordered list of waiting jobs at this moment.
      Returns:
      ordered list of waiting jobs at this moment
    • putJobIfAbsent

      public boolean putJobIfAbsent(T job, boolean asFirst)
      Put new unique job to the list of waiting jobs.

      If job is already running, return false because there would be no change. If job is already waiting, check if asFirst is true. If yes, move it (remove and add) to the first place and return true. If not, return false. If there is no such job, add it and return true.

      Uniqueness of the job in pool is mandatory and it is no difference between waiting and running status of job in this case.

      Parameters:
      job - perunBean object which defines job
      asFirst - true if job will skip order and will be placed to the list as first (LIFO)
      Returns:
      true if unique job was added or job was added to the beginning of the queue (asFirst is true), false if job exists and was not added to the beginning of the queue (asFirst is false) or it is already running
      Throws:
      InternalErrorException - if job in parameter is null
    • removeJob

      public boolean removeJob(T job)
      Remove jobs from set of running jobs. It means that we are done with this job.
      Parameters:
      job - perunBean object which defines job
      Returns:
      true if removing was successful, false if not (structure not contained the job)
    • takeJob

      public T takeJob() throws InterruptedException
      Take job from list of waiting jobs and add it to the list of running jobs.
      Returns:
      first waiting job from the list of waiting jobs
      Throws:
      InterruptedException - if acquiring of permit on semaphore was interrupted