0

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)
    ...
Edd
  • 8,402
  • 14
  • 47
  • 73
  • if the process isn't detecting the file then there would be some error message you should probably include. – barlop Nov 27 '13 at 04:39
  • 1
    http://stackoverflow.com/questions/5159220/windows-file-share-why-sometimes-newly-created-files-arent-visible-for-some-pe could be related. – halfbit Nov 27 '13 at 14:01
  • Amazing... that sounds exactly like my problem... I'll give it a shot – Edd Nov 27 '13 at 14:20
  • @halfbit please feel free to add that link as an answer – Edd Nov 28 '13 at 09:15
  • This is not possible: "Trivial answer converted to comment." But the upvote on the comment is probably sufficient to put others on the right track. - Or answer yourself and add some details? – halfbit Nov 28 '13 at 09:28

1 Answers1

1

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:

Community
  • 1
  • 1
Edd
  • 8,402
  • 14
  • 47
  • 73