How to lock a file on different application levels?

前端 未结 6 927
萌比男神i
萌比男神i 2021-01-05 17:01

Here\'s the scenario: I have a multi threaded java web application which is running inside a servlet container. The application is deployed multiple times inside the servlet

6条回答
  •  一个人的身影
    2021-01-05 17:49

    If you only need to write the file rarely, how about writing the file under a temporary name and then using rename to make it "visible" to the readers?

    This only works reliably with Unix file systems, though. On Windows, you will need to handle the case that some process has the file open (for reading). In this case, the rename will fail. Just try again until the rename succeeds.

    I suggest to test this thoroughly because you might run into congestion: There are so many read requests that the writer task can't replace the file for a long time.

    If that is the case, make the readers check for the temporary file and wait a few moments with the next read until the file vanishes.

提交回复
热议问题