Package org.apache.zookeeper.common
Class FileChangeWatcher
java.lang.Object
org.apache.zookeeper.common.FileChangeWatcher
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 Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionFileChangeWatcher
(Path dirPath, Consumer<WatchEvent<?>> callback) Creates a watcher that watchesdirPath
and invokescallback
on changes. -
Method Summary
Modifier and TypeMethodDescriptiongetState()
Returns the currentFileChangeWatcher.State
.void
start()
Tells the background thread to start.void
stop()
Tells the background thread to stop.
-
Constructor Details
-
FileChangeWatcher
Creates a watcher that watchesdirPath
and invokescallback
on changes.- Parameters:
dirPath
- the directory to watch.callback
- the callback to invoke with events.event.kind()
will return the type of event, andevent.context()
will return the filename relative todirPath
.- Throws:
IOException
- if there is an error creating the WatchService.
-
-
Method Details
-
getState
Returns the currentFileChangeWatcher.State
.- Returns:
- the current state.
-
start
public 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. -
stop
public void stop()Tells the background thread to stop. Does not wait for it to exit.
-