• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

File locking and deleteing strategy using nio

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got a bunch of client apps all polling a single remote directory for new files, when one of the clients sees a new file it locks the file using a FileLock and copies it to another directory. Once that's done I need the client to delete the original file.

The problem is that unless I release the FileLock I can't delete the file, that's not ideal since another client could theoretically obtain a FileLock on the file before the first client gets a chance to delete.

Is there an obvious way round this?

Cheers
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about this?
  • Client polls for file "EvilFile" and finds it.
  • Client creates temporary file "EvilFile_deleting" and locks it. If either part of this step fails, another client is contending to delete the file. First to succeed should probably win and proceed.
  • Client copies original file and deletes it.
  • Client unlocks and deletes the temporary lock file.

  • You'd think that you could delete a file you had locked, though. Can you rename it before copying it? That would stop other clients from seeing it between the time you unlock and delete it.
     
    Simon Good
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And there lies the problem........

    I seem to be unable to delete or rename the file all the while I have a FileLock on that file. Is that the expected behaviour or am I possibly doing something wrong?
     
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Could you just rename it - maybe to another directory - instead of locking it? If two processes get the same filename the first one to rename would win, the second would fail.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic