Package org.apache.hadoop.hbase.io
Class FileChangeWatcher
java.lang.Object
org.apache.hadoop.hbase.io.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.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumprivate classInner class that implements the watcher thread logic. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final org.slf4j.Loggerprivate FileChangeWatcher.Stateprivate final FileChangeWatcher.WatcherThread -
Constructor Summary
ConstructorsConstructorDescriptionFileChangeWatcher(Path dirPath, String threadNameSuffix, Consumer<WatchEvent<?>> callback) Creates a watcher that watchesdirPathand invokescallbackon changes. -
Method Summary
Modifier and TypeMethodDescriptionprivate booleancompareAndSetState(FileChangeWatcher.State[] expectedStates, FileChangeWatcher.State update) Atomically sets the state toupdateif and only if the state is currently one ofexpectedStates.private booleancompareAndSetState(FileChangeWatcher.State expected, FileChangeWatcher.State update) Atomically sets the state toupdateif and only if the state is currentlyexpected.getState()Returns the currentFileChangeWatcher.State.(package private) Stringprivate static voidhandleException(Thread thread, Throwable e) private voidsetState(FileChangeWatcher.State newState) Sets the state tonewState.voidstart()Tells the background thread to start.voidstop()Tells the background thread to stop.(package private) voidwaitForState(FileChangeWatcher.State desiredState) Blocks until the current state becomesdesiredState.
-
Field Details
-
LOG
-
watcherThread
-
state
-
-
Constructor Details
-
FileChangeWatcher
public FileChangeWatcher(Path dirPath, String threadNameSuffix, 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, 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.
-
waitForState
Blocks until the current state becomesdesiredState. Currently only used by tests, thus package-private.- Parameters:
desiredState- the desired state.- Throws:
InterruptedException- if the current thread gets interrupted.
-
setState
Sets the state tonewState.- Parameters:
newState- the new state.
-
compareAndSetState
private boolean compareAndSetState(FileChangeWatcher.State expected, FileChangeWatcher.State update) Atomically sets the state toupdateif and only if the state is currentlyexpected.- Parameters:
expected- the expected state.update- the new state.- Returns:
- true if the update succeeds, or false if the current state does not equal
expected.
-
compareAndSetState
private boolean compareAndSetState(FileChangeWatcher.State[] expectedStates, FileChangeWatcher.State update) Atomically sets the state toupdateif and only if the state is currently one ofexpectedStates.- Parameters:
expectedStates- the expected states.update- the new state.- Returns:
- true if the update succeeds, or false if the current state does not equal any of the
expectedStates.
-
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
Tells the background thread to stop. Does not wait for it to exit. -
getWatcherThreadName
-
handleException
-