public class PathWatcher extends AbstractStatefulLifecycle implements Runnable
Suitable replacement for the old Scanner implementation.
Allows for configured Excludes and Includes using FileSystem.getPathMatcher(String) syntax.
Reports activity via registered PathWatcher.Listeners
| 限定符和类型 | 类和说明 |
|---|---|
static class |
PathWatcher.Config |
static class |
PathWatcher.DirAction |
static interface |
PathWatcher.EventListListener
EventListListener
Listener that reports accumulated events in one shot
|
static interface |
PathWatcher.Listener
Listener for path change events
|
static class |
PathWatcher.PathMatcherSet |
class |
PathWatcher.PathWatchEvent
PathWatchEvent
Represents a file event.
|
static class |
PathWatcher.PathWatchEventType
PathWatchEventType
Type of an event
|
AbstractStatefulLifecycle.StopException| 构造器和说明 |
|---|
PathWatcher()
Construct new PathWatcher
|
| 限定符和类型 | 方法和说明 |
|---|---|
void |
addListener(EventListener listener)
Add a listener for changes the watcher notices.
|
protected static <T> WatchEvent<T> |
cast(WatchEvent<?> event) |
protected void |
doStart() |
protected void |
doStop() |
Collection<PathWatcher.Config> |
getConfigs() |
Iterator<EventListener> |
getListeners()
Get an iterator over the listeners.
|
long |
getUpdateQuietTimeMillis()
Change the quiet time.
|
void |
handleWatchEvent(Path path,
PathWatcher.PathWatchEvent event)
Add an event reported by the WatchService to list of pending events
that will be sent after their quiet time has expired.
|
protected boolean |
isNotifiable()
Check to see if the watcher is in a state where it should generate
watch events to the listeners.
|
boolean |
isNotifyExistingOnStart() |
protected void |
register(Path path,
PathWatcher.Config config) |
boolean |
removeListener(PathWatcher.Listener listener)
Delete a listener
|
void |
reset()
Remove all current configs and listeners.
|
void |
run()
Forever loop.
|
void |
setNotifyExistingOnStart(boolean notify)
Whether or not to issue notifications for directories and files that
already exist when the watcher starts.
|
void |
setUpdateQuietTime(long duration,
TimeUnit unit)
Set the quiet time.
|
String |
toString() |
void |
watch(Path file)
Request watch on a the given path (either file or dir)
using all Config defaults.
|
void |
watch(PathWatcher.Config config)
Request watch on a path with custom Config
provided.
|
addEventListener, getEventPublisher, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeEventListener, setEventPublisher, shutdown, startupprotected static <T> WatchEvent<T> cast(WatchEvent<?> event)
public Collection<PathWatcher.Config> getConfigs()
public void watch(Path file)
file - the path to watchpublic void watch(PathWatcher.Config config)
config - the configuration to watchpublic void addListener(EventListener listener)
listener - change listenerprotected void doStart()
throws Exception
doStart 在类中 AbstractStatefulLifecycleExceptionprotected void doStop()
throws Exception
doStop 在类中 AbstractStatefulLifecycleExceptionpublic void reset()
protected boolean isNotifiable()
public Iterator<EventListener> getListeners()
public long getUpdateQuietTimeMillis()
protected void register(Path path, PathWatcher.Config config) throws IOException
IOExceptionpublic boolean removeListener(PathWatcher.Listener listener)
listener - the listener to removepublic void run()
Wait for the WatchService to report some filesystem events for the watched paths.
When an event for a path first occurs, it is subjected to a quiet time. Subsequent events that arrive for the same path during this quiet time are accumulated and the timer reset. Only when the quiet time has expired are the accumulated events sent. MODIFY events are handled slightly differently - multiple MODIFY events arriving within a quiet time are coalesced into a single MODIFY event. Both the accumulation of events and coalescing of MODIFY events reduce the number and frequency of event reporting for "noisy" files (ie those that are undergoing rapid change).
run 在接口中 RunnableRunnable.run()public void handleWatchEvent(Path path, PathWatcher.PathWatchEvent event)
path - the path to add to the pending listevent - the pending eventpublic void setNotifyExistingOnStart(boolean notify)
notify - true if existing paths should be notified or notpublic boolean isNotifyExistingOnStart()
public void setUpdateQuietTime(long duration,
TimeUnit unit)
duration - the quiet time durationunit - the quite time unitpublic String toString()
toString 在类中 AbstractStatefulLifecycleCopyright © 2022. All rights reserved.