@InterfaceAudience.Private public final class FileChangeWatcher extends Object
Modifier and Type | Class and Description |
---|---|
static class |
FileChangeWatcher.State |
private class |
FileChangeWatcher.WatcherThread
Inner class that implements the watcher thread logic.
|
Modifier and Type | Field and Description |
---|---|
private static org.slf4j.Logger |
LOG |
private FileChangeWatcher.State |
state |
private FileChangeWatcher.WatcherThread |
watcherThread |
Constructor and Description |
---|
FileChangeWatcher(Path dirPath,
String threadNameSuffix,
Consumer<WatchEvent<?>> callback)
Creates a watcher that watches
dirPath and invokes callback on
changes. |
Modifier and Type | Method and Description |
---|---|
private boolean |
compareAndSetState(FileChangeWatcher.State[] expectedStates,
FileChangeWatcher.State update)
Atomically sets the state to
update if and only if the state is currently one of
expectedStates . |
private boolean |
compareAndSetState(FileChangeWatcher.State expected,
FileChangeWatcher.State update)
Atomically sets the state to
update if and only if the state is currently
expected . |
FileChangeWatcher.State |
getState()
Returns the current
FileChangeWatcher.State . |
(package private) String |
getWatcherThreadName() |
private static void |
handleException(Thread thread,
Throwable e) |
private void |
setState(FileChangeWatcher.State newState)
Sets the state to
newState . |
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 becomes
desiredState . |
private static final org.slf4j.Logger LOG
private final FileChangeWatcher.WatcherThread watcherThread
private FileChangeWatcher.State state
public FileChangeWatcher(Path dirPath, String threadNameSuffix, Consumer<WatchEvent<?>> callback) throws IOException
dirPath
and invokes callback
on
changes.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
.IOException
- if there is an error creating the WatchService.public FileChangeWatcher.State getState()
FileChangeWatcher.State
.void waitForState(FileChangeWatcher.State desiredState) throws InterruptedException
desiredState
. Currently only used by tests,
thus package-private.desiredState
- the desired state.InterruptedException
- if the current thread gets interrupted.private void setState(FileChangeWatcher.State newState)
newState
.newState
- the new state.private boolean compareAndSetState(FileChangeWatcher.State expected, FileChangeWatcher.State update)
update
if and only if the state is currently
expected
.expected
- the expected state.update
- the new state.expected
.private boolean compareAndSetState(FileChangeWatcher.State[] expectedStates, FileChangeWatcher.State update)
update
if and only if the state is currently one of
expectedStates
.expectedStates
- the expected states.update
- the new state.expectedStates
.public void start()
public void stop()
String getWatcherThreadName()
private static void handleException(Thread thread, Throwable e)
Copyright © 2007–2020 The Apache Software Foundation. All rights reserved.