ActiveMQ中关于文件锁的机制的学习

阅读更多

            在ActiveMQ中提供了文件数据库机制的数据库kahadb,也即基于文件形式的数据库,文件的中操作必须采用文件锁的机制,那么是怎么实现的呢?

原理如下:

readFile = new RandomAccessFile(file, "rw"); 
FileLock lock = readFile.getChannel().tryLock(0, readFile.getChannel().size(), false);

 

具体实现代码如下:

package org.apache.kahadb.util;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.Date;

/**
 * Used to lock a File.
 * 
 * @author chirino
 */
public class LockFile {
    
    private static final boolean DISABLE_FILE_LOCK = "true".equals(System.getProperty("java.nio.channels.FileLock.broken", "false"));
    final private File file;
    
    private FileLock lock;
    private RandomAccessFile readFile;
    private int lockCounter;
    private final boolean deleteOnUnlock;
    
    public LockFile(File file, boolean deleteOnUnlock) {
        this.file = file;
        this.deleteOnUnlock = deleteOnUnlock;
    }

    /**
     * @throws IOException
     */
    synchronized public void lock() throws IOException {
        if (DISABLE_FILE_LOCK) {
            return;
        }

        if( lockCounter>0 ) {
            return;
        }
        
        IOHelper.mkdirs(file.getParentFile());
        if (System.getProperty(getVmLockKey()) != null) {
            throw new IOException("File '" + file + "' could not be locked as lock is already held for this jvm.");
        }
        if (lock == null) {
            readFile = new RandomAccessFile(file, "rw");
            IOException reason = null;
            try {
                lock = readFile.getChannel().tryLock(0, readFile.getChannel().size(), false);
            } catch (OverlappingFileLockException e) {
                reason = IOExceptionSupport.create("File '" + file + "' could not be locked.",e);
            } catch (IOException ioe) {
                reason = ioe;
            }
            if (lock != null) {
                lockCounter++;
                System.setProperty(getVmLockKey(), new Date().toString());
            } else {
                // new read file for next attempt
                closeReadFile();
                if (reason != null) {
                    throw reason;
                }
                throw new IOException("File '" + file + "' could not be locked.");
            }
              
        }
    }

    /**
     */
    public void unlock() {
        if (DISABLE_FILE_LOCK) {
            return;
        }
        
        lockCounter--;
        if( lockCounter!=0 ) {
            return;
        }
        
        // release the lock..
        if (lock != null) {
            try {
                lock.release();
                System.getProperties().remove(getVmLockKey());
            } catch (Throwable ignore) {
            }
            lock = null;
        }
        closeReadFile();
        
        if( deleteOnUnlock ) {
            file.delete();
        }
    }

    private String getVmLockKey() throws IOException {
        return getClass().getName() + ".lock." + file.getCanonicalPath();
    }

    private void closeReadFile() {
        // close the file.
        if (readFile != null) {
            try {
                readFile.close();
            } catch (Throwable ignore) {
            }
            readFile = null;
        }
        
    }

}

 

你可能感兴趣的:(ActiveMQ,Java,JVM,Apache)