Class JobSchedulerImpl
- java.lang.Object
-
- org.apache.activemq.util.ServiceSupport
-
- org.apache.activemq.store.kahadb.scheduler.JobSchedulerImpl
-
- All Implemented Interfaces:
Runnable
,JobScheduler
,Service
public class JobSchedulerImpl extends ServiceSupport implements Runnable, JobScheduler
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(JobListener l)
Add a Job listener which will receive events related to scheduled jobs.protected void
doStart()
protected void
doStop(ServiceStopper stopper)
List<Job>
getAllJobs()
Get all the outstanding JobsList<Job>
getAllJobs(long start, long finish)
Get all outstanding jobs due to run between start and finishprotected Iterator<org.apache.activemq.store.kahadb.scheduler.JobLocation>
getAllScheduledJobs(Transaction tx)
Walks the Scheduled Job Tree and collects the add location and last update location for all scheduled jobs.String
getName()
List<Job>
getNextScheduleJobs()
Get all the jobs scheduled to run nextlong
getNextScheduleTime()
Get the next time jobs will be firedprotected void
mainLoop()
protected void
process(Transaction tx, KahaAddScheduledJobCommand command, Location location)
Adds a new Scheduled job to the index.protected void
process(Transaction tx, KahaRemoveScheduledJobsCommand command, Location location)
Removes all scheduled jobs within a given time range.protected void
process(Transaction tx, KahaRescheduleJobCommand command, Location location)
Reschedules a Job after it has be fired.void
remove(long time)
remove all jobs scheduled to run at this timevoid
remove(String jobId)
remove a job with the matching jobIdprotected void
removeAll(Transaction tx)
Removes all jobs from the schedulers index.void
removeAllJobs()
remove all the Jobs from the schedulervoid
removeAllJobs(long start, long finish)
remove all the Jobs from the scheduler that are due between the start and finish timesprotected void
removeInRange(Transaction tx, long start, long finish, Location location)
Removes all scheduled jobs within the target range.protected boolean
removeJobAtTime(Transaction tx, String jobId, long executionTime)
Removes a Job from the index using it's Id value and the time it is currently set to be executed.void
removeListener(JobListener l)
remove a JobListener that was previously registered.void
run()
void
schedule(String jobId, ByteSequence payload, long delay)
Add a job to be scheduledvoid
schedule(String jobId, ByteSequence payload, String cronEntry)
Add a job to be scheduledvoid
schedule(String jobId, ByteSequence payload, String cronEntry, long delay, long period, int repeat)
Add a job to be scheduledvoid
setName(String name)
void
startDispatching()
Starts dispatch of scheduled Jobs to registered listeners.void
stopDispatching()
Stops dispatching of scheduled Jobs to registered listeners.String
toString()
void
write(DataOutput out)
-
Methods inherited from class org.apache.activemq.util.ServiceSupport
addServiceListener, dispose, isStarted, isStopped, isStopping, postStop, preStart, removeServiceListener, start, stop
-
-
-
-
Method Detail
-
setName
public void setName(String name)
-
getName
public String getName()
- Specified by:
getName
in interfaceJobScheduler
- Returns:
- the name of the scheduler
-
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 interfaceJobScheduler
- 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 interfaceJobScheduler
- 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 interfaceJobScheduler
- Parameters:
jobId
- a unique identifier for the jobpayload
- the message to be sent when the job is scheduleddelay
- 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 interfaceJobScheduler
- Parameters:
jobId
- a unique identifier for the jobpayload
- the message to be sent when the job is scheduledcronEntry
- 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 interfaceJobScheduler
- Parameters:
jobId
- a unique identifier for the jobpayload
- the message to be sent when the job is scheduledcronEntry
- cron entrydelay
- time in ms to wait before schedulingperiod
- the time in milliseconds between successive executions of the Jobrepeat
- 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 interfaceJobScheduler
- 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 interfaceJobScheduler
- Parameters:
jobId
- The unique Job Id to search for and remove from the scheduled set of jobs.- Throws:
IOException
-
removeAllJobs
public void removeAllJobs() throws IOException
Description copied from interface:JobScheduler
remove all the Jobs from the scheduler- Specified by:
removeAllJobs
in interfaceJobScheduler
- 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 interfaceJobScheduler
- Parameters:
start
- time in millisecondsfinish
- time in milliseconds- Throws:
IOException
-
getNextScheduleTime
public long getNextScheduleTime() throws IOException
Description copied from interface:JobScheduler
Get the next time jobs will be fired- Specified by:
getNextScheduleTime
in interfaceJobScheduler
- Returns:
- the time in milliseconds
- Throws:
IOException
-
getNextScheduleJobs
public List<Job> getNextScheduleJobs() throws IOException
Description copied from interface:JobScheduler
Get all the jobs scheduled to run next- Specified by:
getNextScheduleJobs
in interfaceJobScheduler
- Returns:
- a list of jobs that will be scheduled next
- Throws:
IOException
-
getAllJobs
public List<Job> getAllJobs() throws IOException
Description copied from interface:JobScheduler
Get all the outstanding Jobs- Specified by:
getAllJobs
in interfaceJobScheduler
- Returns:
- a list of all jobs
- Throws:
IOException
-
getAllJobs
public List<Job> getAllJobs(long start, long finish) throws IOException
Description copied from interface:JobScheduler
Get all outstanding jobs due to run between start and finish- Specified by:
getAllJobs
in interfaceJobScheduler
- Returns:
- a list of jobs
- 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.
-
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 interfaceJobScheduler
- Throws:
Exception
-
stopDispatching
public void stopDispatching() throws Exception
Description copied from interface:JobScheduler
Stops dispatching of scheduled Jobs to registered listeners.- Specified by:
stopDispatching
in interfaceJobScheduler
- Throws:
Exception
-
doStart
protected void doStart() throws Exception
- Specified by:
doStart
in classServiceSupport
- Throws:
Exception
-
doStop
protected void doStop(ServiceStopper stopper) throws Exception
- Specified by:
doStop
in classServiceSupport
- Throws:
Exception
-
write
public void write(DataOutput out) throws IOException
- Throws:
IOException
-
-