use of java.nio.channels.FileLock in project activemq-artemis by apache.
the class FileLockNodeManager method writeFileLockStatus.
/**
* @param status
* @throws IOException
*/
private void writeFileLockStatus(byte status) throws Exception {
if (replicatedBackup && channel == null)
return;
logger.debug("writing status: " + status);
ByteBuffer bb = ByteBuffer.allocateDirect(1);
bb.put(status);
bb.position(0);
if (!channel.isOpen()) {
setUpServerLockFile();
}
FileLock lock = null;
try {
lock = lock(STATE_LOCK_POS);
channel.write(bb, 0);
channel.force(true);
} finally {
if (lock != null) {
lock.release();
}
}
}
use of java.nio.channels.FileLock in project activemq-artemis by apache.
the class FileLockNodeManager method tryLock.
protected FileLock tryLock(final long lockPos) throws IOException {
try {
logger.debug("trying to lock position: " + lockPos);
FileLock lock = channel.tryLock(lockPos, LOCK_LENGTH, false);
if (lock != null) {
logger.debug("locked position: " + lockPos);
} else {
logger.debug("failed to lock position: " + lockPos);
}
return lock;
} catch (java.nio.channels.OverlappingFileLockException ex) {
// This just means that another object on the same JVM is holding the lock
return null;
}
}
use of java.nio.channels.FileLock in project activemq-artemis by apache.
the class FileLockNodeManager method lock.
protected FileLock lock(final long lockPosition) throws Exception {
long start = System.currentTimeMillis();
while (!interrupted) {
FileLock lock = tryLock(lockPosition);
if (lock == null) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
return null;
}
if (lockAcquisitionTimeout != -1 && (System.currentTimeMillis() - start) > lockAcquisitionTimeout) {
throw new Exception("timed out waiting for lock");
}
} else {
return lock;
}
}
// todo this is here because sometimes channel.lock throws a resource deadlock exception but trylock works,
// need to investigate further and review
FileLock lock;
do {
lock = tryLock(lockPosition);
if (lock == null) {
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
//
}
}
if (interrupted) {
interrupted = false;
throw new IOException("Lock was interrupted");
}
} while (lock == null);
return lock;
}
use of java.nio.channels.FileLock in project jdepth by Crab2died.
the class FileLockUtil method main.
public static void main(String... args) {
try {
// mod 'r' 'rw' 'rws' 'rwd'
try (RandomAccessFile fis = new RandomAccessFile("D:\\资料\\myFile.txt", "rws");
FileChannel ch = fis.getChannel();
FileLock fl = ch.tryLock()) {
if (null != fl) {
int len;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) != -1) {
System.out.println(new String(buf, 0, len));
}
}
fis.seek(0);
fis.write("aaaaddda".getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
use of java.nio.channels.FileLock in project maven-clean-plugin by apache.
the class CleanMojoTest method testCleanLockedFile.
/**
* Test the removal of a locked file on Windows systems.
* <p>
* Note: Unix systems doesn't lock any files.
* </p>
*
* @throws Exception in case of an error.
*/
public void testCleanLockedFile() throws Exception {
if (!System.getProperty("os.name").toLowerCase().contains("windows")) {
assertTrue("Ignored this test on none Windows based systems", true);
return;
}
String pluginPom = getBasedir() + "/src/test/resources/unit/locked-file-test/plugin-pom.xml";
// safety
FileUtils.copyDirectory(new File(getBasedir(), "src/test/resources/unit/locked-file-test"), new File(getBasedir(), "target/test-classes/unit/locked-file-test"), null, "**/.svn,**/.svn/**");
CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom);
assertNotNull(mojo);
File f = new File(getBasedir(), "target/test-classes/unit/locked-file-test/buildDirectory/file.txt");
FileChannel channel = null;
FileLock lock = null;
try {
channel = new RandomAccessFile(f, "rw").getChannel();
lock = channel.lock();
mojo.execute();
fail("Should fail to delete a file that is locked");
} catch (MojoExecutionException expected) {
assertTrue(true);
} finally {
if (lock != null) {
lock.release();
}
if (channel != null) {
channel.close();
}
}
}
Aggregations