com.jarretttaylor.cron
Class Cron

java.lang.Object
  extended by java.lang.Thread
      extended by com.jarretttaylor.cron.Cron
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
XmlCron

public class Cron
extends java.lang.Thread

Manages cron jobs. Format for schedule is as described in the man page from crontab(5) (copied below).

 ------------------------------------------------------------------------------
 The following was taken from the man page for crontab(5):
 
       The time and date fields are:
 
               field          allowed values
               -----          --------------
               minute         0-59
               hour           0-23
               day of month   0-31
               month          1-12
               day of week    0-6 (0 is Sunday)
 
        A field may be an asterisk (*), which always stands for "first-last".
 
        Ranges of numbers are allowed.  Ranges are two numbers separated with a
        hyphen.   The  specified  range is inclusive.  For example, 8-11 for an
        "hours" entry specifies execution at hours 8, 9, 10 and 11.
 
        Lists are allowed.  A list is a set of numbers (or ranges) separated by
        commas.  Examples: "1,2,5,9", "0-4,8-12".
 
        Step  values can be used in conjunction with ranges.  Following a range
        with "/<number>" specifies skips of the number's  value  through  the
        range.  For example, "0-23/2" can be used in the hours field to spec-
        ify command execution every other hour (the alternative in the V7 stan-
       dard  is "0,2,4,6,8,10,12,14,16,18,20,22").  Steps are also permitted
       after an asterisk, so if you want to say "every two hours", just  use
       "*/2".
 
        Note:  The  day of a command's execution can be specified by two fields
        -- day of month, and day of week.  If both fields are  restricted  (ie,
        aren't  *),  the command will be run when either field matches the cur-
        rent time.  For example, "30 4 1,15 * 5" would cause a command to be 
        run at 4:30 am on the 1st and 15th of each month, plus every Friday.
 
        Instead of the first five fields, one of eight special strings may appear:
 
        string          meaning
        ------          -------
        @reboot         Run once, at startup.
        @yearly         Run once a year, "0 0 1 1 *".
        @annually       (same as @yearly)
        @monthly        Run once a month, "0 0 1 * *".
        @weekly         Run once a week, "0 0 * * 0".
        @daily          Run once a day, "0 0 * * *".
        @midnight       (same as @daily)
        @hourly         Run once an hour, "0 * * * *".
 
 EXAMPLES
        run five minutes after midnight, every day
        5 0 * * *
        run at 2:15pm on the first of every month
        15 14 1 * *
        run at 10 pm on weekdays
        0 22 * * 1-5
        run 23 minutes after midn, 2am, 4am ..., everyday
        23 0-23/2 * * *
        run at 5 after 4 every sunday
        5 4 * * 0
 
 
 AUTHOR (crontab documentation)
        Paul Vixie 
 

Author:
Jarrett Taylor

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Cron(java.util.Set<CronJob> inCronJobs, long inCompensation, boolean fork)
          creates a new Cron scheduler.
 
Method Summary
 void disable()
          disables cron (pause)
 void disableCronJob(java.lang.String id)
          disabled the specified cron job
 void enable()
          enables cron
 void enableCronJob(java.lang.String id)
          disabled the specified cron job
 boolean forceRunCronJob(java.lang.String id)
          Runs the specificed job immediately if is not already running (even if it is disabled.
 java.util.Set getCronJobs()
          returns a set of CronJob objects
 boolean isDisabled()
          returns true if cron is not allowed to kick off CronRunner processes
 boolean isProcessing()
          returns true if cron is kicking off CronRunner processes
 void reload(java.util.Set<CronJob> inCronJobs, long inCompensation, boolean terminateRunningJobs)
          Reloads the cron settings.
 void reset()
          sets the killed flag to false and resets all CronJob objects.
 void run()
          This loops while "killed" is false (i.e. terminate() has not been called).
 void setFirstRun(boolean firstRun)
          if firstRun is true, all the enabled startup jobs will be run.
 boolean terminate()
          calls terminate on all CronRunners and CronJobs.
 void terminateCronJob(java.lang.String id)
          requests for the specified cron job to terminate. actual terminate depends on the implementing class
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Cron

public Cron(java.util.Set<CronJob> inCronJobs,
            long inCompensation,
            boolean fork)
creates a new Cron scheduler.

Parameters:
inCronJobs - a Set of CronJob objects
inCompensation - drift compensation time in milliseconds
fork - if true, frees the calling thread to return
Method Detail

run

public void run()
This loops while "killed" is false (i.e. terminate() has not been called). It wakes up right before the closest job is scheduled to run and kicks off a CronRunner thread and then goes back to sleep. The CronRunner thread decides what should run, so this thread never gets bogged down potentially missing the next time it should wake up.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

isProcessing

public boolean isProcessing()
returns true if cron is kicking off CronRunner processes

Returns:
true if cron is kicking off CronRunner processes

isDisabled

public boolean isDisabled()
returns true if cron is not allowed to kick off CronRunner processes

Returns:
true if cron is not allowed to kick off CronRunner processes

enable

public void enable()
enables cron


disable

public void disable()
disables cron (pause)


terminate

public boolean terminate()
calls terminate on all CronRunners and CronJobs.

Returns:
true if the process was running and has been stopped

reset

public void reset()
sets the killed flag to false and resets all CronJob objects.


reload

public void reload(java.util.Set<CronJob> inCronJobs,
                   long inCompensation,
                   boolean terminateRunningJobs)
Reloads the cron settings.

Parameters:
inCronJobs - a Set of CronJob objects
inCompensation - drift compensation time in milliseconds
terminateRunningJobs - if true, it will attempt to terminate any curretnly running jobs

setFirstRun

public void setFirstRun(boolean firstRun)
if firstRun is true, all the enabled startup jobs will be run.

Parameters:
firstRun - true to request running of the startup jobs

forceRunCronJob

public boolean forceRunCronJob(java.lang.String id)
                        throws java.lang.RuntimeException
Runs the specificed job immediately if is not already running (even if it is disabled. Returns true if it starts the job, false if it does not.

Parameters:
id - the id of the cron job to start
Returns:
true if the job was started; false if it is already running
Throws:
java.lang.RuntimeException - if the specified job name is not found

terminateCronJob

public void terminateCronJob(java.lang.String id)
                      throws java.lang.RuntimeException
requests for the specified cron job to terminate. actual terminate depends on the implementing class

Parameters:
id - the id of the cron job to terminate
Throws:
java.lang.RuntimeException - if the specified job name is not found

enableCronJob

public void enableCronJob(java.lang.String id)
                   throws java.lang.RuntimeException
disabled the specified cron job

Parameters:
id - the id of the cron job to enable
Throws:
java.lang.RuntimeException - if the specified job name is not found

disableCronJob

public void disableCronJob(java.lang.String id)
                    throws java.lang.RuntimeException
disabled the specified cron job

Parameters:
id - the id of the cron job to disable
Throws:
java.lang.RuntimeException - if the specified job name is not found

getCronJobs

public java.util.Set getCronJobs()
returns a set of CronJob objects

Returns:
a set of CronJob objects