public class DefaultFileMonitor extends Object implements Runnable, FileMonitor, AutoCloseable
FileMonitor
implementation.
The DefaultFileMonitor is a Thread based polling file system monitor with a 1 second delay.
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.
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.
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 and Description |
---|
DefaultFileMonitor(FileListener listener)
Constructs a new instance with the given listener.
|
Modifier and Type | Method and 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.
Use
getDelayDuration() . |
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(Duration delay)
Sets the delay between runs.
|
void |
setDelay(long delay)
Deprecated.
Use
setDelay(Duration) . |
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.
|
public DefaultFileMonitor(FileListener listener)
listener
- the listener.public void addFile(FileObject file)
addFile
in interface FileMonitor
file
- The FileObject to monitor.public void close()
close
in interface AutoCloseable
public int getChecksPerRun()
@Deprecated public long getDelay()
getDelayDuration()
.public Duration getDelayDuration()
public boolean isRecursive()
protected void queueAddFile(FileObject file)
file
- The FileObject to add.protected void queueRemoveFile(FileObject file)
file
- The FileObject to be removed from being monitored.public void removeFile(FileObject file)
removeFile
in interface FileMonitor
file
- The FileObject to remove from monitoring.public void run()
public void setChecksPerRun(int checksPerRun)
checksPerRun
- a value less than 1 will disable this featurepublic void setDelay(Duration delay)
delay
- The delay period.@Deprecated public void setDelay(long delay)
setDelay(Duration)
.delay
- The delay period in milliseconds.public void setRecursive(boolean newRecursive)
newRecursive
- true if monitoring should be enabled for children.public void start()
public void stop()
Copyright © 2002–2025 The Apache Software Foundation. All rights reserved.