Search in sources :

Example 91 with FileLock

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();
        }
    }
}
Also used : FileLock(java.nio.channels.FileLock) ByteBuffer(java.nio.ByteBuffer)

Example 92 with FileLock

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;
    }
}
Also used : FileLock(java.nio.channels.FileLock)

Example 93 with FileLock

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;
}
Also used : FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) IOException(java.io.IOException) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)

Example 94 with FileLock

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();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException)

Example 95 with FileLock

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();
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) CleanMojo(org.apache.maven.plugins.clean.CleanMojo) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

FileLock (java.nio.channels.FileLock)246 IOException (java.io.IOException)127 RandomAccessFile (java.io.RandomAccessFile)99 FileChannel (java.nio.channels.FileChannel)83 File (java.io.File)77 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)37 FileOutputStream (java.io.FileOutputStream)29 Test (org.junit.Test)19 Path (java.nio.file.Path)16 FileInputStream (java.io.FileInputStream)13 FileNotFoundException (java.io.FileNotFoundException)12 ByteBuffer (java.nio.ByteBuffer)10 InputStream (java.io.InputStream)5 Properties (java.util.Properties)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 NonWritableChannelException (java.nio.channels.NonWritableChannelException)4 NoSuchFileException (java.nio.file.NoSuchFileException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3