如何在不同的应用程序级别上锁定文件? [英] How to lock a file on different application levels?

查看:136
本文介绍了如何在不同的应用程序级别上锁定文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是场景:我有一个在servlet容器中运行的多线程java Web应用程序。
应用程序在servlet容器中多次部署。在不同的服务器上运行多个servlet容器

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 container. There are multiple servlet containers running on different servers.

也许这个图表清楚说明:

Perhaps this graph makes it clear:

server1
+- servlet container
   +- application1
   |  +- thread1
   |  +- thread2
   +- application2
      +- thread1
      +- thread2
server2
+- servlet container
   +- application1
   |  +- thread1
   |  +- thread2
   +- application2
      +- thread1
      +- thread2

网络共享目录中有一个文件,所有这些线程都可以访问。他们经常访问该文件。
大多数情况下,文件只能被这些线程读取。但有时它是写的。

There is a file inside a network shared directory which all those threads can access. And they do access the file frequently. Most of the time the file is only read by those threads. But sometimes it is written.

无法正常工作的解决方案


  1. 使用java.nio.channels.FileLock

    我可以使用FileLock类同步来自不同服务器的线程。但是这对于同一进程(servlet容器)中的线程
    不起作用,因为文件锁在进程范围内可用。

  1. Using java.nio.channels.FileLock
    I am able to synchronize threads from different servers using the FileLock class. But this does not work for threads inside the same process (servlet container) since file locks are available process wide.

使用用于同步的单独文件

我可以创建一个单独的文件,指示进程正在读取或写入文件。此解决方案
适用于所有线程,但有几个缺点:

Using a separate file for synchronization
I could create a separate file which indicates that a process is reading from or wrinting to the file. This solution works for all threads but has several drawbacks:


  • 性能。创建,删除和检查文件的操作相当慢。使用一个同步文件的低权重实现将阻止文件的并行读取。

  • 在JVM崩溃之后,同步文件将保留,需要手动清理。

  • 我们在删除网络文件系统上的文件时遇到了奇怪的问题。

使用消息传递

我们可以实现一个消息系统,线程将用它来协调文件访问。但这对于这个问题来说似乎太复杂了。再说一次:表现会很差。

Using messaging
We could implement a messaging system which the threads would use to coordinate the file access. But this seems too complex for this problem. And again: performance will be poor.

有什么想法吗?

推荐答案

如果您只需要很少编写文件,那么如何使用临时名称编写文件然后使用重命名使其对读者可见?

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?

但这只适用于Unix文件系统。在Windows上,您将需要处理某些进程打开文件(用于读取)的情况。在这种情况下,重命名将失败。再试一次,直到重命名成功。

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.

这篇关于如何在不同的应用程序级别上锁定文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆