java notify

From Noah.org
Revision as of 20:25, 17 May 2010 by Root (talk | contribs) (Created page with 'Category:Engineering This is used to create a file which can be used as an event notification system to send events to other processes. The other processes may use the Linux…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search


This is used to create a file which can be used as an event notification system to send events to other processes. The other processes may use the Linux inotify API or they may simply poll for the files.

import java.io.*;

public class NotifyEvent
{
     /* This sets the default pattern used to create a filename.
      * It uses String.format formatting where format is passed
      * the current system time as an epoch integer.
      * This attribute is used by sendEvent().
      */
    public static String filename_format = "/var/run/event/event-%10d";

    /** This creates a file intended to notify others of some event.
     * To receive these event a process may use the Linux inotify API or 
     * poll the event directory. Event listeners should delete the file
     * when they have received the event.
     *
     * The filename is set by the class attribute, filename_format.
     *
     * If there is an error then an error message will be printed to stderr.
     *
     * @return String - the filename of the file created or 
     *                - null if there was an error.
     */
    public static String sendEvent ()
    {
        int epoch = (int) (System.currentTimeMillis() / 1000.0);
        String event_filename = String.format(NotifyEvent.filename_format, epoch);
        File event_file = new File(event_filename);
        try {
            // Java claims that this method is atomic.
            if (event_file.createNewFile()) {
                return event_filename;
            }
            else {
                StringBuilder errmsg = new StringBuilder();
                errmsg.append("ERROR: cannot create the following file because it already exists:\n\t");
                errmsg.append(event_filename);
                System.err.println(errmsg);
            }
        }
        catch (IOException e) {
            StringBuilder errmsg = new StringBuilder();
            errmsg.append("ERROR: caught an IOException while calling File.createNewFile().\n");
            errmsg.append("\tThis probably happened because the parent directory does not exist or\n");
            errmsg.append("\tbecause this process does not have permission to create the following file:\n\t");
            errmsg.append(event_filename);
            System.err.println(errmsg);
        }
        return null;
    }

    public static void main(String args[])
    {
        String event_filename = sendEvent();
        if (event_filename == null)
            System.exit(1);
        StringBuilder okmsg = new StringBuilder();
        okmsg.append("OK: The following event notification file was created:\n\t");
        okmsg.append(event_filename);
        System.out.println(okmsg);
        System.exit(0);
    }
}