Class DefaultFileMonitor
- java.lang.Object
-
- org.apache.commons.vfs2.impl.DefaultFileMonitor
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,java.lang.Runnable
,FileMonitor
public class DefaultFileMonitor extends java.lang.Object implements java.lang.Runnable, FileMonitor, java.lang.AutoCloseable
A pollingFileMonitor
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.)
-
-
Constructor Summary
Constructors Constructor Description DefaultFileMonitor(FileListener listener)
Constructs a new instance with the given listener.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addFile(FileObject file)
Adds a file to be monitored.void
close()
int
getChecksPerRun()
Gets the number of files to check per run.long
getDelay()
Deprecated.UsegetDelayDuration()
.java.time.Duration
getDelayDuration()
Gets the delay between runs.boolean
isRecursive()
Tests the recursive setting when adding files for monitoring.protected void
queueAddFile(FileObject file)
Queues a file for addition to be monitored.protected void
queueRemoveFile(FileObject file)
Queues a file for removal from being monitored.void
removeFile(FileObject file)
Removes a file from being monitored.void
run()
Asks the agent for each file being monitored to check its file for changes.void
setChecksPerRun(int checksPerRun)
Sets the number of files to check per run.void
setDelay(long delay)
Deprecated.UsesetDelay(Duration)
.void
setDelay(java.time.Duration delay)
Sets the delay between runs.void
setRecursive(boolean newRecursive)
Sets the recursive setting when adding files for monitoring.void
start()
Starts monitoring the files that have been added.void
stop()
Stops monitoring the files that have been added.
-
-
-
Constructor Detail
-
DefaultFileMonitor
public DefaultFileMonitor(FileListener listener)
Constructs a new instance with the given listener.- Parameters:
listener
- the listener.
-
-
Method Detail
-
addFile
public void addFile(FileObject file)
Adds a file to be monitored.- Specified by:
addFile
in interfaceFileMonitor
- Parameters:
file
- The FileObject to monitor.
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
-
getChecksPerRun
public int getChecksPerRun()
Gets the number of files to check per run.- Returns:
- The number of files to check per iteration.
-
getDelay
@Deprecated public long getDelay()
Deprecated.UsegetDelayDuration()
.Gets the delay between runs.- Returns:
- The delay period in milliseconds.
-
getDelayDuration
public java.time.Duration getDelayDuration()
Gets the delay between runs.- Returns:
- The delay period.
-
isRecursive
public boolean isRecursive()
Tests the recursive setting when adding files for monitoring.- Returns:
- true if monitoring is enabled for children.
-
queueAddFile
protected void queueAddFile(FileObject file)
Queues a file for addition to be monitored.- Parameters:
file
- The FileObject to add.
-
queueRemoveFile
protected void queueRemoveFile(FileObject file)
Queues a file for removal from being monitored.- Parameters:
file
- The FileObject to be removed from being monitored.
-
removeFile
public void removeFile(FileObject file)
Removes a file from being monitored.- Specified by:
removeFile
in 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.- Specified by:
run
in interfacejava.lang.Runnable
-
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
public void setDelay(java.time.Duration delay)
Sets the delay between runs.- Parameters:
delay
- The delay period.- Since:
- 2.10.0
-
setDelay
@Deprecated public void setDelay(long delay)
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.
-
-