Class PushFileWatcher

java.lang.Object
com.iizix.server.push.engine.PushFileWatcher
All Implemented Interfaces:
AutoCloseable

public final class PushFileWatcher extends Object implements AutoCloseable
One java.nio.file.WatchService over the files the Push Notification (PN) engine re-reads when they change: the credentials file and, later, the files it names. No polling (ruling S-5, 2026-09-26): the thread blocks in WatchService.take() and the operating system wakes it.

A WatchService watches directories, so a file is watched through its parent: every event naming the file counts, ENTRY_CREATE included, because editors save by writing a new file and renaming it into place, which arrives as a delete and a create rather than a modify. A burst of events - a save is typically two or three - is debounced into one callback: after the first event the thread waits DEBOUNCE_MILLIS of quiet (WatchService.poll(long,TimeUnit) on the event queue, not on the file) before it runs the callbacks, and every further event restarts the wait. An OVERFLOW runs every callback of that directory, since it is not known which file changed.

One thread, a daemon, named at construction; it ends when close() closes the service. The callbacks run on it, one after the other, and a callback that throws is logged SEVERE and does not stop the watcher.

The tree's other WatchService user, com.iizix.server.app.HotDeploy, is a one-shot change detector bound to an application definition - its first change sets a flag and closes the service - so it cannot serve a file that is reloaded again and again, which is why this class exists beside it.

Author:
Christopher Mindus
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
    The quiet window after an event before the callbacks run, in milliseconds.
  • Constructor Summary

    Constructors
    Constructor
    Description
    PushFileWatcher(String threadName)
    Constructor: opens the service, watches nothing yet.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the service; the thread ends and no callback runs afterwards.
    Gets the watcher thread.
    boolean
    Tells whether the watcher is closed.
    int
    Gets the number of watched files.
    void
    Starts the watcher thread, a daemon.
    boolean
    unwatch(Path file)
    Stops watching a file; its directory is released when no other file in it is watched.
    void
    watch(Path file, Runnable onChange)
    Watches a file: its parent directory is registered, and every event naming the file runs the callback.
  • Field Details

    • DEBOUNCE_MILLIS

      public static final long DEBOUNCE_MILLIS
      The quiet window after an event before the callbacks run, in milliseconds.
      See Also:
  • Constructor Details

    • PushFileWatcher

      public PushFileWatcher(String threadName) throws IOException
      Constructor: opens the service, watches nothing yet.
      Parameters:
      threadName - The name of the watcher thread.
      Throws:
      IOException - If the file system offers no watch service.
  • Method Details

    • watch

      public void watch(Path file, Runnable onChange) throws IOException
      Watches a file: its parent directory is registered, and every event naming the file runs the callback.
      Parameters:
      file - The file; it need not exist, its parent directory must.
      onChange - What to run when the file is created, modified or deleted.
      Throws:
      IOException - If the parent directory does not exist or cannot be registered.
    • unwatch

      public boolean unwatch(Path file)
      Stops watching a file; its directory is released when no other file in it is watched.
      Parameters:
      file - The file.
      Returns:
      true if it was watched.
    • size

      public int size()
      Gets the number of watched files.
      Returns:
      The count.
    • start

      public void start()
      Starts the watcher thread, a daemon.
      Throws:
      IllegalStateException - If started already or closed.
    • getThread

      public Thread getThread()
      Gets the watcher thread.
      Returns:
      The thread, null before start().
    • isClosed

      public boolean isClosed()
      Tells whether the watcher is closed.
      Returns:
      true after close().
    • close

      public void close()
      Closes the service; the thread ends and no callback runs afterwards.
      Specified by:
      close in interface AutoCloseable