use of java.nio.file.WatchEvent 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.WatchEvent in project jimfs by google.
the class AbstractWatchServiceTest method testPostEvent.
@Test
public void testPostEvent() throws IOException {
AbstractWatchService.Key key = watcher.register(new StubWatchable(), ImmutableSet.of(ENTRY_CREATE));
AbstractWatchService.Event<Path> event = new AbstractWatchService.Event<>(ENTRY_CREATE, 1, null);
key.post(event);
key.signal();
assertThat(watcher.queuedKeys()).containsExactly(key);
WatchKey retrievedKey = watcher.poll();
assertThat(retrievedKey).isEqualTo(key);
List<WatchEvent<?>> events = retrievedKey.pollEvents();
assertThat(events).hasSize(1);
assertThat(events.get(0)).isEqualTo(event);
// polling should have removed all events
assertThat(retrievedKey.pollEvents()).isEmpty();
}
use of java.nio.file.WatchEvent in project crate by crate.
the class PutHeadChunkRunnable method waitUntilFileHasGrown.
private void waitUntilFileHasGrown(File pendingFile) {
try {
if (watcher == null) {
initWatcher(pendingFile.getParent());
}
watchKey = watcher.poll(5, TimeUnit.SECONDS);
if (watchKey == null) {
return;
}
for (WatchEvent<?> event : watchKey.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
@SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path filename = ev.context();
if (filename.toString().equals(pendingFile.getName())) {
break;
}
}
} catch (IOException | InterruptedException ex) {
LOGGER.warn(ex.getMessage(), ex);
}
}
use of java.nio.file.WatchEvent in project dubbo by alibaba.
the class FileSystemDynamicConfiguration method processWatchEvents.
/**
* Process the {@link WatchEvent WatchEvents} loop in async execution
*
* @param watchService {@link WatchService}
*/
private void processWatchEvents(WatchService watchService) {
getWatchEventsLoopThreadPool().execute(() -> {
// WatchEvents Loop
while (true) {
WatchKey watchKey = null;
try {
watchKey = watchService.take();
if (watchKey.isValid()) {
for (WatchEvent event : watchKey.pollEvents()) {
WatchEvent.Kind kind = event.kind();
// configChangeType's key to match WatchEvent's Kind
ConfigChangeType configChangeType = CONFIG_CHANGE_TYPES_MAP.get(kind.name());
if (configChangeType != null) {
Path configDirectoryPath = (Path) watchKey.watchable();
Path currentPath = (Path) event.context();
Path configFilePath = configDirectoryPath.resolve(currentPath);
File configDirectory = configDirectoryPath.toFile();
executeMutually(configDirectory, () -> {
fireConfigChangeEvent(configDirectory, configFilePath.toFile(), configChangeType);
signalConfigDirectory(configDirectory);
return null;
});
}
}
}
} catch (Exception e) {
return;
} finally {
if (watchKey != null) {
// reset
watchKey.reset();
}
}
}
});
}
use of java.nio.file.WatchEvent in project zookeeper by apache.
the class FileChangeWatcherTest method testCallbackWorksOnFileAdded.
@Test
public void testCallbackWorksOnFileAdded() throws IOException, InterruptedException {
FileChangeWatcher watcher = null;
try {
final List<WatchEvent<?>> events = new ArrayList<>();
watcher = new FileChangeWatcher(tempDir.toPath(), event -> {
LOG.info("Got an update: {} {}", event.kind(), event.context());
synchronized (events) {
events.add(event);
events.notifyAll();
}
});
watcher.start();
watcher.waitForState(FileChangeWatcher.State.RUNNING);
// TODO hack
Thread.sleep(1000L);
File tempFile2 = File.createTempFile("zk_test_", "", tempDir);
tempFile2.deleteOnExit();
synchronized (events) {
if (events.isEmpty()) {
events.wait(FS_TIMEOUT);
}
assertFalse(events.isEmpty());
WatchEvent<?> event = events.get(0);
assertEquals(StandardWatchEventKinds.ENTRY_CREATE, event.kind());
assertEquals(tempFile2.getName(), event.context().toString());
}
} finally {
if (watcher != null) {
watcher.stop();
watcher.waitForState(FileChangeWatcher.State.STOPPED);
}
}
}
Aggregations