Class PushFileWatcher
- All Implemented Interfaces:
AutoCloseable
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
FieldsModifier and TypeFieldDescriptionstatic final longThe quiet window after an event before the callbacks run, in milliseconds.Constructor Summary
ConstructorsConstructorDescriptionPushFileWatcher(String threadName) Constructor: opens the service, watches nothing yet.Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the service; the thread ends and no callback runs afterwards.Gets the watcher thread.booleanisClosed()Tells whether the watcher is closed.intsize()Gets the number of watched files.voidstart()Starts the watcher thread, a daemon.booleanStops watching a file; its directory is released when no other file in it is watched.voidWatches 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_MILLISThe quiet window after an event before the callbacks run, in milliseconds.- See Also:
Constructor Details
PushFileWatcher
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
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
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
isClosed
close
public void close()Closes the service; the thread ends and no callback runs afterwards.- Specified by:
closein interfaceAutoCloseable