List of utility methods to do FileLock
void | pauseForLock(File f, int frequency, int totalTime) Waits for a file lock for a given time, with a given frequency. int waited = 0; final FileOutputStream out = new FileOutputStream(f); try { FileLock lock = out.getChannel().tryLock(); try { while (lock == null) { Thread.sleep(frequency); waited += frequency; ... |
void | releaseQuietly(FileLock lock) release Quietly try { lock.release(); } catch (Throwable t) { |
void | releaseQuietly(final FileLock lock) Releases the given lock quietly - no logging, no exception if (null != lock) { try { lock.release(); } catch (final IOException io) { |
void | releaseSilent(FileLock fileLock) release Silent if (fileLock != null) { try { fileLock.release(); } catch (IOException ex) { |
void | setNonBlockingFD(Channel c, boolean blocking) set Non Blocking FD if (c instanceof SelectableChannel) { ((SelectableChannel) c).configureBlocking(blocking); |
FileLock | tryLock(File file) try Lock FileLock toReturn = null; try { RandomAccessFile raf = new RandomAccessFile(file, "rw"); try { FileChannel channel = raf.getChannel(); toReturn = channel.tryLock(); raf.writeBytes("lock file for: " + ManagementFactory.getRuntimeMXBean().getName()); } finally { ... |
FileLock | trylock(File file, boolean shared) Use unlock to release lock and avoid channel leaks. FileChannel chan = new RandomAccessFile(file, "rw").getChannel(); return chan.tryLock(0, Long.MAX_VALUE, shared); |
boolean | tryLock(File location) try Lock if (location == null) { location = getDataFolder(); if (exclusiveLock != null) { return false; try { FileChannel lock = new RandomAccessFile(new File(location, LOCK_FILE_NAME), "rw").getChannel(); ... |
void | unblockSocket(SelectableChannel... channels) unblock Socket for (SelectableChannel ch : channels) {
ch.configureBlocking(false);
|
void | unlock(File file) unlock FileLock lock = locks.get(file); if (lock != null) { try { lock.release(); } catch (IOException e) { locks.remove(file); |