Class JobManager

java.lang.Object
org.apache.ofbiz.service.job.JobManager

public final class JobManager extends Object
Job manager. The job manager queues and manages jobs. Client code can queue a job to be run immediately by calling the runJob(Job) method, or schedule a job to be run later by calling the schedule(String, String, String, Map, long, int, int, int, long, int) method. Scheduled jobs are persisted in the JobSandbox entity.

A scheduled job's start time is an approximation - the actual start time will depend on the job manager/job poller configuration (poll interval) and the load on the server. Scheduled jobs might be rescheduled if the server is busy. Therefore, applications requiring a precise job start time should use a different mechanism to schedule the job.

  • Field Details

    • INSTANCE_ID

      public static final String INSTANCE_ID
  • Method Details

    • getInstance

      public static JobManager getInstance(Delegator delegator, boolean enablePoller)
      Returns a JobManager instance.
      Parameters:
      delegator - the delegator
      enablePoller - Enables polling of the JobSandbox entity.
      Returns:
      the instance
      Throws:
      IllegalStateException - if the Job Manager is shut down.
    • shutDown

      public static void shutDown()
      Shuts down all job managers. This method is called when OFBiz shuts down.
    • getDelegator

      public Delegator getDelegator()
      Returns the Delegator.
    • getDispatcher

      public LocalDispatcher getDispatcher()
      Returns the LocalDispatcher.
    • getPoolState

      public Map<String,Object> getPoolState()
      Get a List of each threads current state.
      Returns:
      List containing a Map of each thread's state.
    • isAvailable

      public boolean isAvailable()
      Return true if the jobManager can run job.
      Returns:
      boolean.
    • poll

      protected List<Job> poll(int limit)
      Scans the JobSandbox entity and returns a list of jobs that are due to run. Returns an empty list if there are no jobs due to run. This method is called by the JobPoller polling thread.
    • getJobsToPurge

      public static List<GenericValue> getJobsToPurge(Delegator delegator, String poolId, String instanceId, int limit, Timestamp purgeTime) throws GenericEntityException
      Throws:
      GenericEntityException
    • reloadCrashedJobs

      public void reloadCrashedJobs()
    • runJob

      public void runJob(Job job) throws JobManagerException
      Queues a Job to run now.
      Throws:
      IllegalStateException - if the Job Manager is shut down.
      JobManagerException
    • schedule

      public void schedule(String serviceName, Map<String,? extends Object> context, long startTime, int frequency, int interval, int count) throws JobManagerException
      Schedule a job to start at a specific time with specific recurrence info
      Parameters:
      serviceName - The name of the service to invoke
      context - The context for the service
      startTime - The time in milliseconds the service should run
      frequency - The frequency of the recurrence (HOURLY, DAILY, MONTHLY etc)
      interval - The interval of the frequency recurrence
      count - The number of times to repeat
      Throws:
      JobManagerException - the job manager exception
    • schedule

      public void schedule(String serviceName, Map<String,? extends Object> context, long startTime, int frequency, int interval, int count, long endTime) throws JobManagerException
      Schedule a job to start at a specific time with specific recurrence info
      Parameters:
      serviceName - The name of the service to invoke
      context - The context for the service
      startTime - The time in milliseconds the service should run
      frequency - The frequency of the recurrence (HOURLY, DAILY, MONTHLY etc)
      interval - The interval of the frequency recurrence
      count - The number of times to repeat
      endTime - The time in milliseconds the service should expire
      Throws:
      JobManagerException - the job manager exception
    • schedule

      public void schedule(String serviceName, Map<String,? extends Object> context, long startTime, int frequency, int interval, long endTime) throws JobManagerException
      Schedule a job to start at a specific time with specific recurrence info
      Parameters:
      serviceName - The name of the service to invoke
      context - The context for the service
      startTime - The time in milliseconds the service should run
      frequency - The frequency of the recurrence (HOURLY, DAILY, MONTHLY etc)
      interval - The interval of the frequency recurrence
      endTime - The time in milliseconds the service should expire
      Throws:
      JobManagerException - the job manager exception
    • schedule

      public void schedule(String poolName, String serviceName, Map<String,? extends Object> context, long startTime, int frequency, int interval, int count, long endTime) throws JobManagerException
      Schedule a job to start at a specific time with specific recurrence info
      Parameters:
      poolName - The name of the pool to run the service from
      serviceName - The name of the service to invoke
      context - The context for the service
      startTime - The time in milliseconds the service should run
      frequency - The frequency of the recurrence (HOURLY, DAILY, MONTHLY etc)
      interval - The interval of the frequency recurrence
      count - The number of times to repeat
      endTime - The time in milliseconds the service should expire
      Throws:
      JobManagerException - the job manager exception
    • schedule

      public void schedule(String poolName, String serviceName, String dataId, long startTime) throws JobManagerException
      Schedule a job to start at a specific time with specific recurrence info
      Parameters:
      poolName - The name of the pool to run the service from
      serviceName - The name of the service to invoke
      dataId - The persisted context (RuntimeData.runtimeDataId)
      startTime - The time in milliseconds the service should run
      Throws:
      JobManagerException - the job manager exception
    • schedule

      public void schedule(String jobName, String poolName, String serviceName, Map<String,? extends Object> context, long startTime, int frequency, int interval, int count, long endTime, int maxRetry) throws JobManagerException
      Schedule a job to start at a specific time with specific recurrence info
      Parameters:
      jobName - The name of the job
      poolName - The name of the pool to run the service from
      serviceName - The name of the service to invoke
      context - The context for the service
      startTime - The time in milliseconds the service should run
      frequency - The frequency of the recurrence (HOURLY, DAILY, MONTHLY etc)
      interval - The interval of the frequency recurrence
      count - The number of times to repeat
      endTime - The time in milliseconds the service should expire
      maxRetry - The max number of retries on failure (-1 for no max)
      Throws:
      JobManagerException - the job manager exception
    • schedule

      public void schedule(String jobName, String poolName, String serviceName, String dataId, long startTime, int frequency, int interval, int count, long endTime, int maxRetry) throws JobManagerException
      Schedule a job to start at a specific time with specific recurrence info
      Parameters:
      jobName - The name of the job
      poolName - The name of the pool to run the service from
      serviceName - The name of the service to invoke
      dataId - The persisted context (RuntimeData.runtimeDataId)
      startTime - The time in milliseconds the service should run
      frequency - The frequency of the recurrence (HOURLY, DAILY, MONTHLY etc)
      interval - The interval of the frequency recurrence
      count - The number of times to repeat
      endTime - The time in milliseconds the service should expire
      maxRetry - The max number of retries on failure (-1 for no max)
      Throws:
      JobManagerException - the job manager exception