Class JobSchedulerImpl

    • Method Detail

      • setName

        public void setName​(String name)
      • addListener

        public void addListener​(JobListener l)
        Description copied from interface: JobScheduler
        Add a Job listener which will receive events related to scheduled jobs.
        Specified by:
        addListener in interface JobScheduler
        Parameters:
        l - The job listener to add.
      • removeListener

        public void removeListener​(JobListener l)
        Description copied from interface: JobScheduler
        remove a JobListener that was previously registered. If the given listener is not in the registry this method has no effect.
        Specified by:
        removeListener in interface JobScheduler
        Parameters:
        l - The listener that should be removed from the listener registry.
      • schedule

        public void schedule​(String jobId,
                             ByteSequence payload,
                             long delay)
                      throws IOException
        Description copied from interface: JobScheduler
        Add a job to be scheduled
        Specified by:
        schedule in interface JobScheduler
        Parameters:
        jobId - a unique identifier for the job
        payload - the message to be sent when the job is scheduled
        delay - the time in milliseconds before the job will be run
        Throws:
        IOException
      • schedule

        public void schedule​(String jobId,
                             ByteSequence payload,
                             String cronEntry)
                      throws Exception
        Description copied from interface: JobScheduler
        Add a job to be scheduled
        Specified by:
        schedule in interface JobScheduler
        Parameters:
        jobId - a unique identifier for the job
        payload - the message to be sent when the job is scheduled
        cronEntry - The cron entry to use to schedule this job.
        Throws:
        Exception - if an error occurs while scheduling the Job.
      • schedule

        public void schedule​(String jobId,
                             ByteSequence payload,
                             String cronEntry,
                             long delay,
                             long period,
                             int repeat)
                      throws IOException
        Description copied from interface: JobScheduler
        Add a job to be scheduled
        Specified by:
        schedule in interface JobScheduler
        Parameters:
        jobId - a unique identifier for the job
        payload - the message to be sent when the job is scheduled
        cronEntry - cron entry
        delay - time in ms to wait before scheduling
        period - the time in milliseconds between successive executions of the Job
        repeat - the number of times to execute the job - less than 0 will be repeated forever
        Throws:
        IOException
      • remove

        public void remove​(long time)
                    throws IOException
        Description copied from interface: JobScheduler
        remove all jobs scheduled to run at this time
        Specified by:
        remove in interface JobScheduler
        Parameters:
        time - The UTC time to use to remove a batch of scheduled Jobs.
        Throws:
        IOException
      • remove

        public void remove​(String jobId)
                    throws IOException
        Description copied from interface: JobScheduler
        remove a job with the matching jobId
        Specified by:
        remove in interface JobScheduler
        Parameters:
        jobId - The unique Job Id to search for and remove from the scheduled set of jobs.
        Throws:
        IOException
      • removeAllJobs

        public void removeAllJobs​(long start,
                                  long finish)
                           throws IOException
        Description copied from interface: JobScheduler
        remove all the Jobs from the scheduler that are due between the start and finish times
        Specified by:
        removeAllJobs in interface JobScheduler
        Parameters:
        start - time in milliseconds
        finish - time in milliseconds
        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
      • mainLoop

        protected void mainLoop()
      • startDispatching

        public void startDispatching()
                              throws Exception
        Description copied from interface: JobScheduler
        Starts dispatch of scheduled Jobs to registered listeners. Any listener added after the start dispatch method can miss jobs so its important to register critical listeners before the start of job dispatching.
        Specified by:
        startDispatching in interface JobScheduler
        Throws:
        Exception