List of usage examples for java.nio.file Path register
@Override default WatchKey register(WatchService watcher, WatchEvent.Kind<?>... events) throws IOException
From source file:org.fcrepo.http.api.ExternalContentPathValidator.java
/** * Starts up monitoring of the allowed list configuration for changes. *///from w w w.j a va2 s . c o m private void monitorForChanges() { if (monitorRunning) { return; } final Path path = Paths.get(configPath); if (!path.toFile().exists()) { LOGGER.debug("Allow list configuration {} does not exist, disabling monitoring", configPath); return; } final Path directoryPath = path.getParent(); try { final WatchService watchService = FileSystems.getDefault().newWatchService(); directoryPath.register(watchService, ENTRY_MODIFY); monitorThread = new Thread(new Runnable() { @Override public void run() { try { for (;;) { WatchKey key; try { key = watchService.take(); } catch (final InterruptedException e) { LOGGER.debug("Interrupted the configuration monitor thread."); break; } for (final WatchEvent<?> event : key.pollEvents()) { final WatchEvent.Kind<?> kind = event.kind(); if (kind == OVERFLOW) { continue; } // If the configuration file triggered this event, reload it final Path changed = (Path) event.context(); if (changed.equals(path.getFileName())) { LOGGER.info("External binary configuration {} has been updated, reloading.", path); try { loadAllowedPaths(); } catch (final IOException e) { LOGGER.error("Failed to reload external locations configuration", e); } } // reset the key final boolean valid = key.reset(); if (!valid) { LOGGER.debug("Monitor of {} is no longer valid", path); break; } } } } finally { try { watchService.close(); } catch (final IOException e) { LOGGER.error("Failed to stop configuration monitor", e); } } monitorRunning = false; } }); } catch (final IOException e) { LOGGER.error("Failed to start configuration monitor", e); } monitorThread.start(); monitorRunning = true; }
From source file:eu.edisonproject.rest.FolderWatcherRunnable.java
@Override public void run() { final Path path = FileSystems.getDefault().getPath(dir); try (final WatchService watchService = FileSystems.getDefault().newWatchService()) { final WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); while (true) { final WatchKey wk = watchService.take(); for (WatchEvent<?> event : wk.pollEvents()) { final Path changed = (Path) event.context(); executeClassification(new File(dir + File.separator + changed)); }/* w w w .jav a 2 s .c o m*/ // reset the key boolean valid = wk.reset(); if (!valid) { Logger.getLogger(FolderWatcherRunnable.class.getName()).log(Level.WARNING, "Key has been unregisterede"); } } } catch (IOException ex) { Logger.getLogger(FolderWatcherRunnable.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException ex) { Logger.getLogger(FolderWatcherRunnable.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(FolderWatcherRunnable.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.mycompany.trafficimportfileconverter2.Main2Controller.java
private void watchForConsumption() throws IOException { WatchService watcher = FileSystems.getDefault().newWatchService(); try {/*from w w w . j a v a 2 s. c om*/ Path dir = getOutputDir().toPath(); WatchKey key = dir.register(watcher, ENTRY_DELETE); for (;;) { if (Thread.interrupted()) { key.cancel(); return; } try { key = watcher.take(); } catch (InterruptedException x) { return; } for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind<?> kind = event.kind(); // This key is registered only // for ENTRY_CREATE events, // but an OVERFLOW event can // occur regardless if events // are lost or discarded. if (kind == OVERFLOW) { continue; } // // The filename is the // // context of the event. WatchEvent<Path> ev = (WatchEvent<Path>) event; Path filepath = ev.context(); String filename = filepath.toString(); System.out.println("the filename was: " + filename); System.out.println(kind); Optional<String> res = findFile(filename); if (res.isPresent()) { System.out.println("BEFORE REMOVAL: " + myfiles.toString()); System.out.println("removing: " + res.get()); removeFromFiles(res.get()); System.out.println("Removed. Now: " + myfiles.toString()); int dpi = findThisDP(res.get()); if (-1 != dpi) { UI(() -> { datePickers[dpi].setStyle("-fx-background-color: lightgreen"); dayLabels[dpi].setStyle("-fx-background-color: lightgreen"); }); } log("Wide Orbit CONSUMED: " + filename); } else { System.out.println("is present was false for: " + filename); System.out.println(myfiles.toString()); } // Reset the key -- this step is critical if you want to // receive further watch events. If the key is no longer valid, // the directory is inaccessible so exit the loop. boolean valid = key.reset(); if (!valid) { return; } if (myfiles.isEmpty()) { key.cancel(); log("ALL WRITTEN FILES CONSUMED."); System.out.println("\n\n\n"); return; } } //end of events } //end of infinite loop } catch (IOException x) { System.err.println(x); } finally { Thread.currentThread().interrupt(); } }