Recipe 6.14 Program: Reminder Service

The ReminderService program provides a simple reminder service. The load( ) method reads a plain text file containing a list of appointments like the ones shown here, using a SimpleDateFormat :

2004 03  06  22  00 Get some sleep! 2004 11  26  01  27 Finish this program 2004 11  25  01  29 Document this program

The program is based around java.util.Timer , which runs a threaded timer (threads are discussed in Chapter 24) and starts each TimerTask at the scheduled time. In my version, the TimerTask simply prints the message and displays it in a Swing pop up. Example 6-4 shows the full program.

Example 6-4.
import*; import java.text.*; import java.util.*; import javax.swing.JOptionPane; /** Read a file of reminders, run each when due using java.util.Timer. */ public class ReminderService {     /** The Timer object */     Timer timer = new Timer( );     class Item extends TimerTask {         String message;         Item(String m) {             message = m;         }         public void run( ) {             message(message);         }     }     public static void main(String[] argv) throws IOException {         new ReminderService( ).load( );     }     protected void load( ) throws IOException {         BufferedReader is = new BufferedReader(             new FileReader("ReminderService.txt"));         SimpleDateFormat formatter =             new SimpleDateFormat ("yyyy MM dd hh mm");         String aLine;         while ((aLine = is.readLine( )) != null) {             ParsePosition pp = new ParsePosition(0);             Date date = formatter.parse(aLine, pp);             if (date == null) {                 message("Invalid date in " + aLine);                 continue;             }             String mesg = aLine.substring(pp.getIndex( ));             timer.schedule(new Item(mesg), date);         }     }     /** Display a message on the console and in the GUI.      * Used both by Item tasks and by mainline parser.      */     void message(String message) {         System.out.println("\007" + message);         JOptionPane.showMessageDialog(null,             message,              "Timer Alert",                // titlebar             JOptionPane.INFORMATION_MESSAGE);    // icon     } }

I create a nested class Item to store one notification, scheduling it at its due date and time whilst constructing an Item object to display the message when it's due. The load( ) method reads the file containing the data and converts it, using the date parsing from Recipe 6.5. When invoked, the run( ) method in each Item object displays the reminder, as shown in Figure 6-2, both on the standard output (for debugging) and in a dialog window using the Swing JOptionPane class (see Recipe 14.7). The message( ) method consolidates both displays, allowing you to add a control to use only standard output or only the dialog (this is left as an exercise for the reader).

Figure 6-2. ReminderService in action

See Also

You could implement the functionality of this reminder program without using the Timer class; see ReminderServiceOld in the online source.

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: