سؤال

I have java process 1 which uses rpc to call java process 2 which creates a file on a mapped network drive. Process 1 then attempts to read the file. Usually the file is read ok but sometimes it can't find the file even though I can see that it does get created.

Process 1, 2 and the mapped drive are each on separate Windows Server 2008 machines. Therefore the processes are on separate JVMs and separate operating systems.

Process 2 does this sort of thing before returning (meaning that it ensures the file is written to disk before returning from the rpc call):

FileOutputStream fileOut = new FileOutputStream(new File(pdfPath));
bufferedOut = new BufferedOutputStream(fileOut);
// write to file
bufferedOut.flush();
fileOut.getFD().sync();
bufferedOut.close();

Process 1 will attempt to read the file after the rpc method call returns.

I'm sure that the file is written before process 1 attempts to read it but that some caching is taking place in either the JVM or the operating system which is preventing the process from detecting the file

There's no error until I try to read the file and it looks like this:

java.io.IOException: G:/mydir/my file ( 1.pdf not found as file or resource.
    at com.itextpdf.text.pdf.RandomAccessFileOrArray.<init>(RandomAccessFileOrArray.java:113)
    at com.itextpdf.text.pdf.RandomAccessFileOrArray.<init>(RandomAccessFileOrArray.java:80)
    at com.itextpdf.text.pdf.PRTokeniser.<init>(PRTokeniser.java:112)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:169)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:159)
    ...
هل كانت مفيدة؟

المحلول

Following halfbit's advice I used this answer to a similar question https://stackoverflow.com/a/9935126/669645 and this solved my problem

The solution was to disable SMB2 caching on all machines. To do this I added the following DWORD records to the registry under HKEY_LOCAL_MACHINE\system\CurrentControlSet\Services\LanmanWorkstation\Parameters:

  • Directory cache, by setting DirectoryCacheLifetime to ZERO.
  • File Not Found cache, by setting FileNotFoundCacheLifetime to ZERO.
  • File information cache, by setting FileInfoCacheLifetime to ZERO.

Note. To modify the registry you need to open the Registry Editor: type 'regedit' in the search box on the start menu and run regedit.exe

More info can be found on the following links:

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top