11

I'm using WatchService for synchronization data files with the application workbench. When I rename/move the watched directory I don't get any event nor the WatchKey won't become invalid. I still get events from the renamed directory but as far as I know there no way to find out the actual Path for the WatchKey besides WatchKey.watchable() which however still returns original directory path. I would like to avoid need of locking the watched directory against changes since I want to keep the application as lightweight as possible.

I have experienced this problem with JDK 7u10 on Windows 7

Do you know any workaround for this issue without locking the directory or watching all directories to the root?

UPDATE

On Linux I have observed the same behavior.

So far it seems I have three options now.

1) Rely on user's discipline that he/she won't move the data directories. I don't really like this options since it might lead to undefined behavior.

2) Use more extensive non-standard native library

3) Create hierarchy of watchdogs on superior directories. These would accept only ENTRY_DELETE events since this event (or OVERFLOW) must appear at the moment the actual watched directory is moved or deleted and thus invalid.

rjezek
  • 111
  • 2
  • 5
  • The Javadoc specifies that this uses natively available mechanisms for that, have you tried and search how Win7's mechanism acts in this case? – fge Jan 12 '13 at 14:11
  • The native mechanism on windows uses directory handle which is not changed when the directory is renamed/moved but specific event is sent. This wouldn't be an issue but in java I don't see any way to detect these windows specific events nor any way to get and examine the actual directory handle value. – rjezek Jan 12 '13 at 14:37

1 Answers1

6

My understanding is that renaming a directory will generate file system events on the old and new parent directories, not on the directory that is renamed. According to the answer to Can iNotify tell me where a monitored file is moved?, the OS cannot tell you where something was moved to unless you are monitoring the destination directory. (And besides, in Java 7/8 MOVE events aren't handled by the watch service implementation.)

UPDATE

You could try the jpathwatch project that adds support for (platform specific) extended events using the standard Java7 WatchService APIs.

References:

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Well, I don't really need to know where my directory was moved. It would be just fine to know it was moved so I can change WatchKey registration according to the new situation. I've checked JRE implementation and it seems they just throw away these events from the system. So far it seems the only way to handle this with current implementation is to watch all directories up to the root. It doesn't look very pretty, yet I think I can resolve it this way or do you have any better idea? – rjezek Jan 13 '13 at 15:10
  • According to [KEY_INVALID](http://jpathwatch.sourceforge.net/name/pachler/nio/file/ext/ExtendedWatchEventKind.html#KEY_INVALID) in jpathwatch library it seems this is an isolated Windows problem since other systems invalidate the WatchKey when it's moved. Indeed, the question is if the standard Java 7 WatchService acts like this, too, so I'll make further research of its behavior on Linux and put my results here. – rjezek Jan 14 '13 at 09:13