List of usage examples for java.nio.file WatchService close
@Override void close() throws IOException;
From source file:es.molabs.io.utils.test.FileWatcherRunnableTest.java
@Test public void testEntryDelete() throws Throwable { URL file = getClass().getResource("/es/molabs/io/utils/test/filewatcher/"); // Creates the file if it does not exist File newFile = new File(file.getFile() + File.separator + "test-delete.txt"); if (!newFile.exists()) newFile.createNewFile();//w w w. j a v a 2 s . c o m WatchService watchService = FileSystems.getDefault().newWatchService(); FileWatcherHandler handler = Mockito.mock(FileWatcherHandler.class); FileWatcherRunnable fileWatcherRunnable = new FileWatcherRunnable(watchService, handler); fileWatcherRunnable.addFile(file); executor.submit(fileWatcherRunnable); // Delete the file newFile.delete(); // Waits the refresh time Thread.sleep(fileWatcherRunnable.getRefreshTime() + REFRESH_MARGIN); // Checks that the event handler has been called one time Mockito.verify(handler, Mockito.times(1)).entryDelete(Mockito.any()); // Stops the service watchService.close(); }
From source file:es.molabs.io.utils.test.FileWatcherRunnableTest.java
@Test public void testEntryCreate() throws Throwable { URL file = getClass().getResource("/es/molabs/io/utils/test/filewatcher/"); // Deletes the file if already exists File newFile = new File(file.getFile() + File.separator + "test-create.txt"); if (newFile.exists()) newFile.delete();// w ww. j a v a 2s. c o m WatchService watchService = FileSystems.getDefault().newWatchService(); FileWatcherHandler handler = Mockito.mock(FileWatcherHandler.class); FileWatcherRunnable fileWatcherRunnable = new FileWatcherRunnable(watchService, handler); fileWatcherRunnable.addFile(file); executor.submit(fileWatcherRunnable); // Creates the file newFile.createNewFile(); // Waits the refresh time Thread.sleep(fileWatcherRunnable.getRefreshTime() + REFRESH_MARGIN); // Checks that the event handler has been called one time Mockito.verify(handler, Mockito.times(1)).entryCreate(Mockito.any()); // Stops the service watchService.close(); }
From source file:es.molabs.io.utils.test.FileWatcherRunnableTest.java
@Test public void testEntryModify() throws Throwable { URL file = getClass().getResource("/es/molabs/io/utils/test/filewatcher/test.txt"); WatchService watchService = FileSystems.getDefault().newWatchService(); FileWatcherHandler handler = Mockito.mock(FileWatcherHandler.class); FileWatcherRunnable fileWatcherRunnable = new FileWatcherRunnable(watchService, handler); fileWatcherRunnable.addFile(file);// w ww .j av a 2s. co m executor.submit(fileWatcherRunnable); // Writes to the file FileUtils.write(new File(file.getFile()), "test data.", Charset.defaultCharset()); // Waits the refresh time Thread.sleep(fileWatcherRunnable.getRefreshTime() + REFRESH_MARGIN); // Checks that the event handler has been called one time Mockito.verify(handler, Mockito.times(1)).entryModify(Mockito.any()); // Stops the service watchService.close(); }
From source file:org.fcrepo.http.api.ExternalContentPathValidator.java
/** * Starts up monitoring of the allowed list configuration for changes. *///from ww w . j a v a 2 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:org.fcrepo.kernel.api.utils.AutoReloadingConfiguration.java
/** * Starts up monitoring of the configuration for changes. *//*ww w . j a v a2s . c o m*/ private void monitorForChanges() { if (monitorRunning) { return; } final Path path = Paths.get(configPath); if (!path.toFile().exists()) { LOGGER.debug("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("Configuration {} has been updated, reloading.", path); try { loadConfiguration(); } catch (final IOException e) { LOGGER.error("Failed to reload configuration {}", configPath, 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:org.springframework.cloud.gcp.stream.binder.pubsub.PubSubEmulator.java
private void startEmulator() throws IOException, InterruptedException { boolean configPresent = Files.exists(EMULATOR_CONFIG_PATH); WatchService watchService = null; if (configPresent) { watchService = FileSystems.getDefault().newWatchService(); EMULATOR_CONFIG_DIR.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY); }/* www .ja v a2 s . c o m*/ try { this.emulatorProcess = new ProcessBuilder("gcloud", "beta", "emulators", "pubsub", "start").start(); } catch (IOException ex) { fail("Gcloud not found; leaving host/port uninitialized."); } if (configPresent) { updateConfig(watchService); watchService.close(); } else { createConfig(); } }
From source file:org.wso2.carbon.inbound.localfile.LocalFileOneTimePolling.java
@SuppressWarnings("unchecked") private Object watchDirectory() throws IOException { Path newPath = Paths.get(watchedDir); WatchService watchService = FileSystems.getDefault().newWatchService(); try {/*from w w w . j a v a 2 s . com*/ newPath.register(watchService, ENTRY_MODIFY); while (true) { WatchKey key = watchService.take(); if (key != null) { for (WatchEvent<?> watchEvent : key.pollEvents()) { WatchEvent.Kind<?> kind = watchEvent.kind(); WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent; Path entry = watchEventPath.context(); Path filePath = Paths.get(watchedDir, entry.toString()); if (kind == ENTRY_MODIFY) { processFile(filePath, contentType); } else if (kind == OVERFLOW) { continue; } if (log.isDebugEnabled()) { log.debug("Processing file is : " + entry); } } key.reset(); if (!key.isValid()) { break; } } } } catch (IOException e) { log.error("Error while watching directory: " + e.getMessage(), e); } catch (InterruptedException ie) { log.error("Error while get the WatchKey : " + ie.getMessage(), ie); } finally { watchService.close(); } return null; }