Search in sources :

Example 91 with WatchEvent

use of java.nio.file.WatchEvent in project sldeditor by robward-scisys.

the class FileSystemWatcher method internalWatchDirectoryPath.

/**
 * Internal watch directory path method.
 *
 * @throws InterruptedException
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void internalWatchDirectoryPath() {
    WatchKey key = null;
    // Poll for events in an infinite loop
    for (; ; ) {
        // notification
        try {
            key = watchService.take();
        } catch (InterruptedException e) {
        // Do nothing
        }
        // once a key is obtained, we poll for events on that key
        if (key != null) {
            List<WatchEvent<?>> keys = key.pollEvents();
            processWatchEvents(key, keys);
            // Reset the key so the further key events may be polled
            key.reset();
        }
        if (stopPolling) {
            break;
        }
    }
}
Also used : WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent)

Example 92 with WatchEvent

use of java.nio.file.WatchEvent in project Singularity by HubSpot.

the class WatchServiceHelper method processWatchKey.

private void processWatchKey(WatchKey watchKey) throws IOException {
    final long start = System.currentTimeMillis();
    final List<WatchEvent<?>> events = watchKey.pollEvents();
    int processed = 0;
    for (WatchEvent<?> event : events) {
        WatchEvent.Kind<?> kind = event.kind();
        if (!watchEvents.contains(kind)) {
            LOG.trace("Ignoring an {} event to {}", event.kind(), event.context());
            continue;
        }
        WatchEvent<Path> ev = cast(event);
        Path filename = ev.context();
        if (processEvent(kind, filename)) {
            processed++;
        }
    }
    LOG.debug("Handled {} out of {} event(s) for {} in {}", processed, events.size(), watchDirectory, JavaUtils.duration(start));
}
Also used : Path(java.nio.file.Path) WatchEvent(java.nio.file.WatchEvent)

Aggregations

WatchEvent (java.nio.file.WatchEvent)92 Path (java.nio.file.Path)68 WatchKey (java.nio.file.WatchKey)58 IOException (java.io.IOException)33 File (java.io.File)27 Test (org.junit.Test)24 WatchService (java.nio.file.WatchService)20 BuckEventBus (com.facebook.buck.event.BuckEventBus)13 FakeClock (com.facebook.buck.timing.FakeClock)13 EventBus (com.google.common.eventbus.EventBus)12 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)10 EasyMock.anyObject (org.easymock.EasyMock.anyObject)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)6 Subscribe (com.google.common.eventbus.Subscribe)5 FakeWatchmanClient (com.facebook.buck.io.FakeWatchmanClient)4 FileSystems (java.nio.file.FileSystems)4 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)4 HashSet (java.util.HashSet)4 List (java.util.List)4