Class EFileWatcher

  • All Implemented Interfaces:
    EObject, EPublisher

    public final class EFileWatcher
    extends java.lang.Object
    implements EPublisher
    This class provides an eBus file watcher service, allowing asynchronous notification when a file or directory is created, modified or deleted. This service only supports JVM-local subscribers. This publisher advertises the EFileNotification with the file name as subject. The subject pattern is ".+" which supports any file name.

    The file watcher service starts its timer task with the first subscriber and stops the timer task when there are no more subscribers. This means the timer is running only when there are watched files.

    Author:
    Charles Rapp
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static long DEFAULT_WATCH_RATE
      The default watch rate is one minute.
      static long MAX_WATCH_RATE
      The maximum watch rate is one hour.
      static long MIN_WATCH_RATE
      The minimum watch rate is one half a second.
      static java.lang.String TIMER_NAME
      The thread name is "EFileWatcher".
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean isFileWatcherRunning​(java.io.File file)
      Returns true if the file watcher service is running for the given subject and false otherwise.
      static EMessageKey key​(java.io.File file)
      Returns the EFileWatcher message key for the given file or directory.
      java.lang.String name()
      Returns absolute file name as the eBus object name.
      void publishStatus​(EFeedState fs, IEPublishFeed f)
      Starts or stops a file watcher entry depending on upFlag.
      void shutdown()
      Shuts down the file watcher thread by stopping the watch timer task, retracting the notification advertisement, and clearing the file watchers collection.
      static void startFileWatcher​(java.io.File file)
      Creates the eBus file watcher service using the default watch rate.
      static EMessageKey startFileWatcher​(java.io.File file, long rate)
      Creates an eBus file watcher service for the specified file or directory and checking at a given watch rate.
      void startup()
      Advertises the file watcher service to the local JVM only.
      static void stopFileWatcher​(java.io.File file)
      Stops the watcher thread.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_WATCH_RATE

        public static final long DEFAULT_WATCH_RATE
        The default watch rate is one minute. Value is in milliseconds.
        See Also:
        Constant Field Values
      • MIN_WATCH_RATE

        public static final long MIN_WATCH_RATE
        The minimum watch rate is one half a second. Value is in milliseconds.
        See Also:
        Constant Field Values
      • MAX_WATCH_RATE

        public static final long MAX_WATCH_RATE
        The maximum watch rate is one hour. Value is in milliseconds.
        See Also:
        Constant Field Values
      • TIMER_NAME

        public static final java.lang.String TIMER_NAME
        The thread name is "EFileWatcher".
        See Also:
        Constant Field Values
    • Method Detail

      • name

        public java.lang.String name()
        Returns absolute file name as the eBus object name.
        Specified by:
        name in interface EObject
        Returns:
        eBus object name.
      • startup

        public void startup()
        Advertises the file watcher service to the local JVM only. The timer task is started with the first subscriber and stopped when there are no more subscribers.
        Specified by:
        startup in interface EObject
        See Also:
        EObject.shutdown()
      • shutdown

        public void shutdown()
        Shuts down the file watcher thread by stopping the watch timer task, retracting the notification advertisement, and clearing the file watchers collection.
        Specified by:
        shutdown in interface EObject
        See Also:
        EObject.startup()
      • publishStatus

        public void publishStatus​(EFeedState fs,
                                  IEPublishFeed f)
        Starts or stops a file watcher entry depending on upFlag. The notification subject is the file name.
        Specified by:
        publishStatus in interface EPublisher
        Parameters:
        fs - the f state is either up or down.
        f - the f state applies to this publish f.
      • isFileWatcherRunning

        public static boolean isFileWatcherRunning​(java.io.File file)
        Returns true if the file watcher service is running for the given subject and false otherwise.
        Parameters:
        file - check if this file or directory has a watcher.
        Returns:
        true if the file watcher service is running.
      • startFileWatcher

        public static void startFileWatcher​(java.io.File file)
        Creates the eBus file watcher service using the default watch rate.
        Parameters:
        file - watch this given file or directory.
        Throws:
        java.lang.IllegalStateException - if the file watcher service is already running.
      • startFileWatcher

        public static EMessageKey startFileWatcher​(java.io.File file,
                                                   long rate)
        Creates an eBus file watcher service for the specified file or directory and checking at a given watch rate. Returns the message key for this watcher. Use the returned key to build a subscription to the newly created file watcher.
        Parameters:
        file - watch this given file or directory.
        rate - check for file updates at this millisecond rate.
        Returns:
        the file watcher message key used to subscribe to this file watcher.
        Throws:
        java.lang.IllegalArgumentException - If file is null or if rate is < MIN_WATCH_RATE or > MAX_WATCH_RATE.
        java.lang.IllegalStateException - if the file watcher service is already running but at a different watch rate.
      • key

        public static EMessageKey key​(java.io.File file)
        Returns the EFileWatcher message key for the given file or directory.
        Parameters:
        file - generate the message key for this file.
        Returns:
        file watcher message key for the given file.