5

I'm using java.nio WatchService to watch the filesystem for changes (for a webapp syncing project).

However, when I clean the watched directory, I get a problem that a files is in use (in fact, I'm cleaning with maven, and maven complains it can't clean everything). That would imply that the WatchService somehow locks the watched resources.

How to watch a directory without any locking/disallowing deletion?

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140

2 Answers2

1

I have been using Apache Commons VFS2 for this purpose for long time without any issues in any OS. Basically you need a class to implement FileListener interface that lets you do actions when adding/updating/deleting files from a directory:

public interface FileListener {
    /**
     * Called when a file is created.
     */
    void fileCreated(FileChangeEvent event) throws Exception;

    /**
     * Called when a file is deleted.
     */
    void fileDeleted(FileChangeEvent event) throws Exception;

    /**
     * Called when a file is changed.
     */
    void fileChanged(FileChangeEvent event) throws Exception;
}

More info: Link to FileListener

Then you need to start the monitor for that file listener. Here you have an untested snippet on how to do it:

private void startMonitor() {
    Logger logger = LogManager.getLogger(MyClass.class);
    try {
        FileSystemManager fileSystemManager = VFS.getManager();
        FileObject dirToWatchFO = null;
        String path = "dir/you/want/to/watch";

        File pathFile = new File(path);
        path = pathFile.getAbsolutePath();
        dirToWatchFO = fileSystemManager.resolveFile(path);

        DefaultFileMonitor fileMonitor = new DefaultFileMonitor(new MyFancyFileListener());
        fileMonitor.setRecursive(false);
        fileMonitor.addFile(dirToWatchFO);
        fileMonitor.start();
    } catch (FileSystemException e) {
        logger.error("SOMETHING WENT WRONG!!", e);
    }
}

I hope it helps!

hveiga
  • 6,725
  • 7
  • 54
  • 78
0

If you want to be able to delete the entry while you're still watching it, you should add the watcher to the next directory up. Then it will lock that directory, but if you do it to a directory which will never be deleted, this won't be a problem.

Sometimes this raises the question of "what if I want to know about changes further down the tree?" If you're using the Sun JRE on Windows, you can use the modifier ExtendedWatchEventModifier.FILE_TREE which will watch for all modifications in the file tree, rather than just the immediate children. I don't know if this modifier is supported on any other platforms.

Hakanai
  • 12,010
  • 10
  • 62
  • 132
  • It is not supported on any other platforms. – sigget Jan 22 '16 at 10:50
  • @sigget I did say "If you're using the Sun JRE on Windows". ;) For others, you register every other directory you discover with the same watcher, and unregister them as they disappear. It's too bad that they can't just give us the tree version on all platforms, though, even if it has to be done the hard way. I have more confidence in the JRE implementing it correctly than I do in random developers implementing it correctly. – Hakanai Sep 30 '16 at 01:18