|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.lang.Thread | +--javatools.cron.Cron
Cron is a class that schedules events according to a known algorithm. Most usually, this algorithm will be the Unix Crontab algorithm which can schedule tasks for certain hours, days and months etc. But you can just as well write your own CrontabEntry class that, say, scheduled according to the phases of the moon, or the movement of the planets, or any other deterministic algorithm you care to name. Or you could also mix and match the supplied unix crontab algorithms with your own custom ones. In summary, cron executes a set of crontabEntries that conform to the CrontabEntryInterface interface. This interface only knows two things: when is the next event going to happen after a certain time, and secondly, what to do when that event happens.
Neither does Cron have any knowledge of what exactly is to be done when the event occurs. It could be starting a thread, or executing a process or doing anything else at all. That is totally up to the CrontabEntry class. The algorithm that Cron uses is as follows... We keep a schedule of "soon to occur" events. Soon to occur is defined as the next event to occur for each timetable entry plus all the events that will occur between now and now plus the futureScheduleInterval. FutureScheduleInterval is by default 5 minutes.
We need to schedule at least one event for each timetable entry to make sure that we will awaken at the correct time. But scheduling only one event for each timetable may not be enough for times of heavy system load. We could schedule all events up until the present moment in time but we choose to schedule a bit into the future too to be ahead of the game. The optimum amount for this value probably needs some experimentation and probably depends on the system load. But I anticipate that the default futureScheduleInterval should be fine for 99.99% of cases.
We store some extra information about each CrontabEntry in the timetable in the private class CronEntry. In particular we need to remember for each CrontabEntry up until what point in time we have scheduled (so that we don't schedule the same event twice), and also how many events we have scheduled for this entry (so that we don't schedule any more than one event for each entry that is further in the future than the futureScheduleInterval).
Your application may consider the Cron process to be of higher priority in scheduling. If so, feel free to increase the priority of the Cron thread using the base class Thread methods.
Nested Class Summary | |
(package private) class |
Cron.CronConfig
Why do we make a separate class out of these two variables? It allows us to replace both atomically without resorting to using synchronized. |
(package private) class |
Cron.CronEntry
A private class that we use to keep track of all the timetable entries we have. |
class |
Cron.CronEvent
When we wish to schedule a particular event to happen, we create an object of this type and add it to the schedule. |
Field Summary | |
(package private) Cron.CronConfig |
currentConfig
|
(package private) long |
futureScheduleInterval
How far in the future will we schedule events for? |
(package private) boolean |
keepGoing
Whether Cron has been asked to shutdown or not. |
static int |
SMALLEST_INTERVAL
The smallest interval of time that we contemplate in milliseconds. |
Fields inherited from class java.lang.Thread |
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
Constructor Summary | |
Cron()
Default constructor. |
|
Cron(java.util.Collection crontabEntries)
|
Method Summary | |
long |
getFutureScheduleInterval()
Discover the Future Scheduling Interval |
void |
logTime(long time)
A debugging function. |
protected java.util.List |
makeCronEntries(java.util.Collection crontabEntries)
|
void |
run()
Run the Cron process. |
long |
runEvents(Cron.CronConfig conf,
long now)
Run all the events that occur before or equal to now. |
void |
scheduleEvent(Cron.CronConfig conf,
long time,
Cron.CronEntry entry)
Schedule an event to occur at a certain time for a certain crontab entry. |
void |
scheduleEvents(Cron.CronConfig conf,
long now)
Discover all the "soon to occur" events. |
void |
setCrontab(java.util.Collection crontabEntries)
Reset cron's crontab schedule. |
void |
setFutureScheduleInterval(long time)
Set the Future Scheduling Interval |
void |
shutdown()
Shutdown the cron thread. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.Thread |
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, yield |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final int SMALLEST_INTERVAL
Cron.CronConfig currentConfig
long futureScheduleInterval
boolean keepGoing
Constructor Detail |
public Cron(java.util.Collection crontabEntries)
crontabEntries
- a collection ofobjects.
public Cron()
Method Detail |
public void setCrontab(java.util.Collection crontabEntries)
crontabEntries
- a collection of CrontabEntryInterface objects.public void setFutureScheduleInterval(long time)
time
- The new futureScheduleInterval valuepublic long getFutureScheduleInterval()
public java.lang.String toString()
toString
in class java.lang.Thread
public void shutdown()
shutdown
in interface Server
public void run()
run
in interface java.lang.Runnable
run
in class java.lang.Thread
public long runEvents(Cron.CronConfig conf, long now)
conf
- Description of Parameternow
- Description of Parameter
public void logTime(long time)
time
- Description of Parameterpublic void scheduleEvent(Cron.CronConfig conf, long time, Cron.CronEntry entry)
time
- The time the event should occurentry
- The crontab entry event that should occur.conf
- Description of Parameterpublic void scheduleEvents(Cron.CronConfig conf, long now)
conf
- Description of Parameternow
- Description of Parameterprotected java.util.List makeCronEntries(java.util.Collection crontabEntries)
crontabEntries
- a collection of CrontabEntryInterface objects.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |