Package com.pnfsoftware.jeb.util.io
Class FileMonitor
java.lang.Object
com.pnfsoftware.jeb.util.io.FileMonitor
File monitors can be used to create and retrieve lock on a file for exclusive operations. Locks
are implemented as small files alongside the file to be locked.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic FileMonitorCreate a monitor object for the provided file.static FileMonitorCreate a monitor object for the provided file or group of files.static FileMonitorRetrieve the existing monitor for the provided file.static FileMonitorRetrieve the existing monitor for the provided file or group of files.booleanisLocked()Determine whether a valid lock file exists.voidlock()Attempt to create and refresh a valid lock file (per this monitor setting).voidunlock(boolean unregister) Stop refreshing the lock file.
-
Field Details
-
TOUCH_MS
public static final long TOUCH_MSTouch frequency in ms, that is, the frequency at which a lock-file write-timestamp is updated.- See Also:
-
CHECK_MS
public static final long CHECK_MSCheck frequency in ms, that is, the frequency at which a lock-file write-timestamp is verified.- See Also:
-
-
Method Details
-
get
Retrieve the existing monitor for the provided file.- Parameters:
file- monitored file- Returns:
- existing monitor, or null
- Throws:
IOException- if the file path cannot be canonicalized
-
get
Retrieve the existing monitor for the provided file or group of files.- Parameters:
folder- folder containing lock filesfilename- monitored file name or filename prefixsinglefile- true for one lock file, false for prefix-based lock files- Returns:
- existing monitor, or null
- Throws:
IOException- if the folder path cannot be canonicalized
-
create
Create a monitor object for the provided file. (Note that this method does not create any file.)- Parameters:
file- monitored file- Returns:
- monitor object
- Throws:
IOException- if the file path cannot be canonicalized
-
create
public static FileMonitor create(File folder, String filename, boolean singlefile) throws IOException Create a monitor object for the provided file or group of files. (Note that this method does not create any file.)- Parameters:
folder- folder containing lock filesfilename- monitored file name or filename prefixsinglefile- true for one lock file, false for prefix-based lock files- Returns:
- monitor object
- Throws:
IOException- if the folder path cannot be canonicalized
-
isLocked
public boolean isLocked()Determine whether a valid lock file exists.- Returns:
- true if a valid lock file (per this monitor settings) exists and is recent enough to be valid
-
lock
public void lock()Attempt to create and refresh a valid lock file (per this monitor setting). This call should be mirrored by a call tounlock(boolean)when monitoring is to stop. This method does not hard fail if the lock file cannot be created (e.g. attempting to create a lock on a read-only partition). -
unlock
Stop refreshing the lock file.- Parameters:
unregister- true to remove this monitor from the monitor registry- Throws:
InterruptedException- if interrupted while waiting for the monitor thread to stop
-