The UNIX operating system uses the cron daemon to run scripts at specific times and dates. Java Development Kit (JDK) 1.3 java. util. Timer class allows developers to set to execute some tasks every N milliseconds, but does not have a cron-like structure to specify a specific time in a day or week.
This gap is now blocked by the JDring package. It is a Java schedule reminder program similar to cron written by Olivier Dedieu. The JDring.zip file contains the following parts:
Source code: This is not required, you can ignore it.
Javadoc document: put this in your docs directory.
Jar file: This file contains compiled classes. Put it in your classpath.
There are two steps to use JDring. The first step is to create an AlarmListener, which is an interface containing a method:
Void handleAlarm (AlarmEntry entry );
The AlarmEntry parameter provides the details of the Schedule reminder settings at what time. The following is a simple example of using AlarmListener:
Import com. jalios. jdring. AlarmEntry;
Import com. jalios. jdring. AlarmListener;
Public class Buzzing implements AlarmListener {
Private String buzz;
Public Buzzing (String buzz ){
This. buzz = buzz;
}
Public void handleAlarm (AlarmEntry entry ){
System. err. println ("dropped ...... ");
System. err. println (buzz );
}
}
The second step of using JDring is to notify a central manager of when AlarmListener should ring. This manager is an instance of AlarmManager and has an empty constructor. Calling schedule reminders at specific times is similar to cron, as shown below:
AlarmManager. addAlarm (minute, hour, day of month, month, day of week, year,
AlarmListener)
The following sample code demonstrates a calendar reminder that is set to start logging in 20th minutes every hour:
Import com. jalios. jdring. AlarmManager;
Import com. jalios. jdring. PastDateException;
Public class SetAlarm {
Static public void main (String [] args ){
AlarmManager mgr = new AlarmManager ();
Mgr. addAlarm (20,-1,-1,-1,-1,-1, new Buzzing ());
}
}
This example demonstrates how to set a reminder at five o'clock P.M. every Friday:
Manager. addAlarm (00, 17,-1,-1, Calendar. FRIDAY,-1, new Buzzing ());
// Java. util. Calendar
JDring can also be used to remember an anniversary, as shown below:
Manager. addAlarm (00, 12, 20, Calendar. MARCH,-1,-1,
New Buzzing ("remember tomorrow is the anniversary! "));