FileLock in Java doesn't work in Docke mount volume

六眼飞鱼酱① 提交于 2020-08-10 19:35:09

问题


In my situation, some web configuration files are shared through mount folder in Docker. In same cases we want to modify these files concurrently. That's why I want to use lock to make sure file is being modified once at the same time. But I found flock is not working in Docker. Does it not supported?

 public void modifyFile() {

    try {
        File file = new File("/tmp/fileToLock.dat");

        // Creates a random access file stream to read from, and optionally to write to
        FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

        // Acquire an exclusive lock on this channel's file (blocks until lock can be retrieved)
        FileLock lock = null;

        // Attempts to acquire an exclusive lock on this channel's file (returns null or throws
        // an exception if the file is already locked.
        try {
            lock = channel.tryLock();
            if (null != lock) {
                List<String> fileToString = FileUtils.readLines(file, StandardCharsets.UTF_8);
                long l = 0l;
                if (null != fileToString && fileToString.size() > 0) {
                    l = Long.valueOf(fileToString.get(fileToString.size() - 1));
                }
                l++;
                FileUtils.writeStringToFile(file, String.valueOf(l) + "\r\n", StandardCharsets.UTF_8, true);
            }
        } catch (OverlappingFileLockException e) {
            // thrown when an attempt is made to acquire a lock on a a file that overlaps
            // a region already locked by the same JVM or when another thread is already
            // waiting to lock an overlapping region of the same file
            System.out.println("Overlapping File Lock Error: " + e.getMessage());
            channel.close();
        }

        // release the lock
        if (null != lock) {
            lock.release();
        }
        // close the channel
        channel.close();

    } catch (IOException e) {
        System.out.println("I/O Error: " + e.getMessage());
    }

}

来源:https://stackoverflow.com/questions/63029410/filelock-in-java-doesnt-work-in-docke-mount-volume

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!