Class JobSchedulerImpl

java.lang.Object
org.apache.activemq.util.ServiceSupport
org.apache.activemq.store.kahadb.scheduler.JobSchedulerImpl
All Implemented Interfaces:
Runnable, JobScheduler, org.apache.activemq.Service

public class JobSchedulerImpl extends ServiceSupport implements Runnable, JobScheduler
  • Method Details

    • setName

      public void setName(String name)
    • getName

      public String getName()
      Specified by:
      getName in interface JobScheduler
    • addListener

      public void addListener(JobListener l)
      Specified by:
      addListener in interface JobScheduler
    • removeListener

      public void removeListener(JobListener l)
      Specified by:
      removeListener in interface JobScheduler
    • schedule

      public void schedule(String jobId, ByteSequence payload, long delay) throws IOException
      Specified by:
      schedule in interface JobScheduler
      Throws:
      IOException
    • schedule

      public void schedule(String jobId, ByteSequence payload, String cronEntry) throws Exception
      Specified by:
      schedule in interface JobScheduler
      Throws:
      Exception
    • schedule

      public void schedule(String jobId, ByteSequence payload, String cronEntry, long delay, long period, int repeat) throws IOException
      Specified by:
      schedule in interface JobScheduler
      Throws:
      IOException
    • remove

      public void remove(long time) throws IOException
      Specified by:
      remove in interface JobScheduler
      Throws:
      IOException
    • remove

      public void remove(String jobId) throws IOException
      Specified by:
      remove in interface JobScheduler
      Throws:
      IOException
    • removeAllJobs

      public void removeAllJobs() throws IOException
      Specified by:
      removeAllJobs in interface JobScheduler
      Throws:
      IOException
    • removeAllJobs

      public void removeAllJobs(long start, long finish) throws IOException
      Specified by:
      removeAllJobs in interface JobScheduler
      Throws:
      IOException
    • getNextScheduleTime

      public long getNextScheduleTime() throws IOException
      Specified by:
      getNextScheduleTime in interface JobScheduler
      Throws:
      IOException
    • getNextScheduleJobs

      public List<Job> getNextScheduleJobs() throws IOException
      Specified by:
      getNextScheduleJobs in interface JobScheduler
      Throws:
      IOException
    • getAllJobs

      public List<Job> getAllJobs() throws IOException
      Specified by:
      getAllJobs in interface JobScheduler
      Throws:
      IOException
    • getAllJobs

      public List<Job> getAllJobs(long start, long finish) throws IOException
      Specified by:
      getAllJobs in interface JobScheduler
      Throws:
      IOException
    • process

      protected void process(Transaction tx, KahaAddScheduledJobCommand command, Location location) throws IOException
      Adds a new Scheduled job to the index. Must be called under index lock. This method must ensure that a duplicate add is not processed into the scheduler. On index recover some adds may be replayed and we don't allow more than one instance of a JobId to exist at any given scheduled time, so filter these out to ensure idempotence.
      Parameters:
      tx - Transaction in which the update is performed.
      command - The new scheduled job command to process.
      location - The location where the add command is stored in the journal.
      Throws:
      IOException - if an error occurs updating the index.
    • process

      protected void process(Transaction tx, KahaRescheduleJobCommand command, Location location) throws IOException
      Reschedules a Job after it has be fired. For jobs that are repeating this method updates the job in the index by adding it to the jobs list for the new execution time. If the job is not a cron type job then this method will reduce the repeat counter if the job has a fixed number of repeats set. The Job will be removed from the jobs list it just executed on. This method must also update the value of the last update location in the JobLocation instance so that the checkpoint worker doesn't drop the log file in which that command lives. This method must ensure that an reschedule command that references a job that doesn't exist does not cause an error since it's possible that on recover the original add might be gone and so the job should not reappear in the scheduler.
      Parameters:
      tx - The TX under which the index is updated.
      command - The reschedule command to process.
      location - The location in the index where the reschedule command was stored.
      Throws:
      IOException - if an error occurs during the reschedule.
    • process

      protected void process(Transaction tx, KahaRemoveScheduledJobsCommand command, Location location) throws IOException
      Removes all scheduled jobs within a given time range. The method can be used to clear the entire scheduler index by specifying a range that encompasses all time [0...Long.MAX_VALUE] or a single execution time can be removed by setting start and end time to the same value.
      Parameters:
      tx - The transaction under which the index is updated.
      command - The remove command to process.
      location - The location of the remove command in the Journal.
      Throws:
      IOException - if an error occurs while updating the scheduler index.
    • removeAll

      protected void removeAll(Transaction tx) throws IOException
      Removes all jobs from the schedulers index. Must be called with the index locked.
      Parameters:
      tx - The transaction under which the index entries for this scheduler are removed.
      Throws:
      IOException - if an error occurs removing the jobs from the scheduler index.
    • removeInRange

      protected void removeInRange(Transaction tx, long start, long finish, Location location) throws IOException
      Removes all scheduled jobs within the target range. This method can be used to remove all the stored jobs by passing a range of [0...Long.MAX_VALUE] or it can be used to remove all jobs at a given scheduled time by passing the same time value for both start and end. If the optional location parameter is set then this method will update the store's remove location tracker with the location value and the Jobs that are being removed. This method must be called with the store index locked for writes.
      Parameters:
      tx - The transaction under which the index is to be updated.
      start - The start time for the remove operation.
      finish - The end time for the remove operation.
      location - (optional) The location of the remove command that triggered this remove.
      Throws:
      IOException - if an error occurs during the remove operation.
    • removeJobAtTime

      protected boolean removeJobAtTime(Transaction tx, String jobId, long executionTime) throws IOException
      Removes a Job from the index using it's Id value and the time it is currently set to be executed. This method will only remove the Job if it is found at the given execution time. This method must be called under index lock.
      Parameters:
      tx - the transaction under which this method is being executed.
      jobId - the target Job Id to remove.
      executionTime - the scheduled time that for the Job Id that is being removed.
      Throws:
      IOException - if an error occurs while removing the Job.
    • getAllScheduledJobs

      protected Iterator<org.apache.activemq.store.kahadb.scheduler.JobLocation> getAllScheduledJobs(Transaction tx) throws IOException
      Walks the Scheduled Job Tree and collects the add location and last update location for all scheduled jobs. This method must be called with the index locked.
      Parameters:
      tx - the transaction under which this operation was invoked.
      Returns:
      a iterator of all referenced Location values for this JobSchedulerImpl
      Throws:
      IOException - if an error occurs walking the scheduler tree.
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • mainLoop

      protected void mainLoop()
    • startDispatching

      public void startDispatching() throws Exception
      Specified by:
      startDispatching in interface JobScheduler
      Throws:
      Exception
    • stopDispatching

      public void stopDispatching() throws Exception
      Specified by:
      stopDispatching in interface JobScheduler
      Throws:
      Exception
    • doStart

      protected void doStart() throws Exception
      Specified by:
      doStart in class ServiceSupport
      Throws:
      Exception
    • doStop

      protected void doStop(ServiceStopper stopper) throws Exception
      Specified by:
      doStop in class ServiceSupport
      Throws:
      Exception
    • write

      public void write(DataOutput out) throws IOException
      Throws:
      IOException