use of java.nio.file.ClosedWatchServiceException 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.ClosedWatchServiceException in project javaee7-samples by javaee-samples.
the class WatchingThread method run.
public void run() {
while (true) {
try {
WatchKey watchKey = watchService.take();
if (watchKey != null) {
dispatchEvents(watchKey.pollEvents(), resourceAdapter.getListener(watchKey));
watchKey.reset();
}
} catch (ClosedWatchServiceException e) {
return;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
use of java.nio.file.ClosedWatchServiceException in project moco by dreamhead.
the class WatcherService method loop.
private void loop() {
try {
WatchKey key = service.take();
Collection<Path> paths = keys.get(key);
List<WatchEvent<?>> events = key.pollEvents().stream().filter(e -> e.kind().equals(ENTRY_MODIFY)).collect(Collectors.toList());
for (WatchEvent<?> event : events) {
final Path context = (Path) event.context();
List<Path> contextPaths = paths.stream().filter(p -> p.endsWith(context)).collect(Collectors.toList());
for (Path path : contextPaths) {
for (Function<File, Void> listener : this.listeners.get(path)) {
listener.apply(path.toFile());
}
break;
}
}
key.reset();
} catch (ClosedWatchServiceException ignored) {
} catch (InterruptedException e) {
logger.error("Error happens", e);
}
}
use of java.nio.file.ClosedWatchServiceException in project jimfs by google.
the class JimfsFileSystemCloseTest method testPathMethodsThrow.
@Test
public void testPathMethodsThrow() throws IOException {
Path p = fs.getPath("/foo");
Files.createDirectory(p);
WatchService ws = fs.newWatchService();
fs.close();
try {
p.register(ws, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
fail();
} catch (ClosedWatchServiceException expected) {
}
try {
p = p.toRealPath();
fail();
} catch (ClosedFileSystemException expected) {
}
// While technically (according to the FileSystem.close() spec) all methods on Path should
// probably throw, we only throw for methods that access the file system itself in some way...
// path manipulation methods seem totally harmless to keep working, and I don't see any need to
// add the overhead of checking that the file system is open for each of those method calls.
}
use of java.nio.file.ClosedWatchServiceException in project jimfs by google.
the class JimfsFileSystemCloseTest method testOpenWatchServicesClosed.
@Test
public void testOpenWatchServicesClosed() throws IOException {
WatchService ws1 = fs.newWatchService();
WatchService ws2 = fs.newWatchService();
assertNull(ws1.poll());
assertNull(ws2.poll());
fs.close();
try {
ws1.poll();
fail();
} catch (ClosedWatchServiceException expected) {
}
try {
ws2.poll();
fail();
} catch (ClosedWatchServiceException expected) {
}
}
Aggregations