简体   繁体   English

JAVA:- 多个 Java 应用程序之间的文件共享和锁定在 Linux 中不起作用

[英]JAVA :- File sharing and locking between multiple Java applications not working in Linux

I have two separate Java processes.我有两个独立的 Java 进程。 Say ABC and DEF.说 ABC 和 DEF。 DEF is the child of ABC ( That is, DEF is spawned by ABC. Both ABC and DEF are spawned using two jar files ABc.jar and DEF.jar respectively). DEF 是 ABC 的子代(也就是说,DEF 是由 ABC 生成的。ABC 和 DEF 都是分别使用两个 jar 文件 ABc.jar 和 DEF.Z68995FCBF432492D15484DAC40Z 生成的)。 My requirement is to keep a file common to both ABC and DEF.我的要求是保留 ABC 和 DEF 通用的文件。 This file is created by ABC and will be locked.该文件由 ABC 创建,将被锁定。 DEF checks if the file is unlocked. DEF 检查文件是否已解锁。 If unlocked, the process DEF exits.如果解锁,则进程 DEF 退出。 This is to account for the issue of Orphan process wherein the child process is not killed when a parent process crashes.这是为了解决孤立进程的问题,即当父进程崩溃时子进程不会被杀死。

So, In this case, the created file by ABC will remain locked until ABC exits.因此,在这种情况下,ABC 创建的文件将保持锁定状态,直到 ABC 退出。 DEF is already checking if the lock of the file is released. DEF 已经在检查文件的锁是否被释放。 Since the condition for the lock to be released being ABC terminating, DEF terminates as soon as ABC terminates.由于释放锁的条件是 ABC 终止,所以只要 ABC 终止,DEF 就会终止。

So, I have created a FileLock.java class in the ABC process:-所以,我在ABC过程中创建了一个FileLock.java class:-

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileLock;

public class FileLock {

    public FileLockExample() throws FileNotFoundException {
        FileOutputStream fileOutStream;
        try {
            fileOutStream= new FileOutputStream("Temp.txt");
            FileLock fileLock ;
            try {
                fileLock = fileOutStream.getChannel().tryLock();
                if (fileLock != null) {
                    System.out.println("Locked File");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

    }
}

So, a file Temp.txt is created and is locked.因此,创建并锁定了一个文件 Temp.txt。

Now I have added a function in DEF where I check if the file is locked.现在我在 DEF 中添加了一个 function ,我在其中检查文件是否被锁定。

public static void isFileUnlocked() {
        Integer count = 1000;
        boolean isFirstTry = false;
        while (count > 0) {
            try {
                if (!isFirstTry) {
                    FileOutputStream fos = new FileOutputStream("Temp.txt");
                    FileLock fileLock = fos.getChannel().tryLock();
                    isFirstTry = true;
                }
                FileInputStream fis = new FileInputStream("Temp.txt");
                fis.read();
                System.exit(0);
            } catch (IOException x) {

            }
          Thread.sleep(1000);
          count--;
        }
    }

The function isFileUnlocked() in process DEF checks if the file lock is released for every one second.进程 DEF 中的 function isFileUnlocked() 检查文件锁是否每秒钟释放一次。 If the lock is released, It means that ABC has been terminated due to some reasons.如果锁被释放,则说明 ABC 由于某些原因已经终止。 So, DEF exits as soon as the file lock is released.因此,一旦文件锁被释放,DEF 就会退出。

I verified this in Windows and it seems to be working.我在 Windows 中验证了这一点,它似乎正在工作。 Hwever, In Linux, the process DEF exists as soon as the function isFileUnlocked() is called.然而,在 Linux 中,只要调用 function isFileUnlocked(),进程 DEF 就存在。 Thus when DEF tries to read the file, It is able to do so.因此,当 DEF 尝试读取文件时,它能够这样做。 (Which might mean that the file is unlocked). (这可能意味着文件已解锁)。

Any thoughts on this?对此有什么想法吗?

Assuming the [locked] file is not closed when method checkIfFileIsUnlocked() is called and also assuming that you are invoking method checkIfFileIsUnlocked() with the correct parameter, in order to test whether the file is locked, you need to read from the file.假设调用方法 checkIfFileIsUnlocked( checkIfFileIsUnlocked()时 [locked] 文件没有关闭,并且假设您正在使用正确的参数调用方法checkIfFileIsUnlocked() ,为了测试文件是否被锁定,您需要从文件中读取。 Merely opening the file will not throw an exception.仅仅打开文件不会抛出异常。

This is a very badly written example but it is merely intended to demonstrate how to implement your check.这是一个写得很糟糕的例子,但它只是为了演示如何实现你的检查。


try {
    FileOutputStream fos = new FileOutputStream("lockfile.txt");
    FileLock fileLock = fos.getChannel().tryLock();

    FileInputStream fis = new FileInputStream("lockfile.txt");
    fis.read();
}
catch (IOException x) {
    x.printStackTrace();
}

When the above code runs, the following line throws an exception.当上面的代码运行时,下面一行会抛出异常。

fis.read()

The exception is:例外是:

java.io.IOException: The process cannot access the file because another process has locked a portion of the file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM