Class DefaultFileMonitor
- All Implemented Interfaces:
AutoCloseable,Runnable,FileMonitor
FileMonitor implementation.
The DefaultFileMonitor is a Thread based polling file system monitor with a 1 second delay.
Design:
There is a Map of monitors known as FileMonitorAgents. With the thread running, each FileMonitorAgent object is asked to "check" on the file it is responsible for. To do this check, the cache is cleared.
- If the file existed before the refresh and it no longer exists, a delete event is fired.
- If the file existed before the refresh and it still exists, check the last modified timestamp to see if that has changed.
- If it has, fire a change event.
With each file delete, the FileMonitorAgent of the parent is asked to re-build its list of children, so that they can be accurately checked when there are new children.
New files are detected during each "check" as each file does a check for new children. If new children are found, create events are fired recursively if recursive descent is enabled.
For performance reasons, added a delay that increases as the number of files monitored increases. The default is a delay of 1 second for every 1000 files processed.
Example usage:
FileSystemManager fsManager = VFS.getManager();
FileObject listenDir = fsManager.resolveFile("/home/username/monitored/");
DefaultFileMonitor fm = new DefaultFileMonitor(new CustomFileListener());
fm.setRecursive(true);
fm.addFile(listenDir);
fm.start();
(where CustomFileListener is a class that implements the FileListener interface.)-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classFile monitor agent. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Stack<FileObject> File objects to be added to the monitor map.private intSets the number of files to check until a delay will be insertedprivate static final Durationprivate static final intprivate DurationSets the delay between checksprivate final Stack<FileObject> File objects to be removed from the monitor map.private final FileListenerA listener object that if set, is notified on file creation and deletion.private static final org.apache.commons.logging.Logprivate final Map<FileName, DefaultFileMonitor.FileMonitorAgent> Map from FileName to FileObject being monitored.private ThreadThe low priority thread used for checking the files being monitored.private booleanA flag used to determine if adding files to be monitored should be recursive.private booleanA flag used to determine if the monitor thread should be running.private static final ThreadFactory -
Constructor Summary
ConstructorsConstructorDescriptionDefaultFileMonitor(FileListener listener) Constructs a new instance with the given listener. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFile(FileObject file) Adds a file to be monitored.voidclose()intGets the number of files to check per run.longgetDelay()Deprecated.Gets the delay between runs.(package private) FileListenerGets the current FileListener object notified when there are changes with the files added.booleanTests the recursive setting when adding files for monitoring.protected voidqueueAddFile(FileObject file) Queues a file for addition to be monitored.protected voidqueueRemoveFile(FileObject file) Queues a file for removal from being monitored.voidremoveFile(FileObject file) Removes a file from being monitored.voidrun()Asks the agent for each file being monitored to check its file for changes.voidsetChecksPerRun(int checksPerRun) Sets the number of files to check per run.voidsetDelay(long delay) Deprecated.UsesetDelay(Duration).voidSets the delay between runs.voidsetRecursive(boolean newRecursive) Sets the recursive setting when adding files for monitoring.voidstart()Starts monitoring the files that have been added.voidstop()Stops monitoring the files that have been added.
-
Field Details
-
THREAD_FACTORY
-
LOG
private static final org.apache.commons.logging.Log LOG -
DEFAULT_DELAY
-
DEFAULT_MAX_FILES
private static final int DEFAULT_MAX_FILES- See Also:
-
monitorMap
Map from FileName to FileObject being monitored. -
monitorThread
The low priority thread used for checking the files being monitored. -
deleteStack
File objects to be removed from the monitor map. -
addStack
File objects to be added to the monitor map. -
runFlag
private volatile boolean runFlagA flag used to determine if the monitor thread should be running. -
recursive
private boolean recursiveA flag used to determine if adding files to be monitored should be recursive. -
delay
Sets the delay between checks -
checksPerRun
private int checksPerRunSets the number of files to check until a delay will be inserted -
listener
A listener object that if set, is notified on file creation and deletion.
-
-
Constructor Details
-
DefaultFileMonitor
Constructs a new instance with the given listener.- Parameters:
listener- the listener.
-
-
Method Details
-
addFile
Adds a file to be monitored.- Specified by:
addFilein interfaceFileMonitor- Parameters:
file- The FileObject to monitor.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
getChecksPerRun
public int getChecksPerRun()Gets the number of files to check per run.- Returns:
- The number of files to check per iteration.
-
getDelay
Deprecated.UsegetDelayDuration().Gets the delay between runs.- Returns:
- The delay period in milliseconds.
-
getDelayDuration
Gets the delay between runs.- Returns:
- The delay period.
-
getFileListener
FileListener getFileListener()Gets the current FileListener object notified when there are changes with the files added.- Returns:
- The FileListener.
-
isRecursive
public boolean isRecursive()Tests the recursive setting when adding files for monitoring.- Returns:
- true if monitoring is enabled for children.
-
queueAddFile
Queues a file for addition to be monitored.- Parameters:
file- The FileObject to add.
-
queueRemoveFile
Queues a file for removal from being monitored.- Parameters:
file- The FileObject to be removed from being monitored.
-
removeFile
Removes a file from being monitored.- Specified by:
removeFilein interfaceFileMonitor- Parameters:
file- The FileObject to remove from monitoring.
-
run
public void run()Asks the agent for each file being monitored to check its file for changes. -
setChecksPerRun
public void setChecksPerRun(int checksPerRun) Sets the number of files to check per run. An additional delay will be added if there are more files to check.- Parameters:
checksPerRun- a value less than 1 will disable this feature
-
setDelay
Sets the delay between runs.- Parameters:
delay- The delay period.- Since:
- 2.10.0
-
setDelay
Deprecated.UsesetDelay(Duration).Sets the delay between runs.- Parameters:
delay- The delay period in milliseconds.
-
setRecursive
public void setRecursive(boolean newRecursive) Sets the recursive setting when adding files for monitoring.- Parameters:
newRecursive- true if monitoring should be enabled for children.
-
start
public void start()Starts monitoring the files that have been added. -
stop
public void stop()Stops monitoring the files that have been added.
-
getDelayDuration().