Search in sources :

Example 31 with WatchKey

use of java.nio.file.WatchKey in project hazelcast-jet by hazelcast.

the class StreamFilesP method drainWatcherEvents.

private void drainWatcherEvents() throws InterruptedException {
    final ILogger logger = getLogger();
    // poll with blocking only when there is no other work to do
    final WatchKey key = (currentFile == null && eventQueue.isEmpty()) ? watcher.poll(1, SECONDS) : watcher.poll();
    if (key == null) {
        if (!Files.exists(watchedDirectory)) {
            logger.info("Directory " + watchedDirectory + " does not exist, stopped watching");
            close(null);
        }
        return;
    }
    for (WatchEvent<?> event : key.pollEvents()) {
        final WatchEvent.Kind<?> kind = event.kind();
        final Path fileName = ((WatchEvent<Path>) event).context();
        final Path filePath = watchedDirectory.resolve(fileName);
        if (kind == ENTRY_CREATE || kind == ENTRY_MODIFY) {
            if (glob.matches(fileName) && belongsToThisProcessor(fileName) && !Files.isDirectory(filePath)) {
                logFine(logger, "Will open file to read new content: %s", filePath);
                eventQueue.add(filePath);
            }
        } else if (kind == ENTRY_DELETE) {
            logFinest(logger, "File was deleted: %s", filePath);
            fileOffsets.remove(filePath);
        } else if (kind == OVERFLOW) {
            logger.warning("Detected OVERFLOW in " + watchedDirectory);
        } else {
            throw new JetException("Unknown kind of WatchEvent: " + kind);
        }
    }
    if (!key.reset()) {
        logger.info("Watch key is invalid. Stopping watcher.");
        close(null);
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) ILogger(com.hazelcast.logging.ILogger) WatchEvent(java.nio.file.WatchEvent) JetException(com.hazelcast.jet.JetException)

Example 32 with WatchKey

use of java.nio.file.WatchKey in project stagen by wiztools.

the class MonitorChangesBuild method run.

@Override
public void run() {
    while (true) {
        WatchKey key;
        try {
            key = watcher.take();
        } catch (InterruptedException ex) {
            return;
        }
        key.pollEvents().stream().forEach((event) -> {
            WatchEvent.Kind<?> kind = event.kind();
            if (!(kind == OVERFLOW)) {
                WatchEvent<Path> ev = (WatchEvent<Path>) event;
                Path filename = ev.context();
                LOG.info(MessageFormat.format("Detected change: {0}", filename));
                // Generate site:
                RunnerGen gen = new RunnerGen();
                RunnerClean clean = new RunnerClean();
                try {
                    clean.run(baseDir);
                    gen.run(baseDir);
                } catch (ExecutorException | IOException ex) {
                    LOG.log(Level.WARNING, null, ex);
                }
            }
        });
        boolean valid = key.reset();
        if (!valid) {
            break;
        }
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent) IOException(java.io.IOException)

Example 33 with WatchKey

use of java.nio.file.WatchKey in project divolte-collector by divolte.

the class ExternalDatabaseLookupService method processWatchEvents.

private void processWatchEvents() {
    final Thread thisThread = Thread.currentThread();
    try {
        logger.debug("Starting to wait for watch events");
        while (!thisThread.isInterrupted()) {
            // Block for the key to be signaled.
            logger.debug("Awaiting next file notification...");
            final WatchKey key = watcher.take();
            try {
                final Path parentDirectory = (Path) key.watchable();
                for (final WatchEvent<?> event : key.pollEvents()) {
                    processWatchEvent(parentDirectory, event);
                }
            } finally {
                // Ensure that no matter what happens we will receive subsequent notifications.
                key.reset();
            }
        }
        logger.debug("Stopped processing watch events due to interruption.");
    } catch (final InterruptedException e) {
        logger.debug("Interrupted while waiting for a watch event; stopping.");
        // Preserve the interrupt flag.
        thisThread.interrupt();
    } catch (final ClosedWatchServiceException e) {
        logger.debug("Stopped processing watch events; watcher has been closed.");
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException)

Example 34 with WatchKey

use of java.nio.file.WatchKey in project streamline by hortonworks.

the class FileWatcher method processEvents.

/**
 * Blocking method to check and dispatch file events.
 * @return Returns false if file watcher will not receive any more events to indicate caller to break out of loop
 */
public boolean processEvents() {
    if (watchKeyPathMap.isEmpty()) {
        return false;
    }
    // wait for key to be signalled
    WatchKey key;
    try {
        key = watchService.take();
    } catch (ClosedWatchServiceException | InterruptedException ex) {
        LOG.info("Watch service interrupted or closed while waiting to get next watch key. Exiting!", ex);
        return false;
    }
    Path dir = watchKeyPathMap.get(key);
    if (dir == null) {
        LOG.info("Unrecognized watch key: " + key + ". Skipping the key without reseting it.");
        return true;
    }
    for (WatchEvent<?> event : key.pollEvents()) {
        WatchEvent.Kind kind = event.kind();
        if (kind == StandardWatchEventKinds.OVERFLOW) {
            LOG.warn("Overflow event received for key: " + key + ". This means events have been missed or discarded. Please verify.");
            return true;
        }
        // Context for directory entry event is the file name of entry
        WatchEvent<Path> ev = (WatchEvent<Path>) event;
        Path name = ev.context();
        Path child = dir.resolve(name);
        LOG.info("{}: {}", event.kind().name(), child);
        try {
            if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
                watchKeyFileEventHandlerMap.get(key).created(child);
            } else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
                watchKeyFileEventHandlerMap.get(key).modified(child);
            } else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
                watchKeyFileEventHandlerMap.get(key).deleted(child);
            }
        } catch (RuntimeException ex) {
            LOG.warn("Exception thrown by handler {} while processing event {}", watchKeyFileEventHandlerMap.get(key), event.kind().name(), ex);
        }
    }
    // reset key and remove from set if directory no longer accessible
    boolean valid = key.reset();
    if (!valid) {
        LOG.info("Key " + key + " not being watched any more as it could not be reset.");
        watchKeyPathMap.remove(key);
        watchKeyFileEventHandlerMap.remove(key);
    }
    return true;
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException) WatchEvent(java.nio.file.WatchEvent)

Example 35 with WatchKey

use of java.nio.file.WatchKey in project meghanada-server by mopemope.

the class FileSystemWatcher method start.

public void start(final List<File> files) throws IOException {
    this.abort = false;
    try (final FileSystem fileSystem = FileSystems.getDefault();
        final WatchService watchService = fileSystem.newWatchService()) {
        this.watchKeyHolder = new WatchKeyHolder(watchService);
        for (final File root : files) {
            if (root.exists()) {
                final Path rootPath = root.toPath();
                this.watchKeyHolder.walk(rootPath);
            }
        }
        this.started = true;
        while (!abort) {
            final WatchKey key = watchService.take();
            this.handleEvent(this.watchKeyHolder, key);
            if (!key.reset()) {
                this.watchKeyHolder.remove(key);
            }
            this.watchKeyHolder.sweep();
            if (this.watchKeyHolder.isEmpty()) {
                break;
            }
        }
    } catch (InterruptedException e) {
    // ignore
    }
}
Also used : Path(java.nio.file.Path) FileSystem(java.nio.file.FileSystem) WatchKey(java.nio.file.WatchKey) WatchService(java.nio.file.WatchService) File(java.io.File)

Aggregations

WatchKey (java.nio.file.WatchKey)134 Path (java.nio.file.Path)88 WatchEvent (java.nio.file.WatchEvent)65 IOException (java.io.IOException)49 WatchService (java.nio.file.WatchService)30 File (java.io.File)27 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)18 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)7 Map (java.util.Map)7 FileSystems (java.nio.file.FileSystems)6 FileVisitResult (java.nio.file.FileVisitResult)6 HashSet (java.util.HashSet)6 List (java.util.List)6 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)5 InputStream (java.io.InputStream)4 AccessDeniedException (java.nio.file.AccessDeniedException)4 Kind (java.nio.file.WatchEvent.Kind)4 Logger (org.slf4j.Logger)4