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
Modifier and TypeClassDescriptionstatic enum
private class
Inner class that implements the watcher thread logic. -
Field Summary
Modifier and TypeFieldDescriptionprivate static final org.slf4j.Logger
private FileChangeWatcher.State
private final FileChangeWatcher.WatcherThread
-
Constructor Summary
ConstructorDescriptionFileChangeWatcher
(Path dirPath, String threadNameSuffix, Consumer<WatchEvent<?>> callback) Creates a watcher that watchesdirPath
and invokescallback
on changes. -
Method Summary
Modifier and TypeMethodDescriptionprivate boolean
compareAndSetState
(FileChangeWatcher.State[] expectedStates, FileChangeWatcher.State update) Atomically sets the state toupdate
if and only if the state is currently one ofexpectedStates
.private boolean
compareAndSetState
(FileChangeWatcher.State expected, FileChangeWatcher.State update) Atomically sets the state toupdate
if and only if the state is currentlyexpected
.getState()
Returns the currentFileChangeWatcher.State
.(package private) String
private static void
handleException
(Thread thread, Throwable e) private void
setState
(FileChangeWatcher.State newState) Sets the state tonewState
.void
start()
Tells the background thread to start.void
stop()
Tells the background thread to stop.(package private) void
waitForState
(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 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.
-
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 toupdate
if 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 toupdate
if 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
-