use of java.nio.file.WatchEvent in project openscoring by openscoring.
the class DirectoryDeployer method processKey.
private void processKey(WatchKey key, Path root) throws Exception {
List<WatchEvent<?>> events = key.pollEvents();
for (WatchEvent<?> event : events) {
Path child = root.resolve((Path) event.context());
process((WatchEvent.Kind<Path>) event.kind(), child);
}
}
use of java.nio.file.WatchEvent in project HotswapAgent by HotswapProjects.
the class AbstractNIO2Watcher method processEvents.
/**
* Process all events for keys queued to the watcher
*
* @return true if should continue
* @throws InterruptedException
*/
private boolean processEvents() throws InterruptedException {
// wait for key to be signalled
WatchKey key = watcher.poll(10, TimeUnit.MILLISECONDS);
if (key == null) {
return true;
}
PathPair dir = keys.get(key);
if (dir == null) {
LOGGER.warning("WatchKey '{}' not recognized", key);
return true;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == OVERFLOW) {
LOGGER.warning("WatchKey '{}' overflowed", key);
continue;
}
// Context for directory entry event is the file name of entry
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);
LOGGER.debug("Watch event '{}' on '{}' --> {}", event.kind().name(), child, name);
dispatcher.add(ev, child);
// register it and its sub-directories
if (kind == ENTRY_CREATE) {
try {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
registerAll(dir.getWatched(), child);
}
} catch (IOException x) {
LOGGER.warning("Unable to register events for directory {}", x, child);
}
}
}
// reset key and remove from set if directory no longer accessible
boolean valid = key.reset();
if (!valid) {
LOGGER.warning("Watcher on {} not valid, removing...", keys.get(key).getShortDescription());
keys.remove(key);
// all directories are inaccessible
if (keys.isEmpty()) {
return false;
}
if (classLoaderListeners.isEmpty()) {
for (WatchKey k : keys.keySet()) {
k.cancel();
}
return false;
}
}
return true;
}
use of java.nio.file.WatchEvent 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.WatchEvent 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.WatchEvent 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;
}
Aggregations