File Lock-filelock

Source: Internet
Author: User

recently in the flume part of the function of the source code, about the use of filelock, in fact, a lot of open source framework has involved, I have seen the lucene,zookeeper,hadoop,es and other open source framework are useful to the following simple introduction of the next Filelock.

        1,filelock is an exclusive lock that controls concurrent access to the same file by different programs (JVMS).
        &NBSP 2, you can lock the write file (w) and must be a writable file. Otherwise, return: Java.nio.channels.NonWritableChannelException exception, this can guarantee that only the same process can get lock to file access. Other processes cannot access the modified file or delete the directory for the file.

        &NBSP 3, because it is an exclusive lock,

        &NBSP 4, filelock life cycle, The life cycle ends when the call Filelock.release (), or channel.close (), or the JVM shuts down.

5,filelock Lock and Trylock can only be called once and released before they can continue to acquire a lock.


java.io.file.deleteonexit ()   in filelock life cycle end Yes, the file is deleted and is typically used for deletion of temporary files. forces the virtual machine to shut down, and the file is not deleted. test code:

public static void Main (string[] args) throws IOException {    File f = null;    try {        f = file.createtempfile ("tmp", ". txt");        System.out.println ("Path:" + F.getabsolutepath ());        F.deleteonexit ();        f = file.createtempfile ("tmp", null);        System.out.print ("Path:" + F.getabsolutepath ());        F.deleteonexit ();    } catch (Exception e) {        e.printstacktrace ();    }}

get Filelock exclusive lock files, can not be deleted through delete, you may deleteonexit () deleted at the end of the Filelock life cycle, test code:

    Filelock filelock = null;    File File = new file ("d:\\trylock\\", "Fish.lock");    Randomaccessfile randaccessfile = new Randomaccessfile (file, "RWS");    Gets the exclusive lock, blocked method, when the file lock is not available, the current process will be suspended    //Randaccessfile.getchannel (). Lock ();    Gets the exclusive lock, non-blocking method, when the file lock is not available, Trylock () will get a null value    Filelock = Randaccessfile.getchannel (). Trylock ();    if (Filelock! = null && file.isdirectory ()) {        file.delete ();    } else if (filelock! = null && file.i SFile ()) {//        file.delete ();//Delete no effect, must use Deleteonexit        file.deleteonexit ();//virtual machine exits, delete    }    if ( Filelock = = null) {        return;    } else {        filelock.release ();        Filelock.channel (). Close ();        Filelock = null;    }

Use in flume:

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;  }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

File Lock-filelock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.