當前位置: 首頁>>代碼示例>>Java>>正文


Java OverlappingFileLockException類代碼示例

本文整理匯總了Java中java.nio.channels.OverlappingFileLockException的典型用法代碼示例。如果您正苦於以下問題:Java OverlappingFileLockException類的具體用法?Java OverlappingFileLockException怎麽用?Java OverlappingFileLockException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


OverlappingFileLockException類屬於java.nio.channels包,在下文中一共展示了OverlappingFileLockException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: lock

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
public void lock(Object key) throws IOException {
    if (key == null) {
        throw new NullPointerException("key should not be null");
    }
    int hashCode = key.hashCode();
    FileLock fileLock;
    for (;;) {
        try {
            fileLock = fileChannel.lock(hashCode, 1, false);
            break;
        } catch (OverlappingFileLockException e) {
            Thread.yield();
        } catch (Throwable cause) {
            throw new RuntimeException("lock file fail", cause);
        }
    }
    FileLock old = fileLockThreadLocal.get();
    if (old != null) {
        old.release();
    }
    fileLockThreadLocal.set(fileLock);
}
 
開發者ID:neozhangning,項目名稱:FasterDB,代碼行數:23,代碼來源:KeyLocker.java

示例2: lock

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
private static boolean lock(File directory)
{
  String name = "node.lock";
  File lockfile = new File(directory, name);
  lockfile.deleteOnExit();
  try
  {
    FileChannel fc = (FileChannel) Channels.newChannel(new FileOutputStream(lockfile));
    lock = fc.tryLock();
    if (lock != null)
    {
      return true;
    }
  }
  catch (IOException x)
  {
    System.err.println(x.toString());
  }
  catch (OverlappingFileLockException e)
  {
    System.err.println(e.toString());
  }
  return false;
}
 
開發者ID:iisi-nj,項目名稱:GemFireLite,代碼行數:25,代碼來源:WorkPathHelper.java

示例3: lock

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
private void lock() throws IOException, BlockStoreException {
    if (!semaphores.tryAcquire()) {
        throw new BlockStoreException("File in use");
    }
    try {
        lock = file.getChannel().tryLock();
    } catch (OverlappingFileLockException e) {
        semaphores.release();
        lock = null;
    }
    if (lock == null) {
        try {
            this.file.close();
        } finally {
            this.file = null;
        }
        throw new BlockStoreException("Could not lock file");
    }
}
 
開發者ID:lvaccaro,項目名稱:BitcoinBlockExplorer,代碼行數:20,代碼來源:DiskBlockStore.java

示例4: obtain

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
/**
 * Locks the file, with a timeout (non-blocking).
 * @param timeout_ms timeout duration in milliseconds.
 * @throws IOException I/O exception occured.
 * @throws InterruptedException current thread interrupted.
 * @throws TimeoutException failed to obtain lock.
 */
public void obtain(long timeout_ms)
      throws IOException, InterruptedException, TimeoutException
{
   Long quit_time = System.currentTimeMillis() + timeout_ms;
   if (fileLock != null && fileLock.isValid())
   {
      // lock has already been obtained.
      return;
   }
   do
   {
      try
      {
         fileLock = fileToLock.tryLock();
         return;
      }
      catch (OverlappingFileLockException e)
      {
         Thread.sleep(1000);
      }
   } while (System.currentTimeMillis() < quit_time);
   throw new TimeoutException();
}
 
開發者ID:SentinelDataHub,項目名稱:dhus-core,代碼行數:31,代碼來源:AsyncFileLock.java

示例5: tryLock

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
/**
 * Attempts to acquire an exclusive lock on the directory.
 *
 * @return A lock object representing the newly-acquired lock or
 * <code>null</code> if directory is already locked.
 * @throws IOException if locking fails.
 */
@SuppressWarnings("resource")
private FileLock tryLock(File dir) throws IOException {
  File lockF = new File(dir, 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;
  } catch (IOException e) {
    LOGGER.error("Cannot create lock on " + lockF, e);
    file.close();
    throw e;
  }
  return res;
}
 
開發者ID:moueimei,項目名稱:flume-release-1.7.0,代碼行數:26,代碼來源:Log.java

示例6: shouldLockTaskStateDirectory

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
@Test
public void shouldLockTaskStateDirectory() throws Exception {
    final TaskId taskId = new TaskId(0, 0);
    final File taskDirectory = directory.directoryForTask(taskId);

    directory.lock(taskId, 0);

    try (
        final FileChannel channel = FileChannel.open(
            new File(taskDirectory, StateDirectory.LOCK_FILE_NAME).toPath(),
            StandardOpenOption.CREATE, StandardOpenOption.WRITE)
    ) {
        channel.tryLock();
        fail("shouldn't be able to lock already locked directory");
    } catch (final OverlappingFileLockException e) {
        // pass
    } finally {
        directory.unlock(taskId);
    }
}
 
開發者ID:YMCoding,項目名稱:kafka-0.11.0.0-src-with-comment,代碼行數:21,代碼來源:StateDirectoryTest.java

示例7: isPreUpgradableLayout

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
@Override
public boolean isPreUpgradableLayout(StorageDirectory sd) throws IOException {
  File oldF = new File(sd.getRoot(), "storage");
  if (!oldF.exists()) {
    return false;
  }
  // check the layout version inside the storage file
  // Lock and Read old storage file
  try (RandomAccessFile oldFile = new RandomAccessFile(oldF, "rws");
    FileLock oldLock = oldFile.getChannel().tryLock()) {
    if (null == oldLock) {
      LOG.error("Unable to acquire file lock on path " + oldF.toString());
      throw new OverlappingFileLockException();
    }
    oldFile.seek(0);
    int oldVersion = oldFile.readInt();
    if (oldVersion < LAST_PRE_UPGRADE_LAYOUT_VERSION) {
      return false;
    }
  }
  return true;
}
 
開發者ID:aliyun-beta,項目名稱:aliyun-oss-hadoop-fs,代碼行數:23,代碼來源:DataStorage.java

示例8: lock

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
private void lock() throws IOException, BlockStoreException {
    if (!semaphores.tryAcquire(fileName)) {
        throw new BlockStoreException("File in use");
    }
    try {
        lock = channel.tryLock();
    } catch (OverlappingFileLockException e) {
        semaphores.release(fileName);
        lock = null;
    }
    if (lock == null) {
        try {
            this.file.close();
        } finally {
            this.file = null;
        }
        throw new BlockStoreException("Could not lock file");
    }
}
 
開發者ID:appteam-nith,項目名稱:NithPointsj,代碼行數:20,代碼來源:BoundedOverheadBlockStore.java

示例9: lock

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
private void lock() throws IOException, BlockStoreException {
    if (!semaphores.tryAcquire(fileName)) {
        throw new BlockStoreException("File in use");
    }
    try {
        lock = file.getChannel().tryLock();
    } catch (OverlappingFileLockException e) {
        semaphores.release(fileName);
        lock = null;
    }
    if (lock == null) {
        try {
            this.file.close();
        } finally {
            this.file = null;
        }
        throw new BlockStoreException("Could not lock file");
    }
}
 
開發者ID:appteam-nith,項目名稱:NithPointsj,代碼行數:20,代碼來源:DiskBlockStore.java

示例10: test_repate_restart

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
@Test(expected = OverlappingFileLockException.class)
public void test_repate_restart() throws Exception {
    long totalMsgs = 100;
    QUEUE_TOTAL = 1;
    MessageBody = StoreMessage.getBytes();

    MessageStoreConfig messageStoreConfig = new MessageStoreConfig();
    messageStoreConfig.setMapedFileSizeCommitLog(1024 * 8);
    messageStoreConfig.setMapedFileSizeConsumeQueue(1024 * 4);
    messageStoreConfig.setMaxHashSlotNum(100);
    messageStoreConfig.setMaxIndexNum(100 * 10);
    MessageStore master = new DefaultMessageStore(messageStoreConfig, null, new MyMessageArrivingListener(), new BrokerConfig());

    boolean load = master.load();
    assertTrue(load);

    try {
        master.start();
        master.start();
    } finally {
        master.shutdown();
        master.destroy();
    }
}
 
開發者ID:apache,項目名稱:rocketmq,代碼行數:25,代碼來源:DefaultMessageStoreTest.java

示例11: tryLock

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
/**
 * Attempts to acquire an exclusive lock on the storage.
 * 
 * @return A lock object representing the newly-acquired lock or
 * <code>null</code> if storage is already locked.
 * @throws IOException if locking fails.
 */
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;
  } catch(IOException e) {
    LOG.error("Cannot create lock on " + lockF, e);
    file.close();
    throw e;
  }
  return res;
}
 
開發者ID:rhli,項目名稱:hadoop-EAR,代碼行數:25,代碼來源:Storage.java

示例12: lockCLI

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
void lockCLI(File lockPlace) throws Exception {
   if (lockPlace != null) {
      lockPlace.mkdirs();
      if (serverLockFile == null) {
         File fileLock = new File(lockPlace, "cli.lock");
         serverLockFile = new RandomAccessFile(fileLock, "rw");
      }
      try {
         FileLock lock = serverLockFile.getChannel().tryLock();
         if (lock == null) {
            throw new CLIException("Error: There is another process using the server at " + lockPlace + ". Cannot start the process!");
         }
         serverLockLock = lock;
      } catch (OverlappingFileLockException e) {
         throw new CLIException("Error: There is another process using the server at " + lockPlace + ". Cannot start the process!");
      }
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:19,代碼來源:LockAbstract.java

示例13: tryLock

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
public FileLock tryLock(Channel channel, long start, long size, boolean shared) throws IOException{
    
    if(!channel.isOpen()) {
        throw new ClosedChannelException();
    }
    
    Iterator<EphemeralFsFileLock> iter = locks.iterator();
    while(iter.hasNext()) {
        EphemeralFsFileLock oldLock = iter.next();
        if(!oldLock.isValid()) {
            iter.remove();
        }
        else if(oldLock.overlaps(start, size)) {
            throw new OverlappingFileLockException();
        }
    }
    EphemeralFsFileLock newLock = 
            channel instanceof FileChannel ? 
            new EphemeralFsFileLock((FileChannel) channel, start, size, shared) :
            new EphemeralFsFileLock((AsynchronousFileChannel) channel, start, size, shared);
   locks.add(newLock);
   return newLock;

}
 
開發者ID:sbridges,項目名稱:ephemeralfs,代碼行數:25,代碼來源:FileContents.java

示例14: testLockShared

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
@Test
public void testLockShared() throws Exception {
    Path file = root.resolve("locked");
    Files.createFile(file);
    
    FileChannel channel = (FileChannel) 
            Files.newByteChannel(file, StandardOpenOption.WRITE, StandardOpenOption.READ);
    try
    {
        FileLock lock = channel.tryLock(0, 5, true);
        assertNotNull(lock);
        assertTrue(lock.isShared());
        assertTrue(lock.isValid());
        
        try
        {
            lock = channel.tryLock(0, 5, true);
            fail();
        } catch(OverlappingFileLockException e) {
            //pass
        }
        
    } finally {
        channel.close();
    }
}
 
開發者ID:sbridges,項目名稱:ephemeralfs,代碼行數:27,代碼來源:FileLockTest.java

示例15: addLock

import java.nio.channels.OverlappingFileLockException; //導入依賴的package包/類
synchronized void addLock(FileLock lock)
        throws OverlappingFileLockException {
    long lockEnd = lock.position() + lock.size();
    for (Iterator<FileLock> keyItr = locks.iterator(); keyItr.hasNext();) {
        FileLock existingLock = keyItr.next();
        if (existingLock.position() > lockEnd) {
            // This, and all remaining locks, start beyond our end (so
            // cannot overlap).
            break;
        }
        if (existingLock.overlaps(lock.position(), lock.size())) {
            throw new OverlappingFileLockException();
        }
    }
    locks.add(lock);
}
 
開發者ID:keplersj,項目名稱:In-the-Box-Fork,代碼行數:17,代碼來源:LockManager.java


注:本文中的java.nio.channels.OverlappingFileLockException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。