本文整理汇总了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);
}
示例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;
}
示例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");
}
}
示例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();
}
示例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;
}
示例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);
}
}
示例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;
}
示例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");
}
}
示例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");
}
}
示例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();
}
}
示例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;
}
示例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!");
}
}
}
示例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;
}
示例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();
}
}
示例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);
}