|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.jalios.jdring.AlarmManager
public class AlarmManager
This class implements an alarm manager similar to Unix cron
and at
daemons. It is intended to fire events
when alarms' date and time match the current ones. Alarms are
added dynamically and can be one-shot or repetitive
(i.e. rescheduled when matched). Time unit is seconds. Alarms
scheduled less than one second to the current time are rejected (a
PastDateException
is thrown).
The alarm scheduler has been designed to manage a large quantity of alarms (it uses a priority queue to optimize alarm dates selection) and to reduce the use of the CPU time (the AlarmManager's thread is started only when there are alarms to be managed and it sleeps until the next alarm date).
Note : because of clocks' skews some alarm dates may be erroneous, particularly if the next alarm date is scheduled for a remote time (e.g. more than a few days). In order to avoid that problem, well-connected machines can use the Network Time Protocol (NTP) to synchronize their clock.
Example of use:
// Creates a new AlarmManager AlarmManager mgr = new AlarmManager(); // Date alarm (non repetitive) mgr.addAlarm(new Date(System.currentTimeMillis() + 300000), new AlarmListener() { public void handleAlarm(AlarmEntry entry) { System.out.println("5 minutes later"); } }); Calendar cal = Calendar.getInstance(); cal.add(Calendar.WEEK_OF_YEAR, 1); mgr.addAlarm(cal.getTime(), new AlarmListener() { public void handleAlarm(AlarmEntry entry) { System.out.println("One week later"); } }); // Alarm with a delay (in minute) relative to the current time. mgr.addAlarm(1, true, new AlarmListener() { public void handleAlarm(AlarmEntry entry) { System.out.println("1 more minute ! (" + new Date() + ")"); } }); // Cron-like alarm (minute, hour, day of month, month, day of week, year) // Repetitive when the year is not specified. mgr.addAlarm(-1, -1, -1, -1, -1, -1, new AlarmListener() { public void handleAlarm(AlarmEntry entry) { System.out.println("Every minute (" + new Date() + ")"); } }); mgr.addAlarm(5, -1, -1, -1, -1, -1, new AlarmListener() { public void handleAlarm(AlarmEntry entry) { System.out.println("Every hour at 5' (" + new Date() + ")"); } }); mgr.addAlarm(00, 12, -1, -1, -1, -1, new AlarmListener() { public void handleAlarm(AlarmEntry entry) { System.out.println("Lunch time (" + new Date() + ")"); } }); mgr.addAlarm(07, 14, 1, Calendar.JANUARY, -1, -1, new AlarmListener() { public void handleAlarm(AlarmEntry entry) { System.out.println("Happy birthday Lucas !"); } }); mgr.addAlarm(30, 9, 1, -1, -1, -1, new AlarmListener() { public void handleAlarm(AlarmEntry entry) { System.out.println("On the first of every month at 9:30"); } }); mgr.addAlarm(00, 18, -1, -1, Calendar.FRIDAY, -1, new AlarmListener() { public void handleAlarm(AlarmEntry entry) { System.out.println("On every Friday at 18:00"); } }); mgr.addAlarm(00, 13, 1, Calendar.AUGUST, -1, 2001, new AlarmListener() { public void handleAlarm(AlarmEntry entry) { System.out.println("2 years that this class was programmed !"); } });
Field Summary | |
---|---|
protected boolean |
isDaemon
|
protected SortedSet<AlarmEntry> |
queue
|
static String |
REVISION
|
protected String |
threadName
|
protected AlarmWaiter |
waiter
|
Constructor Summary | |
---|---|
AlarmManager()
Creates a new AlarmManager. |
|
AlarmManager(boolean isDaemon,
String threadName)
Creates a new AlarmManager. |
Method Summary | |
---|---|
void |
addAlarm(AlarmEntry entry)
Adds an alarm for a specified AlarmEntry |
AlarmEntry |
addAlarm(Date date,
AlarmListener listener)
Adds an alarm for a specified date. |
AlarmEntry |
addAlarm(int delay,
boolean isRepetitive,
AlarmListener listener)
Adds an alarm for a specified delay. |
AlarmEntry |
addAlarm(int minute,
int hour,
int dayOfMonth,
int month,
int dayOfWeek,
int year,
AlarmListener listener)
Adds an alarm for a specified date. |
boolean |
containsAlarm(AlarmEntry alarmEntry)
Tests whether the supplied AlarmEntry is in the manager. |
void |
executeNextAlarmNow()
Do not wait for the next alarm and executes it right now. |
void |
executeNextAlarmNow(boolean executeInCurrentThread)
Do not wait for the next alarm and executes it right now. |
protected void |
finalize()
Stops the waiter thread before ending. |
List<AlarmEntry> |
getAllAlarms()
Returns a copy of all alarms in the manager. |
AlarmEntry |
getNextAlarm()
Returns the next alarm present in queue but don't remove it from queue |
protected void |
notifyListeners()
This is method is called when an alarm date is reached. |
boolean |
removeAlarm(AlarmEntry entry)
Removes the specified AlarmEntry. |
void |
removeAllAlarms()
Removes all the alarms. |
void |
triggerAllAlarms()
Trigger all the alarms now |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String REVISION
protected AlarmWaiter waiter
protected SortedSet<AlarmEntry> queue
protected boolean isDaemon
protected String threadName
Constructor Detail |
---|
public AlarmManager(boolean isDaemon, String threadName)
isDaemon
- true if the waiter thread should run as a daemon.threadName
- the name of the waiter threadpublic AlarmManager()
Method Detail |
---|
public AlarmEntry addAlarm(Date date, AlarmListener listener) throws PastDateException
date
- the alarm date to be added.listener
- the alarm listener.
PastDateException
- if the alarm date is in the past
or less than 1 second closed to the current date).public AlarmEntry addAlarm(int delay, boolean isRepetitive, AlarmListener listener) throws PastDateException
delay
- the alarm delay in minute (relative to now).isRepetitive
- true
if the alarm must be
reactivated, false
otherwise.listener
- the alarm listener.
PastDateException
- if the alarm date is in the past
(or less than 1 second closed to the current date).public AlarmEntry addAlarm(int minute, int hour, int dayOfMonth, int month, int dayOfWeek, int year, AlarmListener listener) throws PastDateException
minute
- minute of the alarm. Allowed values 0-59.hour
- hour of the alarm. Allowed values 0-23.dayOfMonth
- day of month of the alarm (-1 if every
day). This attribute is exclusive with
dayOfWeek
. Allowed values 1-7 (1 = Sunday, 2 =
Monday, ...). java.util.Calendar
constants can be used.month
- month of the alarm (-1 if every month). Allowed values
0-11 (0 = January, 1 = February, ...). java.util.Calendar
constants can be used.dayOfWeek
- day of week of the alarm (-1 if every
day). This attribute is exclusive with
dayOfMonth
. Allowed values 1-31.year
- year of the alarm. When this field is not set
(i.e. -1) the alarm is repetitive (i.e. it is rescheduled when
reached).listener
- the alarm listener.
PastDateException
- if the alarm date is in the past
(or less than 1 second closed to the current date).public void addAlarm(AlarmEntry entry) throws PastDateException
entry
- the AlarmEntry.
PastDateException
- if the alarm date is in the past
(or less than one second too closed to the current date).public boolean removeAlarm(AlarmEntry entry)
entry
- the AlarmEntry that needs to be removed.
true
if there was an alarm for this date,
false
otherwise.public void triggerAllAlarms()
The alarm will be executed asynchronously in the dedicated AlarmWaiter thread as usual.
public void removeAllAlarms()
public boolean containsAlarm(AlarmEntry alarmEntry)
alarmEntry
- the alarmEntry
public List<AlarmEntry> getAllAlarms()
public AlarmEntry getNextAlarm()
AlarmEntry
or null if queue is emptyprotected void notifyListeners()
public void executeNextAlarmNow()
public void executeNextAlarmNow(boolean executeInCurrentThread)
executeInCurrentThread
- if set to true, the alarm will be executed in the current thread, if set to false, the alarm will be executed asynchronously in the dedicated AlarmWaiter thread as usual.executeNextAlarmNow()
,
triggerAllAlarms()
protected void finalize()
finalize
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |