• Post Reply Bookmark Topic Watch Topic
  • New Topic

File locking and deleteing strategy using nio

 
Simon Good
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • 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
    • 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?
     
    Stan James
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • 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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!