Package java.nio.channels

Examples of java.nio.channels.FileLock


        writer.doWrite(q);

        File file = new File(String.format("%s/%s-%s.json", outputPath, isQueue ? "queue": "topic", exchangeName));
        FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
        try {
            FileLock lock = channel.lock();
            try {
                mapper.writeValue(file, metric);
            }
            catch(Exception e) {
                logger.error("write metric error: ", e);
            }
            lock.release();
        }
        catch(Exception e) {
            logger.error("Cannot lock file {}", file.getAbsolutePath());
        }
        finally {


    private synchronized void writeToFile(String action) {
        Date date = new Date();
        try {
            FileOutputStream out = new FileOutputStream(logFile, true);
            FileChannel channel = out.getChannel();
            FileLock lock = channel.lock(0, Long.MAX_VALUE, false);
            PrintWriter writer = new PrintWriter(out, false);
            writer.println(DATE_FORMAT.format(date)+" - "+action+" - "+username);
            writer.flush();
            writer.close();
            if(lock.isValid()) {
                lock.release();
            }
        } catch (IOException e) {
            throw new RuntimeException("Unable to write to authentication log file", e);
        }
    }

    }

    public synchronized FileLock tryLock(long position, long size, boolean shared) throws IOException {
        if (shared) {
            // cast to FileChannel to avoid JDK 1.7 ambiguity
            return new FileLock((FileChannel) null, position, size, shared) {

                @Override
                public boolean isValid() {
                    return true;
                }

                return null;
            }
        }

        // cast to FileChannel to avoid JDK 1.7 ambiguity
        FileLock lock = new FileLock((FileChannel) null, position, size, shared) {

            @Override
            public boolean isValid() {
                return true;
            }

    private synchronized void writeToFile(String action) {
        Date date = new Date();
        try {
            FileOutputStream out = new FileOutputStream(logFile, true);
            FileChannel channel = out.getChannel();
            FileLock lock = channel.lock(0, Long.MAX_VALUE, false);
            PrintWriter writer = new PrintWriter(out, false);
            writer.println(DATE_FORMAT.format(date)+" - "+action+" - "+username);
            writer.flush();
            writer.close();
            if(lock.isValid()) {
                lock.release();
            }
        } catch (IOException e) {
            throw new RuntimeException("Unable to write to authentication log file", e);
        }
    }

    return val;
  }

  private String getFromCache(ZooKeeperCall call) throws IOException,
          InterruptedException, KeeperException {
    FileLock lock = tryLock(true, call);
    String result = null;
    BufferedReader reader = null;
    try {
      File cache = call.getDataFile(cacheDir);
      if (!cache.exists()) {
        return retrieveAndPopulateCache(cache, call);
      }
      updateReadTime(cache.getName(), cache.lastModified());
      reader = new BufferedReader(new FileReader(cache));
      result = reader.readLine();
    } finally {
      if (reader != null) {
        reader.close();
      }
      lock.release();
    }
    return result;
  }

          Thread.sleep(250);
        }
      }
    }

    FileLock lock = null;
    do {
      try {
        lock = file.getChannel().tryLock();
      } catch (OverlappingFileLockException ex) {
        // A thread inside of this JVM has the lock on the file

    return lock;
  }

  private String populateCache(ZooKeeperCall call)
      throws IOException, InterruptedException, KeeperException {
    FileLock lock = tryLock(true, call);
    String val = null;
    try {
      File cache = call.getDataFile(cacheDir);
      long fileModTime = cache.lastModified();
      Long lastFileReadTime = getReadTime(cache.getName());
      if (lastFileReadTime != null && lastFileReadTime >= fileModTime) {
        // Cache has not been updated and we need to populate it
        val = retrieveAndPopulateCache(cache, call);
      }
    } finally {
      if (lock != null) {
        lock.release();
      }
    }
    if (val == null) {
      val = getFromCache(call);
    }

      CachingAvatarZooKeeperClient cazkc = new CachingAvatarZooKeeperClient(conf, null);
      Method m = CachingAvatarZooKeeperClient.class.getDeclaredMethod(
          "tryLock", Boolean.TYPE, ZooKeeperCall.class);
      ZooKeeperCall call = cazkc.new GetStat(null);
      m.setAccessible(true);
      FileLock fl = (FileLock) m.invoke(cazkc, true, call);
      fl.release();
      TestCase.assertNotNull(fl);
      fl = (FileLock) m.invoke(cazkc, true, call);
      TestCase.assertNotNull(fl);
      fl.release();
      new File(directoryName, ".avatar_zk_cache_lock").delete();
      m.invoke(cazkc, true, call);
    } finally {
      new File(directoryName, ".avatar_zk_cache_lock").delete();
      new File(directoryName).delete();

     */
    FileLock tryLock() throws IOException {
      File lockF = new File(root, STORAGE_FILE_LOCK);
      lockF.deleteOnExit();
      RandomAccessFile file = new RandomAccessFile(lockF, "rws");
      FileLock res = null;
      try {
        res = file.getChannel().tryLock();
      } catch(OverlappingFileLockException oe) {
        file.close();
        return null;

TOP

Related Classes of java.nio.channels.FileLock

Copyright © 2018 www.massapicom. 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.