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 file for changes. When a file's modification time
changes, the callback provided by the user will be called from a background thread. Modification
are detected by checking the file's attributes every polling interval. 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.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
(package private) static enum
private class
Inner class that implements the watcher thread logic. -
Field Summary
Modifier and TypeFieldDescriptionprivate final Path
private FileTime
private final Object
private static final org.slf4j.Logger
private final Duration
private FileChangeWatcher.State
private final FileChangeWatcher.WatcherThread
-
Constructor Summary
ConstructorDescriptionFileChangeWatcher
(Path filePath, String threadNameSuffix, Duration pollInterval, FileChangeWatcher.FileChangeWatcherCallback callback) Creates a watcher that watchesfilePath
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
.private FileChangeWatcher.State
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
-
lastModifiedTime
-
lastModifiedTimeLock
-
filePath
-
pollInterval
-
-
Constructor Details
-
FileChangeWatcher
public FileChangeWatcher(Path filePath, String threadNameSuffix, Duration pollInterval, FileChangeWatcher.FileChangeWatcherCallback callback) throws IOException Creates a watcher that watchesfilePath
and invokescallback
on changes.- Parameters:
filePath
- the file 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
-