Skip to main content

WatchService API - Detecting when a file is closed after a write?

3 replies [Last post]
Anonymous

I'm attempting to transition a project that uses the Inotify-Java library (Linux only) over to the JDK 7 WatchService API.

My problem is the following:

I can't figure out a straightforward way to detect when the writing of a file to a watched directory has completed. Inotify-Java provides a "CLOSE_WRITE" event that allows me to detect exactly this condition; however, I don't see a ready way to do this using the JDK 7 provided StandardWatchEventKind types.

The problem with StandardWatchEventKind.ENTRY_MODIFY is that I receive hundreds of them through the WatchService instance when a larger file is being copied into a watched directory. The "processing" on this file only needs to be done once and only after the file is completely written out to the watched directory.

Am I missing an easy way to solve my problem with the JDK 7 WatchService API, or is the limited number of WatchEvent.Kind types the tradeoff in order to support multiple OS types (e.g., Windows and Linux)?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
fudmail

Thanks for the response.

Given that I can't control how the user will interact with the file system (file copies, moving, etc), I think I'll keep with a Linux only implementation at this point.

However, I am still curious about transitioning off of Inotify-Java to something that more closely uses the JDK 7 WatchService API. And I get the sense I'm not lucky enough that what I want is already built into the core JDK 7.

My goal then is to expose the additional Inotify event type IN_CLOSE_WRITE (and maybe a few others).

Please let me know if I'm off-base here, but after skimming over the JDK 7 source, it looks l may have to do something like the following:

1) Create a class called LinuxWatchEventKind to implement WatchEvent.Kind
with the additional Inotify event types (e.g., IN_WRITE_CLOSE)

2) Extend the LinuxWatchService class and alter the portions that currently us StandardWatchEventKind to handle the additional Inotify event types
It looks like the native code won't need to change.

3) Then it looks like I need an alternative FileSystem implementation that will create my extended LinuxWatchService as opposed to the existing one.

If I'm making this more complicated than it needs to be let me know. Hopefully, this is in line with how this area was intended to be extended. Thoughts?

alanb
Offline
Joined: 2005-08-08
Points: 0

The nio-dev@openjdk.java.net would be a better place to bring this up.

alanb
Offline
Joined: 2005-08-08
Points: 0

The standard events have to be implementable on a wide range of platforms, and this event isn't. A WatchService can support addition implementation specific events but that might not be what you want. One idea is to move the file into the watched directory rather than copying it (or a copy it into a temporary location and use moveTo(target, ATOMIC_MOVE) to move it as an atomic operation.