com.arsdigita.acs
Class Scheduler

java.lang.Object
  |
  +--com.arsdigita.acs.Scheduler

public class Scheduler
extends Object

Utility class for running scheduled procedures, modelled after the AOLserver ns_schedule_proc interface. Has an interface for daily/weekly procs.

Since procedures are not first-class objects in Java, we have to pass in the procedure to be scheduled wrapped inside an object that implements the java.lang.Runnable interface. We define an abstract class, ScheduledProcedure, that serves as a record for maintaining data about a stored procedure, when it will run next, whether or not it is periodic, etc.

We keep the list of scheduled procedures in a priority queue, so that the iteration over the list to figure out whose turn is next is done when procedures are scheduled rather than on a regular basis. Each task is run in its own thread, so that other scheduled procedures are not delayed due to one procedure's execution time.

See Also:
java.util.Timer, Thread, ScheduledProcedure

Constructor Summary
Scheduler()
          Creates a new Scheduler interface and starts it running.
 
Method Summary
static Scheduler getInstance()
          Utility function to get a static Scheduler instance.
static void main(String[] args)
           
 int schedule(ScheduledProcedure sp, long interval)
          Schedules a procedure to run every interval seconds.
 int scheduleDaily(ScheduledProcedure sp, int hour, int minute)
          Schedules a procedure to run every day at hour:minute.
 int scheduleDailyOnce(ScheduledProcedure sp, int hour, int minute)
          Schedules a procedure to run once at hour:minute.
 int scheduleOnce(ScheduledProcedure sp, long interval)
          Schedules a procedure to run once in interval seconds.
 int scheduleWeekly(ScheduledProcedure sp, int day, int hour, int minute)
          Schedules a procedure to run each week at hour:minute, on day.
 int scheduleWeeklyOnce(ScheduledProcedure sp, int day, int hour, int minute)
          Schedules a procedure to run once at hour:minute, on day.
 void unschedule(int spId)
          Unschedules a scheduled procedure.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Scheduler

public Scheduler()
Creates a new Scheduler interface and starts it running.
Method Detail

getInstance

public static Scheduler getInstance()
Utility function to get a static Scheduler instance.
Returns:
This JVM's Scheduler instance.

schedule

public int schedule(ScheduledProcedure sp,
                    long interval)
Schedules a procedure to run every interval seconds.
Parameters:
sp - Proc to schedule.
interval - Time between execution in seconds.
Returns:
The procedure's ID, for later unscheduling

scheduleOnce

public int scheduleOnce(ScheduledProcedure sp,
                        long interval)
Schedules a procedure to run once in interval seconds.
Parameters:
sp - Proc to schedule.
interval - Time until execution, in seconds.
Returns:
The procedure's ID, for later unscheduling

scheduleDaily

public int scheduleDaily(ScheduledProcedure sp,
                         int hour,
                         int minute)
Schedules a procedure to run every day at hour:minute.
Parameters:
sp - Proc to schedule.
hour - Hour to run proc (in 24-hour time)
minute - Minute to run proc
Returns:
The procedure's ID, for later unscheduling

scheduleDailyOnce

public int scheduleDailyOnce(ScheduledProcedure sp,
                             int hour,
                             int minute)
Schedules a procedure to run once at hour:minute. Waits until tomorrow if that time has already passed today.
Parameters:
sp - Proc to schedule.
hour - Hour to run proc (in 24-hour time)
minute - Minute to run proc
Returns:
The procedure's ID, for later unscheduling

scheduleWeekly

public int scheduleWeekly(ScheduledProcedure sp,
                          int day,
                          int hour,
                          int minute)
Schedules a procedure to run each week at hour:minute, on day.
Parameters:
sp - Proc to schedule.
day - Day of week to run proc. Use the day-of-week constants in Calendar.
hour - Hour to run proc (in 24-hour time)
minute - Minute to run proc
Returns:
The procedure's ID, for later unscheduling
See Also:
Calendar.DAY_OF_WEEK

scheduleWeeklyOnce

public int scheduleWeeklyOnce(ScheduledProcedure sp,
                              int day,
                              int hour,
                              int minute)
Schedules a procedure to run once at hour:minute, on day. Waits for next week if that time has already passed this week.
Parameters:
sp - Proc to schedule.
day - Day of week to run proc. Use the day-of-week constants in Calendar.
hour - Hour to run proc (in 24-hour time)
minute - Minute to run proc
Returns:
The procedure's ID, for later unscheduling
See Also:
Calendar.DAY_OF_WEEK

unschedule

public void unschedule(int spId)
Unschedules a scheduled procedure.
Parameters:
spId - The scheduled proc ID to unschedule.

main

public static void main(String[] args)