public class FileLockUtility extends Object
FileLock
s and advisory locks
depending on what is supported by the platform, file access mode, and volume
on which the file resides.Modifier and Type | Field and Description |
---|---|
protected static boolean |
DEBUG
Deprecated.
|
protected static boolean |
INFO
Deprecated.
|
protected static org.apache.log4j.Logger |
log
Deprecated.
|
Constructor and Description |
---|
FileLockUtility()
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
static boolean |
acquireAdvisoryLock(File file)
Deprecated.
Creates an advisory lock file having the same basename as the given file
with a
.lock extension. |
static void |
closeFile(File file,
RandomAccessFile raf)
Deprecated.
Close the file and automatically releases the
FileLock (if any)
and removes the advisory lock for that file (if any). |
static boolean |
isOurLockFile(File lockFile)
Deprecated.
|
static RandomAccessFile |
openFile(File file,
String fileMode,
boolean useFileLock)
Deprecated.
Create/open the file and obtain an exclusive lock.
|
static void |
removeAdvisoryLock(File file)
Deprecated.
Removes the advisory lock for the file if it exists.
|
protected static final org.apache.log4j.Logger log
protected static final boolean INFO
protected static final boolean DEBUG
public static RandomAccessFile openFile(File file, String fileMode, boolean useFileLock) throws IOException
A FileLock
will be used when supported and requested. An advisory
lock will be used if useFileLock == false, if the fileMode
is read-only. If tryLock() returns null
then the lock
exists and this request will fail. However, if
FileChannel.tryLock()
throws an IOException
then the
underlying platform does not support FileLock
for the named file
(memory mapped files, read-only files, and NFS mounted files can all have
this problem) and we will attempt to acquire an advisory lock instead.
Advisory locks are NOT visible to other applications
Do NOT request a FileLock
if you are going to use a memory-mapped
buffer. The JDK cautions that these things do not play well together on
some platforms.
file
- The file.fileMode
- The file mode for
RandomAccessFile.RandomAccessFile(File, String)
useFileLock
- true
if FileChannel.tryLock()
should be
attempted. when false
only an advisory lock
will be sought.RandomAccessFile
IOException
- If the file could not be opened or someone already holds a
lock for that file.FileMetadata#acquireAdvisoryLock(File)
,
Options.FILE_LOCK_ENABLED
public static void closeFile(File file, RandomAccessFile raf) throws IOException
FileLock
(if any)
and removes the advisory lock for that file (if any).
Note: This method should be used in combination with
openFile(File, String, boolean)
in order to ensure that the
optional advisory lock file is deleted when the file is closed. The
purpose of the advisory lock file is to provide advisory locking file
modes (read-only), platforms, or file systems (NFS) that do not support
FileLock
.
file
- The file.raf
- The RandomAccessFile
.IOException
public static boolean acquireAdvisoryLock(File file) throws IOException
.lock
extension.
Note: This uses File.createNewFile()
which is NOT advised for
this purpose. However, FileLock
does not work in some contexts so
this is used as a fallback mechanism. We write a UUID
into the
advisory lock since Java does not have platform independent PIDs. That
UUID
allows us to tell whether the advisory lock file was created
by this process or by another process.
Note: If a Thread
is interrupted during an NIO operation then the
FileChannel
will be closed asynchronously. While this correctly
releases a FileLock
it does NOT cause our advisory lock file to
be deleted. During a normal shutdown of an AbstractJournal
, the
advisory lock file is deleted by
closeFile(File, RandomAccessFile)
. However, following an
abnormal shutdown the advisory lock file MAY still exist and (assuming
that FileLock
is not working since we created an advisory lock in
the first place) it MUST be removed by hand before the
AbstractJournal
can be reopened.
file
- The given file.true
if the advisory lock was created or exists
and was created by this process. false
if the
advisory lock already exists and was created by another process.IOException
- If there is a problem.pid
public static boolean isOurLockFile(File lockFile) throws IOException
IOException
public static void removeAdvisoryLock(File file) throws IOException
file
- The file whose .lock
file will be removed.IOException
- if the lock file exists but does not belong to this process
or can not be removed.acquireAdvisoryLock(File)
Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.