Java Code Examples for java.nio.file.WatchService#close()
The following examples show how to use
java.nio.file.WatchService#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LotsOfCancels.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
/** * Stress the given WatchService, specifically the cancel method, in * the given directory. Closes the WatchService when done. */ static void handle(Path dir, WatchService watcher) { try { try { Path file = dir.resolve("anyfile"); for (int i=0; i<2000; i++) { WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE); Files.createFile(file); Files.delete(file); key.cancel(); } } finally { watcher.close(); } } catch (Exception e) { e.printStackTrace(); failed = true; } }
Example 2
Source File: LotsOfCancels.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
/** * Stress the given WatchService, specifically the cancel method, in * the given directory. Closes the WatchService when done. */ static void handle(Path dir, WatchService watcher) { try { try { Path file = dir.resolve("anyfile"); for (int i=0; i<2000; i++) { WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE); Files.createFile(file); Files.delete(file); key.cancel(); } } finally { watcher.close(); } } catch (Exception e) { e.printStackTrace(); failed = true; } }
Example 3
Source File: LotsOfCancels.java From hottub with GNU General Public License v2.0 | 6 votes |
/** * Stress the given WatchService, specifically the cancel method, in * the given directory. Closes the WatchService when done. */ static void handle(Path dir, WatchService watcher) { try { try { Path file = dir.resolve("anyfile"); for (int i=0; i<2000; i++) { WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE); Files.createFile(file); Files.delete(file); key.cancel(); } } finally { watcher.close(); } } catch (Exception e) { e.printStackTrace(); failed = true; } }
Example 4
Source File: PubSubEmulator.java From spring-cloud-gcp with Apache License 2.0 | 6 votes |
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); } 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(); } }
Example 5
Source File: LotsOfCancels.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
/** * Stress the given WatchService, specifically the cancel method, in * the given directory. Closes the WatchService when done. */ static void handle(Path dir, WatchService watcher) { try { try { Path file = dir.resolve("anyfile"); for (int i=0; i<2000; i++) { WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE); Files.createFile(file); Files.delete(file); key.cancel(); } } finally { watcher.close(); } } catch (Exception e) { e.printStackTrace(); failed = true; } }
Example 6
Source File: LotsOfCancels.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Stress the given WatchService, specifically the cancel method, in * the given directory. Closes the WatchService when done. */ static void handle(int id, Path dir, WatchService watcher) { System.out.printf("begin handle %d%n", id); try { try { Path file = dir.resolve("anyfile"); for (int i=0; i<2000; i++) { WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE); Files.createFile(file); Files.delete(file); key.cancel(); } } finally { System.out.printf("WatchService %d closing ...%n", id); watcher.close(); System.out.printf("WatchService %d closed %n", id); } } catch (Exception e) { e.printStackTrace(); failed = true; } System.out.printf("end handle %d%n", id); }
Example 7
Source File: LotsOfCancels.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
/** * Stress the given WatchService, specifically the cancel method, in * the given directory. Closes the WatchService when done. */ static void handle(Path dir, WatchService watcher) { try { try { Path file = dir.resolve("anyfile"); for (int i=0; i<2000; i++) { WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE); Files.createFile(file); Files.delete(file); key.cancel(); } } finally { watcher.close(); } } catch (Exception e) { e.printStackTrace(); failed = true; } }
Example 8
Source File: FileWatcher.java From atlas with Apache License 2.0 | 6 votes |
public void start() throws IOException { if (existsAndReadyCheck()) { return; } WatchService watcher = FileSystems.getDefault().newWatchService(); Path pathToWatch = FileSystems.getDefault().getPath(fileToWatch.getParent()); register(watcher, pathToWatch); try { LOG.info(String.format("Migration File Watcher: Watching: %s", fileToWatch.toString())); startWatching(watcher); } catch (InterruptedException ex) { LOG.error("Migration File Watcher: Interrupted!"); } finally { watcher.close(); } }
Example 9
Source File: LotsOfCancels.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
/** * Stress the given WatchService, specifically the cancel method, in * the given directory. Closes the WatchService when done. */ static void handle(Path dir, WatchService watcher) { try { try { Path file = dir.resolve("anyfile"); for (int i=0; i<2000; i++) { WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE); Files.createFile(file); Files.delete(file); key.cancel(); } } finally { watcher.close(); } } catch (Exception e) { e.printStackTrace(); failed = true; } }
Example 10
Source File: LotsOfCancels.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
/** * Stress the given WatchService, specifically the cancel method, in * the given directory. Closes the WatchService when done. */ static void handle(Path dir, WatchService watcher) { try { try { Path file = dir.resolve("anyfile"); for (int i=0; i<2000; i++) { WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE); Files.createFile(file); Files.delete(file); key.cancel(); } } finally { watcher.close(); } } catch (Exception e) { e.printStackTrace(); failed = true; } }
Example 11
Source File: LotsOfCloses.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Returns a task that closes the given WatchService. */ static Callable<Void> newCloserTask(WatchService watcher) { return () -> { try { watcher.close(); return null; } catch (IOException ioe) { throw new UncheckedIOException(ioe); } }; }
Example 12
Source File: SVMJVMImpl.java From visualvm with GNU General Public License v2.0 | 5 votes |
public File takeHeapDump() throws IOException { if (!isTakeHeapDumpSupported()) { throw new UnsupportedOperationException(); } String cwd = monitoredVm.findByName(USER_DIR_COUNTER_NAME); Path applicationCwd = Paths.get(cwd); WatchService watchService = FileSystems.getDefault().newWatchService(); WatchKey key = applicationCwd.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); Runtime.getRuntime().exec(new String[] {"kill", "-USR1", String.valueOf(application.getPid())}); try { Path name = findHeapDumpFile(key); if (name == null) { key = watchService.poll(20, TimeUnit.SECONDS); name = findHeapDumpFile(key); } watchService.close(); if (name == null) { return null; } File dumpFile = applicationCwd.resolve(name).toFile(); waitDumpDone(dumpFile); return dumpFile; } catch (InterruptedException ex) { watchService.close(); return null; } }
Example 13
Source File: TestWatchService.java From jsr203-hadoop with Apache License 2.0 | 5 votes |
@Test @Ignore public void testSimple() throws IOException { Path rootPath = Paths.get(clusterUri); WatchService watcher = rootPath.getFileSystem().newWatchService(); rootPath.register(watcher, new WatchEvent.Kind<?>[] { ENTRY_MODIFY }); watcher.close(); }
Example 14
Source File: TestWatchService.java From jsr203-hadoop with Apache License 2.0 | 5 votes |
@Test(expected=ClosedWatchServiceException.class) @Ignore public void testSimpleEx() throws IOException { Path rootPath = Paths.get(clusterUri); WatchService watcher = rootPath.getFileSystem().newWatchService(); rootPath.register(watcher, new WatchEvent.Kind<?>[] { ENTRY_MODIFY }); watcher.close(); // Should throw ClosedWatchServiceException watcher.poll(); }
Example 15
Source File: DirectoryWatcherTest.java From openjdk-systemtest with Apache License 2.0 | 4 votes |
public void testEntryDelete() throws IOException, InterruptedException, StfException { Path tempDirectory = getTemporaryDirectory(); WatchService watchService = FileSystems.getDefault().newWatchService(); try { // Create the file before registering the WatchService, so that // we don't need to pause before deleting the file Path newFilename = Paths.get("ENTRY_DELETE.txt"); Path newFile = tempDirectory.resolve(newFilename); Files.createFile(newFile); assertTrue("File was not created", Files.exists(newFile)); tempDirectory.register(watchService, StandardWatchEventKinds.ENTRY_DELETE); Files.delete(newFile); WatchKey key = null; // We will give it POLL_TIMEOUT_SECONDS seconds, if it hasn't got // something by // then we assume failure key = watchService.poll(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS); assertNotNull("Polling WatchService object returned null", key); boolean eventFound = false; // Check for exactly one event for (WatchEvent<?> event : key.pollEvents()) { if (event.kind().equals(StandardWatchEventKinds.ENTRY_DELETE)) { // Assert exactly one event assertFalse("Duplicate ENTRY_DELETE events delivered for one file deletion", eventFound); // Assert filename is correct assertEquals(newFilename, (Path) event.context()); eventFound = true; } else { fail(event.kind() + " event retured, expected ENTRY_DELETE"); } } // Reset the key, to allow for future events key.reset(); } finally { watchService.close(); } okForCleanup(); }
Example 16
Source File: DirectoryWatcherTest.java From openjdk-systemtest with Apache License 2.0 | 4 votes |
public void testDirectoryWatcher() throws IOException, StfException { Path tempDirectory = getTemporaryDirectory(); WatchService watchService = null; // Obtain watch service for the default file system and register // an interest in knowing when files are created watchService = FileSystems.getDefault().newWatchService(); try { tempDirectory.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); // Hard-coded values of how many files to touch int numberOfIterationsRemaining = 10; int numberOfFiles = 5; int totalFiles = numberOfFiles * numberOfIterationsRemaining; long startTime = System.currentTimeMillis(); // Our variables containing information as to whether we are going // to pass // or fail int filesFound = 0; boolean discardedEvents = false; LOOP: while (true) { WatchKey key; // Spread out the creating of files, interspersing them with polls. if (numberOfIterationsRemaining > 0) { createFiles(tempDirectory, numberOfFiles); numberOfIterationsRemaining--; } // Try taking a key off the service, if available (null if not) key = watchService.poll(); if (key != null) { // Retrieve each event for (WatchEvent<?> event : key.pollEvents()) { if (event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)) { // New file found filesFound++; } else if (event.kind().equals(StandardWatchEventKinds.OVERFLOW)) { // Overflow occurred. // This is sort of a warning, but expected if we // (the JVM?) can't process // the events fast enough discardedEvents = true; } } // Important to reset the key key.reset(); } long duration = System.currentTimeMillis() - startTime; // Check if we have received everything we intended to // if so, we can break the loop if (numberOfIterationsRemaining == 0) { if (filesFound == totalFiles) { break LOOP; } if (discardedEvents == true) { break LOOP; } } // Check to see if we have been doing this for too long // if so, then break the loop if (duration > TIMEOUT) { fail("Timeout exceeded: " + filesFound + " files found in " + duration + "ms"); break LOOP; } } System.out.println("Summary: totalfile - " + totalFiles + " , files found " + filesFound); if (discardedEvents == false && (totalFiles != filesFound)) { fail("No events were knowingly discarded, but the total number of files found does not equal the number created"); } if (discardedEvents == true) { System.out.println("Warning, events were discarded."); } } finally { watchService.close(); } okForCleanup(); }
Example 17
Source File: DirectoryWatcherTest.java From openjdk-systemtest with Apache License 2.0 | 4 votes |
public void testEntryModify() throws IOException, InterruptedException, StfException { Path tempDirectory = getTemporaryDirectory(); WatchService watchService = FileSystems.getDefault().newWatchService(); try { Path newFilename = Paths.get("ENTRY_MODIFY.txt"); Path newFile = tempDirectory.resolve(newFilename); Files.createFile(newFile); assertTrue("File was not created", Files.exists(newFile)); // We need to pause so that the implementation on AIX and zOS // notices the modification time // has changed, which only work to the second apparently. try { Thread.sleep(2000); } catch (InterruptedException e) { } tempDirectory.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY); // Modify file OutputStream out = Files.newOutputStream(newFile, StandardOpenOption.WRITE); out.write("A".getBytes()); out.close(); WatchKey key = null; // We will give it POLL_TIMEOUT_SECONDS seconds, if it hasn't got // something by // then we assume failure key = watchService.poll(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS); assertNotNull("Polling WatchService object returned null", key); boolean eventFound = false; // Check for exactly one event for (WatchEvent<?> event : key.pollEvents()) { if (event.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY)) { // Assert exactly one event assertFalse("Duplicate ENTRY_MODIFY events delivered for one file modification", eventFound); // Assert filename is correct assertEquals(newFilename, (Path) event.context()); eventFound = true; } else { fail(event.kind() + " event retured, expected ENTRY_MODIFY"); } } // Reset the key, to allow for future events key.reset(); } finally { watchService.close(); } okForCleanup(); }
Example 18
Source File: DirectoryWatcherTest.java From openjdk-systemtest with Apache License 2.0 | 4 votes |
public void testMultipleEntryCreate() throws IOException, InterruptedException, StfException { Path tempDirectory = getTemporaryDirectory(); WatchService watchService = FileSystems.getDefault().newWatchService(); try { tempDirectory.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); WatchKey key = null; for (int iteration = 0; iteration < 10; iteration++) { HangNotifier.ping(); Path newFilename = Paths.get("ENTRY_CREATE" + iteration + ".txt"); Path newFile = tempDirectory.resolve(newFilename); Files.createFile(newFile); assertTrue("File was not created", Files.exists(newFile)); key = null; // We will give it POLL_TIMEOUT_SECONDS seconds, if it hasn't got something by // then we assume failure key = watchService.poll(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS); assertNotNull("Polling WatchService object returned null", key); boolean eventFound = false; // Check for exactly one event for (WatchEvent<?> event : key.pollEvents()) { if (event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)) { // Assert exactly one event assertFalse("Duplicate ENTRY_CREATE events delivered for one file creation", eventFound); // Assert filename is correct assertEquals(newFilename, (Path) event.context()); eventFound = true; } else { fail(event.kind() + " event retured, expected ENTRY_CREATE"); } } // Reset the key, to allow for future events if (key != null) { key.reset(); } } } finally { watchService.close(); } okForCleanup(); }
Example 19
Source File: DirectoryWatcherTest.java From openjdk-systemtest with Apache License 2.0 | 4 votes |
public void testEntryCreate() throws IOException, InterruptedException, StfException { Path tempDirectory = getTemporaryDirectory(); WatchService watchService = FileSystems.getDefault().newWatchService(); try { tempDirectory.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); Path newFilename = Paths.get("ENTRY_CREATE.txt"); Path newFile = tempDirectory.resolve(newFilename); Files.createFile(newFile); assertTrue("File was not created", Files.exists(newFile)); WatchKey key = null; // We will give it POLL_TIMEOUT_SECONDS seconds, if it hasn't got something by // then we assume failure key = watchService.poll(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS); assertNotNull("Polling WatchService object returned null", key); boolean eventFound = false; // Check for exactly one event for (WatchEvent<?> event : key.pollEvents()) { if (event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)) { // Assert exactly one event assertFalse("Duplicate ENTRY_CREATE events delivered for one file creation", eventFound); // Assert filename is correct assertEquals(newFilename, (Path) event.context()); eventFound = true; } else { fail(event.kind() + " event retured, expected ENTRY_CREATE"); } } // Reset the key, to allow for future events key.reset(); } finally { watchService.close(); } okForCleanup(); }