io - Filesystem lock in Java -


I am using a file system lock when reading and processing lines from a file. The reason for this is that there are more than one application server in my architecture; I only want one server to process every row to avoid data copy.

I am making a DTO in a subsidiary law. Object lock is populated with details, which can be returned by the helpful method and can be checked later if the lock is successful or not. If so, the file can be processed on-line.

Typically, if two servers process and process the line in the file at the same time, an exception is entered by one of them as the file is locked by another if such a , My code spreads an exception and just goes to the next line in the file.

Lock is implemented below:

  string filename = & lt; Processing unique identifier recording & gt;; File lockfile = new file (System.getProperty ("java.io.tmpdir"), file name); RandomAccessFile Random AccessFile = New RandomAccessFile (Lockfile, "RW"); File channel filechannel = randomxsetfile.gratname (); FileLockDTO File LockDTO = New FileLockDTO (); FileLockDTO.setLockedfile (lockfile); FileLockDTO.setRandomAccessfile (randomAccessfile); FileLockDTO.setFileLock (fileChannel.tryLock ()); For most of the time, the code works well though on rare occasions, duplicate records appear, it is a case of an edge where more than one application server Can make a lock at the same time on time. 

I have seen for a long time & amp; Tried a lot of code and trials in the code but can not identify how it will be. If somebody has any ideas, what do you think?

Thanks in advance for any assistance.

From Jawadox:

A locked area actually locked Whether or not the other program prevents another program from reaching the content, is system-based and therefore unspecified. Some system's original file locking features are only advisors, which means that programs should support a known locking protocol to guarantee integrity of the programs.

And from: (The emphasis is mine) If a lock which overlaps the requested area then it is already kept by this Java virtual machine , or if

This is a simple failover scenario for your app:

< Ol>
  • To see a query whether the lock exists on the resource 1 => false return
  • To see the server B questions whether lock is present on the resource 1 => false returns
  • Server A Run
  • Runs 1-4 lines in your code snippet on server B and succeeds (What to stop it?)
  • Server A makes DTO
  • Server B
  • Server A "Lock" records the process
  • Server B processes the "lock" record

  • Comments

    Popular posts from this blog

    import - Python ImportError: No module named wmi -

    Editing Python Class in Shell and SQLAlchemy -

    c# - MySQL Parameterized Select Query joining tables issue -