Presents your JAVA E-NEWSLETTER for June 6, 2002 SET CRON-LIKE ALARM SCHEDULING WITH JDRING The UNIX operating system uses the cron daemon to run scripts at certain times on designated days. Java Development Kit (JDK)1.3's java.util.Timer class allows developers to set up tasks to fire every N milliseconds, but there's no cron-like structure to specify a particular time of the day or week. That gap is currently filled by the JDring package, a Java cron-like alarm scheduler by Olivier Dedieu. Here are the components within the JDring zip file: * Source code: This isn't critical to keep and can be discarded. * Javadoc documents: Put these in your docs directory. * Jar file: This file contains the essential classes that need to be placed in your classpath. There are two parts to using JDring. The first requires that you create an AlarmListener, which is an interface with one method to fulfill: void handleAlarm(AlarmEntry entry); The AlarmEntry argument provides details about the time for which the alarm is set. Here's a simple code example of the 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("BUUUZZZZZZZZ"); System.err.println(buzz); } } The second part to using JDring involves telling a central manager when the AlarmListener should go off. The manager is an instance of AlarmManager, and it has an empty constructor. Invoking the alarm for a particular time is similar to cron, such as: AlarmManager.addAlarm(minute, hour, day of month, month, day of week, year, AlarmListener) The following sample code shows the alarm set to go off at 20 minutes past 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 the alarm set for 5 P.M. every Friday: manager.addAlarm(00, 17, -1, -1,Calendar.FRIDAY, -1, newBuzzing()); // java.util.Calendar JDring can also be used to remember an anniversary,such as: manager.addAlarm(00, 12, 20, Calendar.MARCH,-1, -1,new Buzzing("Remember the anniversary tomorrow!") ); ----------------------------------------