سؤال

I'm trying to develop a watch service that, every time a piece of code is modifed, created or deleted, does a certain action.

I've made a Daemon that implements WatchService and that gets the path to a certain package to watch, for example "../JDV/src/randompackage/java/test/money_scenario".

This Daemon has the following code:

public Daemon(Path dir) throws IOException{
    this.dir = dir;
    watcher = FileSystems.getDefault().newWatchService();

    WatchKey key = dir.register(watcher, ENTRY_CREATE,ENTRY_DELETE,
            ENTRY_MODIFY);
}

public void processEvents() throws InitializationError {
    for (;;) {
        // wait for key to be signaled
        WatchKey key;
        try {
            key = watcher.take();
        } catch (InterruptedException x) {
            return;
        }
        for (WatchEvent<?> event: key.pollEvents()) {
            WatchEvent.Kind kind = event.kind();
            if (kind == OVERFLOW) {
                continue;
            }
            if(kind == ENTRY_CREATE) {
                System.out.println("Creation has been detected in " + getDirName());
            }
            if(kind == ENTRY_DELETE) {
                System.out.println("Deletion has been detected in " + getDirName());
            }
            if(kind == ENTRY_MODIFY) {
                System.out.println("Modification has been detected in " + getDirName());
            }
        }
        boolean valid = key.reset();
        if (!valid) {
                break;
        }
    }
}

What happens is the following: I run the Daemon and it is active. Whenever I go Create or Delete a class in in the money_scenario package (which itselves has subpackages from where I could delete/create the class), the Daemon detects it and prints "Modification has been detected", instead of creation/deletion. When I modify a class in the money_scenario package, it doesn't detect anything.

What am I doing wrong?

Edit: Resolved but another issue popped up. Multiple events: When I delete a class for example I get:

Modification has been detected in money_scenario Deletion has been detected in money_scenario Modification has been detected in money_scenario

هل كانت مفيدة؟

المحلول

I'm really not sure what to tell you on this one, because I took your code and it ran fine on my machine.

enter image description here

The only thing I removed was the throws InitializationError, though I doubt that it has something to do with it.

Config: Mac OS X 10.9/Java 1.7.0_45/Intellij IDEA 12.1.6

P.S.

which itselves has subpackages from where I could delete/create the class

WatchService only watches the directory that you point to, if you want to watch the subdirectories you need to recursively walk and register each directory in the subtree.

EDIT:

    public void processEvents() {
    boolean finished = false;
    while (!finished) {
        // wait for key to be signaled
        WatchKey key;
        try {
            key = watcher.take();
        } catch (InterruptedException x) {
            return;
        }
        for (WatchEvent<?> event : key.pollEvents()) {
            WatchEvent.Kind<?> kind = event.kind();
            switch (kind.name()) {
                case "ENTRY_CREATE":
                    System.out.println("Creation has been detected in " + getDirName());
                    break;
                case "ENTRY_DELETE":
                    System.out.println("Deletion has been detected in " + getDirName());
                    break;
                case "ENTRY_MODIFY":
                    System.out.println("Modification has been detected in " + getDirName());
                    break;
                default:
                    continue;
            }
            if (!key.reset()) {
                finished = true;
            }
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top