开发者

premature end of file exception in parsing xml file when another thread is parsing it at the time other thread is creating it

I have a java thread A that continously polls a fo开发者_运维技巧lder RESULTFOLDER and checks if there are some new files present in it.

Now the files are posted by some other program running on another machine into RESULTFOLDER.Now the files posted are all xml files (only xml).so at any point RESULTFOLDEr can hold only xml files.

Now my thread A continiously polls the RESULTFOLDER and parses the xml files one at a time and then deletes it.

Now sometimes what happenes is that if thread A tries to read and parse the file A at the time the other program is posting the file A .In this case i get exception in parsing file.Saying pre mature end of file.

How can i resolve the problem?

One way i think is to check date time of file creation and ensure that file is presnt at least for 1 minute or so.But i dont think java provides such API.How can i go about solving this problem?


You can write the .xml file to the folder, and then write a separate control file written after that. The control file would have zero bytes, and have a different extension, such as .ctl, but would have the same first part of the name.

When the thread polling the result folder finds the .ctl file, it knows it is safe to open the same-named file with a .xml extension.

This approach has the added benefit that it will work even when the writing task is on another computer.


Have the creating thread call setWritable(true, true) and setReadable(true, true) on the file at creation time. This will prevent non-creating thread from accessing that file when it is being created by the creating thread. After file creation, setWritable(true, false) and setReadable(true, false). The polling thread will need to check write Ability at polling time to ensure that the file should be read from.

Alternatively, you could provide a mutex for the directory. Have the thread that is creating the file acquire the mutex for the directory, create and populate the file, then release the mutex. When the polling thread needs to do its check, grab the mutex, check the directory, process the files, then release the mutex.


Three approaches:

  1. As the file is being written, is has the name foo.tmp. Once it is completely written, it is renamed by the producer to foo.xml. Thus the consumer won't see the XML file until is is completely written by the producer.

  2. (Same answer as @aaaa bbbb). Once the file foo.xml is completely written, another file is created (which can be empty) named foo.ctl. The consumer does not process the XML file until it sees the CTL file, after which it can remove both.

  3. (Same answer as @tafoo85). The consumer cannot read the file until is is comlpetely written and made readable by the producer.

These approaches have the added benefit of working correctly even if the producer thread dies while in the middle of writing an incomplete XML file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜