watchservice
private void startCatalogWatcher(File catalogConfigurationDir) throws IOException, InterruptedException
{
WatchService watchService = FileSystems.getDefault().newWatchService();
Paths.get(catalogConfigurationDir.getAbsolutePath()).register(
watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
WatchKey key = watchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
String name = event.kind().name();
if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
log.info("New file in catalog directory : " + event.context());
Path newCatalog = (Path) event.context();
}
else if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
log.info("Delete file from catalog directory : " + event.context());
Path deletedCatalog = (Path) event.context();
}
else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
log.info("Modify file from catalog directory : " + event.context());
Path modifiedCatalog = (Path) event.context();
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
}