Package org.eclipse.jetty.util
Class PathWatcher
java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.PathWatcher
Watch a Path (and sub directories) for Path changes.
Suitable replacement for the old Scanner implementation.
Allows for configured Excludes and Includes using FileSystem.getPathMatcher(String) syntax.
Reports activity via registered PathWatcher.Listeners
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic enumstatic interfaceListener that reports accumulated events in one shotstatic interfaceListener for path change eventsstatic classclassRepresents a file event.static enumType of an eventNested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopException -
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(EventListener listener) Add a listener for changes the watcher notices.protected static <T> WatchEvent<T>cast(WatchEvent<?> event) protected voiddoStart()Method to override to start the lifecycleprotected voiddoStop()Method to override to stop the lifecycleGet an iterator over the listeners.voidhandleWatchEvent(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 booleanCheck to see if the watcher is in a state where it should generate watch events to the listeners.booleanprotected voidregister(Path path, PathWatcher.Config config) booleanremoveListener(PathWatcher.Listener listener) Delete a listenervoidreset()Remove all current configs and listeners.voidrun()Forever loop.voidsetNotifyExistingOnStart(boolean notify) Whether or not to issue notifications for directories and files that already exist when the watcher starts.voidsetUpdateQuietTime(long duration, TimeUnit unit) Set the quiet time.toString()voidRequest watch on a the given path (either file or dir) using all Config defaults.voidwatch(PathWatcher.Config config) Request watch on a path with custom Config provided.Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addEventListener, getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeEventListener, setEventListeners, start, stop
-
Constructor Details
-
PathWatcher
public PathWatcher()Construct new PathWatcher
-
-
Method Details
-
cast
-
getConfigs
-
watch
Request watch on a the given path (either file or dir) using all Config defaults. In the case of a dir, the default is not to recurse into subdirs for watching.- Parameters:
file- the path to watch
-
watch
Request watch on a path with custom Config provided.- Parameters:
config- the configuration to watch
-
addListener
Add a listener for changes the watcher notices.- Parameters:
listener- change listener
-
doStart
Description copied from class:AbstractLifeCycleMethod to override to start the lifecycle- Overrides:
doStartin classAbstractLifeCycle- Throws:
AbstractLifeCycle.StopException- If thrown, the lifecycle will immediately be stopped.Exception- If there was a problem starting. Will cause a transition to FAILED state
-
doStop
Description copied from class:AbstractLifeCycleMethod to override to stop the lifecycle- Overrides:
doStopin classAbstractLifeCycle- Throws:
Exception- If there was a problem stopping. Will cause a transition to FAILED state
-
reset
public void reset()Remove all current configs and listeners. -
isNotifiable
protected boolean isNotifiable()Check to see if the watcher is in a state where it should generate watch events to the listeners. Used to determine if watcher should generate events for existing files and dirs on startup.- Returns:
- true if the watcher should generate events to the listeners.
-
getListeners
Get an iterator over the listeners.- Returns:
- iterator over the listeners.
-
register
- Throws:
IOException
-
removeListener
Delete a listener- Parameters:
listener- the listener to remove- Returns:
- true if the listener existed and was removed
-
run
public void run()Forever loop. 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). -
handleWatchEvent
Add an event reported by the WatchService to list of pending events that will be sent after their quiet time has expired.- Parameters:
path- the path to add to the pending listevent- the pending event
-
setNotifyExistingOnStart
public void setNotifyExistingOnStart(boolean notify) Whether or not to issue notifications for directories and files that already exist when the watcher starts.- Parameters:
notify- true if existing paths should be notified or not
-
isNotifyExistingOnStart
public boolean isNotifyExistingOnStart() -
setUpdateQuietTime
Set the quiet time.- Parameters:
duration- the quiet time durationunit- the quite time unit
-
toString
- Overrides:
toStringin classAbstractLifeCycle
-