1

I'm trying to use Java 7 and WatchService to monitor when folders are added to a folder (by being copied from a different location), then I want to act on the files within the newly created folder.

On OSX it works as I expect, I don't receive notification of new folder creation until the folder and its contents have been copied over. But on Windows I receive the key event on the folder creation before the contents of the folder have been copied so when I try to process the files within the folder there are not there, usually just the first file is there.

My current workaround is after receiving the folder notification I sleep for 10 seconds to wait for the files within to be copied over but this is not very satisfactory because the size of folders can vary considerably so Im going to be sleeping not long enough or too long most of the time.

Why the difference between OSX and Windows, and how can I solve my problem on Windows ?

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351

1 Answers1

0

WatchService is intended to be somewhat platform-dependent. From the Java 7 API documentation:

    The implementation that observes events from the file system is 
    intended to map directly on to the native file event notification 
    facility where available, or to use a primitive mechanism, such as 
    polling, when a native facility is not available. Consequently, many 
    of the details on how events are detected, their timeliness, and 
    whether their ordering is preserved are highly implementation specific.

Consider the following two cases.

  • A single copy operation that takes longer than the sleep.
  • Multiple copy operations into the same folder.

If you respond to the creation of the folder contents rather than the folder itself, you cover both these cases. You can also eliminate the race condition inherent in the sleep.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Intended to be platform-dependent, or just is, I read the above statement as saying using polling if nothing better is available rather than planned divergence. The task I want to perform works on a folder and it contents, it cannot work on individual files as they are added so your workaround wouldn't work for me. I have sort of solved the first issue, by counting files in folder, sleeping for two seconds, counting again, and repeating until there is no difference between the last two counts. The second scenario you mention probbbaly isnt an issue for me. – Paul Taylor Oct 22 '12 at 21:35
  • You're right that depending on sleep() leaves you open to a race condition. It can take longer than a few seconds to copy a single file. There might not be any observable difference between the two cases above, and solving the latter covers the former. If you need to operate on a set of files at a time, can you recognize when you have a complete or sufficient set? – Andy Thomas Oct 22 '12 at 22:12
  • No I cannot in a robust way, thats the problem. The usecase is the files represents songs in an album, and the folder represents the album but I do not know how many songs the album contains or even if the user has all the files of the atom. What I wanted was notification that the folder had been created and it contents had completed copying, I thought this was what OSX was giving me but it just seemed like that because it was polling and the polling occurred after the folder and its files had been copied. – Paul Taylor Oct 23 '12 at 10:47
  • Okay, say a user is missing a file when the album is first copied. A few moments later, the user has finished downloaded the file and copies it into the album copy via the operating system. How do you respond? – Andy Thomas Oct 23 '12 at 14:25
  • Im not trying to handle that case, just trying to handle when a complete folder is copied over. – Paul Taylor Oct 24 '12 at 10:15