Package org.apache.zookeeper.common
Class FileChangeWatcher
- java.lang.Object
- 
- org.apache.zookeeper.common.FileChangeWatcher
 
- 
 public final class FileChangeWatcher extends Object Instances of this class can be used to watch a directory for file changes. When a file is added to, deleted from, or is modified in the given directory, the callback provided by the user will be called from a background thread. Some things to keep in mind:- The callback should be thread-safe.
- Changes that happen around the time the thread is started may be missed.
- There is a delay between a file changing and the callback firing.
- The watch is not recursive - changes to subdirectories will not trigger a callback.
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classFileChangeWatcher.State
 - 
Constructor SummaryConstructors Constructor Description FileChangeWatcher(Path dirPath, Consumer<WatchEvent<?>> callback)Creates a watcher that watchesdirPathand invokescallbackon changes.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description FileChangeWatcher.StategetState()Returns the currentFileChangeWatcher.State.voidstart()Tells the background thread to start.voidstop()Tells the background thread to stop.
 
- 
- 
- 
Constructor Detail- 
FileChangeWatcherpublic FileChangeWatcher(Path dirPath, Consumer<WatchEvent<?>> callback) throws IOException Creates a watcher that watchesdirPathand invokescallbackon changes.- Parameters:
- dirPath- the directory to watch.
- callback- the callback to invoke with events.- event.kind()will return the type of event, and- event.context()will return the filename relative to- dirPath.
- Throws:
- IOException- if there is an error creating the WatchService.
 
 
- 
 - 
Method Detail- 
getStatepublic FileChangeWatcher.State getState() Returns the currentFileChangeWatcher.State.- Returns:
- the current state.
 
 - 
startpublic void start() Tells the background thread to start. Does not wait for it to be running. Calling this method more than once has no effect.
 - 
stoppublic void stop() Tells the background thread to stop. Does not wait for it to exit.
 
- 
 
-