Package java.nio.channels

Examples of java.nio.channels.FileChannel.tryLock()


            lockFile.deleteOnExit();

            @SuppressWarnings("resource")
            FileOutputStream out = new FileOutputStream(lockFile);
            FileChannel channel = out.getChannel();
            _fileLock = channel.tryLock();
        }
        catch (IOException ioe)
        {
            throw new StoreException("Cannot create the lock file " + lockFile.getName(), ioe);
        }
View Full Code Here


                    }
                }

                // get the lock using either try lock or not depending on if we are using timeout or not
                try {
                    lock = timeout > 0 ? channel.tryLock() : channel.lock();
                } catch (IllegalStateException ex) {
                    // Also catch the OverlappingFileLockException here. Do nothing here                   
                }
                if (lock != null) {
                    LOG.trace("Acquired exclusive read lock: {} to file: {}", lock, target);
View Full Code Here

        try {
            if ((!file.exists()) || file.canWrite()) {
                try {
                    raf = new RandomAccessFile(file, "rw");
                    final FileChannel channel = raf.getChannel();  
                    final FileLock lock = channel.tryLock();
                    if (lock == null)
                        {readOnly = true;}
                //TODO : who will release the lock ? -pb
                } catch (final NonWritableChannelException e) {
                    //No way : switch to read-only mode
View Full Code Here

                lockFile.createNewFile();
                lockFile.deleteOnExit();
            }

            FileChannel lockChannel = new FileOutputStream(lockFile).getChannel();
            lock = lockChannel.tryLock();
        } catch (IOException e)
        {
            LOG.log(Level.WARNING, "Unable to create lock", e);
        }
View Full Code Here

      try {
        channel = new RandomAccessFile(file, "rw").getChannel();

        // Use the file channel to create a lock on the file.
        // This method blocks until it can retrieve the lock.
        FileLock lock = channel.tryLock();
        if (lock == null) {
          // could not get lock - means some other instance is locking the file
          return true;
        }
        lock.release();
View Full Code Here

                    }
                }

                // get the lock using either try lock or not depending on if we are using timeout or not
                try {
                    lock = timeout > 0 ? channel.tryLock() : channel.lock();
                } catch (IllegalStateException ex) {
                    // Also catch the OverlappingFileLockException here. Do nothing here                   
                }
                if (lock != null) {
                    if (LOG.isTraceEnabled()) {
View Full Code Here

            {
                _storeRAF = new RandomAccessFile(_storeFile, "rw");
                FileChannel fileChannel = _storeRAF.getChannel();
                try
                {
                    _storeLock = fileChannel.tryLock();
                }
                catch (OverlappingFileLockException e)
                {
                    _storeLock = null;
                }
View Full Code Here

            lockFile.deleteOnExit();

            @SuppressWarnings("resource")
            FileOutputStream out = new FileOutputStream(lockFile);
            FileChannel channel = out.getChannel();
            _fileLock = channel.tryLock();
        }
        catch (IOException ioe)
        {
            throw new StoreException("Cannot create the lock file " + lockFile.getName(), ioe);
        }
View Full Code Here

                runtimeDirectory = "";
            }
            for (int i = 2;; i++) { // Try current directory, then "runtime2/", "runtime3/", etc.
                RandomAccessFile raf = new RandomAccessFile(runtimeDirectory + "lockfile.lck", "rw");
                FileChannel fc = raf.getChannel();
                pathLock = fc.tryLock();
                if (pathLock != null) {
                    break;
                }
                runtimeDirectory = "runtime" + i + "/"; // Construct a subdirectory name "runtime2/", "runtime3/", etc.
            }
View Full Code Here

    public boolean lockComponent(String name) throws FileNotFoundException {
        File file = lockFile(name);
        RandomAccessFile raf = new RandomAccessFile(file, "rwd");
        FileChannel channel = raf.getChannel();
        try {
            FileLock l = channel.tryLock();
            if (l == null) {
                Execute.close(channel);
                return false;
            }
            file.deleteOnExit();
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.