简体   繁体   English

具有读写模式的RandomAccessFile过滤器在Linux环境下不起作用

[英]RandomAccessFile filter with read write mode does not work on linux environment

I need help in solving an issue regarding filtering files which have not been completely written as I am getting an exception in that case. 我需要帮助来解决有关过滤尚未完全编写的文件的问题,因为在这种情况下我会遇到异常。

Scenario is: I have a program which takes in files from a queue and puts it in a location on server and another program which reads those files from that location, both these programs runs continuously and the consumer keeps poling to check if there are any files present in the location resulting in reading incomplete xml files at times and throws an error while parisng. 方案是:我有一个程序从队列中取出文件并将其放在服务器上的某个位置,另一个程序从该位置读取这些文件,这两个程序连续运行,并且使用者不断查询是否有文件出现在该位置,导致有时读取不完整的xml文件,并且在parisng时抛出错误。

I have written a code to read only those files which can be accessed in read write mode, which seems to work on windows environment but fails on linux. 我已经编写了一个代码,以仅读取那些可以在读写模式下访问的文件,该文件似乎在Windows环境下工作,但在Linux上失败。

Code snippet that runs on windows is as below: 在Windows上运行的代码段如下所示:

listOfFiles = folder.listFiles(new FileFilter() {
   public boolean accept(final File file) {
         try (RandomAccessFile stream = new RandomAccessFile(file, "rw")) {
                 return true;
         } catch(final Exception e) {
              System.out.println("skipping file: file is not completely written");
         }
         return false;
   }
});

Can someone help me with it, why this code is running on windows but not on linux or is there a better solution to this issue? 有人可以帮我解决这个问题,为什么这段代码在Windows上而不是Linux上运行,还是有更好的解决方案?

You are using the wrong technique if you are depending on R/W open telling you if the file is available. 如果您依赖R / W open告诉您文件是否可用,则使用的技术错误。

Given all the different types of filesystems available there is no builtin Java mechanism for ensuring that a file is "complete". 考虑到可用的所有不同类型的文件系统,没有内置的Java机制可确保文件“完整”。 Since you control both ends of the process, the solution is quite simple. 由于您控制过程的两端,因此解决方案非常简单。

The process that moves the files to the location where they are to be processed writes the file using a temporary filename format that the reading process knows to ignore. 将文件移到要处理的位置的过程将使用读取过程知道忽略的临时文件名格式写入文件。 When the writing process completes the writing of a file it renames the file to a name format that the reading process knows is complete. 写入过程完成文件的写入后,它将文件重命名为读取过程知道已完成的名称格式。 Then, by definition, if a file is "visible" to the reading process it is complete. 然后,根据定义,如果文件在读取过程中“可见”,则说明文件已完成。

The steps would look something like this: 步骤如下所示:

  1. The writing process creates a file in the reading location, named TEMP_nnnn_ where nnnn is chosen to make the filename unique. 写入过程会在读取位置创建一个名为TEMP_nnnn_的文件,其中选择nnnn以使文件名唯一。 The reading process knows to ignore files whose names start with TEMP. 读取过程知道忽略名称以TEMP开头的文件。
  2. The writing process copies data to the file. 写入过程会将数据复制到文件中。
  3. When the file is complete the writing process closes the file (to flush all data to disk) and renames it to FILE_nnnn_. 文件完成后,写入过程将关闭文件(以将所有数据刷新到磁盘)并将其重命名为FILE_nnnn_。
  4. The reading process becomes aware of FILE_nnnn_ and knows the file is ready to read. 读取过程知道FILE_nnnn_,并且知道文件已准备好读取。

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

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