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);
}
}
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;
}
}
}
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.");
}
}
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;
}
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
}
}
Aggregations