Example usage for java.nio.file WatchKey reset

List of usage examples for java.nio.file WatchKey reset

Introduction

In this page you can find the example usage for java.nio.file WatchKey reset.

Prototype

boolean reset();

Source Link

Document

Resets this watch key.

Usage

From source file:Test.java

public static void main(String[] args) throws Exception {
    WatchService watcher = FileSystems.getDefault().newWatchService();

    Path dir = FileSystems.getDefault().getPath("/usr/a");

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

    while (true) {
        key = watcher.take();/*  w  ww .jav  a 2  s  .  co  m*/
        for (WatchEvent<?> event : key.pollEvents()) {
            if (event.kind() == ENTRY_MODIFY) {
                System.out.println("Home dir changed!");
            }
        }
        key.reset();
    }
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    FileSystem fileSystem = FileSystems.getDefault();
    WatchService watchService = fileSystem.newWatchService();
    Path directory = Paths.get("/home/docs");
    WatchEvent.Kind<?>[] events = { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
            StandardWatchEventKinds.ENTRY_MODIFY };
    directory.register(watchService, events);
    while (true) {
        System.out.println("Waiting for a watch event");
        WatchKey watchKey = watchService.take();
        System.out.println("Path being watched: " + watchKey.watchable());
        if (watchKey.isValid() == false) {
            return;
        }//from  w w w . j  av  a 2s  .  com
        for (WatchEvent<?> event : watchKey.pollEvents()) {
            System.out.println("Kind: " + event.kind());
            System.out.println("Context: " + event.context());
            System.out.println("Count: " + event.count());
            System.out.println();
        }
        boolean valid = watchKey.reset();
        System.out.println(valid);

    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileSystem fileSystem = FileSystems.getDefault();
    WatchService watchService = fileSystem.newWatchService();
    Path directory = Paths.get("c:/");
    WatchEvent.Kind<?>[] events = { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
            StandardWatchEventKinds.ENTRY_MODIFY };
    directory.register(watchService, events);
    while (true) {
        System.out.println("Waiting for a watch event");
        WatchKey watchKey = watchService.take();

        System.out.println("Path being watched: " + watchKey.watchable());
        System.out.println();/*w ww .  j ava 2s .c o  m*/

        if (watchKey.isValid()) {
            for (WatchEvent<?> event : watchKey.pollEvents()) {
                System.out.println("Kind: " + event.kind());
                System.out.println("Context: " + event.context());
                System.out.println("Count: " + event.count());
                System.out.println();
            }

            boolean valid = watchKey.reset();
            if (!valid) {
                // The watchKey is not longer registered
            }
        }
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    FileSystem fileSystem = FileSystems.getDefault();
    WatchService watchService = fileSystem.newWatchService();
    Path directory = Paths.get("c:/");
    WatchEvent.Kind<?>[] events = { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
            StandardWatchEventKinds.ENTRY_MODIFY };
    directory.register(watchService, events);
    while (true) {
        System.out.println("Waiting for a watch event");
        WatchKey watchKey = watchService.take();

        System.out.println("Path being watched: " + watchKey.watchable());
        System.out.println();//from w  ww  . ja  va  2 s.c o m

        if (watchKey.isValid()) {
            for (WatchEvent<?> event : watchKey.pollEvents()) {
                System.out.println("Kind: " + event.kind());
                System.out.println("Context: " + event.context());
                System.out.println("Count: " + event.count());
                System.out.println();
            }

            boolean valid = watchKey.reset();
            if (!valid) {
                // The watchKey is not longer registered
            }
        }
    }

}

From source file:Main.java

public static void main(String[] args) {
    try (WatchService ws = FileSystems.getDefault().newWatchService()) {
        Path dirToWatch = Paths.get("C:\\myName");
        dirToWatch.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY,
                StandardWatchEventKinds.ENTRY_DELETE);
        while (true) {
            WatchKey key = ws.take();
            for (WatchEvent<?> event : key.pollEvents()) {
                Kind<?> eventKind = event.kind();
                if (eventKind == StandardWatchEventKinds.OVERFLOW) {
                    System.out.println("Event  overflow occurred");
                    continue;
                }/*  ww w  . j a v a  2s.co m*/
                WatchEvent<Path> currEvent = (WatchEvent<Path>) event;
                Path dirEntry = currEvent.context();
                System.out.println(eventKind + "  occurred on  " + dirEntry);
            }
            boolean isKeyValid = key.reset();
            if (!isKeyValid) {
                System.out.println("No  longer  watching " + dirToWatch);
                break;
            }
        }
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.tomee.jul.handler.rotating.ArchivingTest.java

private static void watch(final WatchKey key) {

    if (watcherThread != null) {
        // tell the old watchter thread to shutdown
        watcherThread.interrupt();//from   w w  w.j av  a  2  s  .  c  o  m
    }

    watcherThread = new Thread("ArchivingTest.watch") {
        @Override
        public void run() {

            for (;;) {
                for (final WatchEvent<?> event : key.pollEvents()) {
                    final WatchEvent.Kind<?> kind = event.kind();

                    if (kind == StandardWatchEventKinds.OVERFLOW) {
                        continue;
                    }

                    if (watcherThread != this || isInterrupted()) {
                        return;
                    }

                    lastEvent.set(event);

                    latch.get().countDown();
                }

                final boolean valid = key.reset();
                if (!valid) {
                    System.out.println("ArchivingTest.watch terminated");
                    break;
                }
            }
        }
    };

    watcherThread.start();
}

From source file:com.playonlinux.filesystem.DirectoryWatcher.java

public void run() {
    try {//  w  w  w. jav a2s  .  c o  m
        WatchKey key = watcher.take();
        key.pollEvents();
        notifyConsumer();

        while (key.reset()) {
            key = watcher.take();
            key.pollEvents();
            notifyConsumer();
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(String.format("Watcher Interupted for %s", observedDirectory.toString()));
    }
}

From source file:request.processing.ServletLoader.java

/**
 * Watch dir monitors the current directory for any new files and calls
 * loadJar on them.//from ww w.  j  a va 2s.  c o m
 */
public static void watchDir() {

    WatchService watcher = null;
    try {
        watcher = FileSystems.getDefault().newWatchService();
        WatchKey key = servletDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE);
        servletDir.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
    } catch (IOException x) {
        System.err.println(x);
    }

    while (true) {
        WatchKey key = null;
        try {
            key = watcher.take();

        } catch (InterruptedException x) {
            System.err.println("Interrupted Exception");
            x.printStackTrace();
        }

        for (WatchEvent<?> event : key.pollEvents()) {
            WatchEvent.Kind<?> kind = event.kind();

            if (kind == StandardWatchEventKinds.OVERFLOW) {
                continue;
            }

            try {
                System.out.println("Directory Changed!");
                Thread.sleep(1000);
                loadServletsAndConfig();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            boolean valid = key.reset();
            if (!valid) {
                break;
            }
        }
    }

}

From source file:net.mindengine.dashserver.compiler.GlobalAssetsFileWatcher.java

@Override
public void run() {
    copyAllAssets();/*  w w w.j  a  va 2  s  . c o  m*/

    Path path = Paths.get(this.assetsFolderPath);
    FileSystem fileSystem = path.getFileSystem();
    try (WatchService service = fileSystem.newWatchService()) {
        path.register(service, ENTRY_MODIFY, ENTRY_CREATE);

        while (true) {
            WatchKey watchKey = service.take();

            for (WatchEvent<?> watchEvent : watchKey.pollEvents()) {
                Path watchEventPath = (Path) watchEvent.context();
                String fileName = watchEventPath.toString();
                copyFileAsset(fileName);
            }

            if (!watchKey.reset()) {
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MyWatch.java

public void watchRNDir(Path path) throws IOException, InterruptedException {
    try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
        path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY,
                StandardWatchEventKinds.ENTRY_DELETE);

        while (true) {
            // retrieve and remove the next watch key
            final WatchKey key = watchService.take();

            for (WatchEvent<?> watchEvent : key.pollEvents()) {
                final Kind<?> kind = watchEvent.kind();
                if (kind == StandardWatchEventKinds.OVERFLOW) {
                    continue;
                }/*from   www  . jav  a  2s  .  c o  m*/
                final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent;
                final Path filename = watchEventPath.context();
                System.out.println(kind + " -> " + filename);
            }

            boolean valid = key.reset();

            if (!valid) {
                break;
            }
        }
    }
}