@Test void testListenToPath() throws IOException { final Path tempFolder = Files.createTempDirectory("configurate-test"); final Path testFile = tempFolder.resolve("listenPath.txt"); Files.write(testFile, Collections.singleton("version one"), StandardOpenOption.SYNC, StandardOpenOption.CREATE); final AtomicBoolean notified = new AtomicBoolean(); final AtomicInteger callCount = new AtomicInteger(0); final Object condition = new Object(); final AtomicReference<Disposable> disposer = new AtomicReference<>(); disposer.set(listener.listenToFile(testFile, event -> { synchronized (condition) { final int oldVal = callCount.getAndIncrement(); if (oldVal > 1) { disposer.get().dispose(); return; } notified.set(true); condition.notify(); if (oldVal >= 1) { disposer.get().dispose(); } } })); assertEquals(0, callCount.get()); assertTimeout(Duration.ofMinutes(1), () -> { synchronized (condition) { Files.write(testFile, Collections.singleton("version two"), StandardOpenOption.SYNC); while (!notified.get()) { condition.wait(); } notified.set(false); assertEquals(1, callCount.get()); Files.write(testFile, Collections.singleton("version three"), StandardOpenOption.SYNC); while (!notified.get()) { condition.wait(); } notified.set(false); assertEquals(2, callCount.get()); } Files.write(testFile, Collections.singleton("version four"), StandardOpenOption.SYNC); assertEquals(2, callCount.get()); }); }
public Disposable listenToFile(Path file, final Subscriber<WatchEvent<?>> callback) throws IOException, IllegalArgumentException { file = file.toAbsolutePath(); if (Files.isDirectory(file)) { throw new IllegalArgumentException("Path " + file + " must be a file"); } final Path fileName = file.getFileName(); return getRegistration(file.getParent()).subscribe(fileName, callback); }
WatchServiceListener implements AutoCloseable { public Disposable listenToFile(Path file, final Subscriber<WatchEvent<?>> callback) throws IOException, IllegalArgumentException { file = file.toAbsolutePath(); if (Files.isDirectory(file)) { throw new IllegalArgumentException("Path " + file + " must be a file"); } final Path fileName = file.getFileName(); return getRegistration(file.getParent()).subscribe(fileName, callback); } }
WatchServiceListener implements AutoCloseable { public Disposable listenToFile(Path file, final Subscriber<WatchEvent<?>> callback) throws IOException, IllegalArgumentException { file = file.toAbsolutePath(); if (Files.isDirectory(file)) { throw new IllegalArgumentException("Path " + file + " must be a file"); } final Path fileName = file.getFileName(); return getRegistration(file.getParent()).subscribe(fileName, callback); } private  WatchServiceListener(final ThreadFactory factory, final FileSystem fileSystem, final Executor taskExecutor); }
WatchServiceListener implements AutoCloseable { public Disposable listenToFile(Path file, final Subscriber<WatchEvent<?>> callback) throws IOException, IllegalArgumentException { file = file.toAbsolutePath(); if (Files.isDirectory(file)) { throw new IllegalArgumentException("Path " + file + " must be a file"); } final Path fileName = file.getFileName(); return getRegistration(file.getParent()).subscribe(fileName, callback); } private  WatchServiceListener(final ThreadFactory factory, final FileSystem fileSystem, final Executor taskExecutor); static Builder builder(); static WatchServiceListener create(); Disposable listenToFile(Path file, final Subscriber<WatchEvent<?>> callback); Disposable listenToDirectory(Path directory, final Subscriber<WatchEvent<?>> callback); ConfigurationReference<N> listenToConfiguration(final Function<Path, ConfigurationLoader<? extends N>> loaderFunc, final Path path); @Override void close(); }
WatchServiceListener implements AutoCloseable { public Disposable listenToFile(Path file, final Subscriber<WatchEvent<?>> callback) throws IOException, IllegalArgumentException { file = file.toAbsolutePath(); if (Files.isDirectory(file)) { throw new IllegalArgumentException("Path " + file + " must be a file"); } final Path fileName = file.getFileName(); return getRegistration(file.getParent()).subscribe(fileName, callback); } private  WatchServiceListener(final ThreadFactory factory, final FileSystem fileSystem, final Executor taskExecutor); static Builder builder(); static WatchServiceListener create(); Disposable listenToFile(Path file, final Subscriber<WatchEvent<?>> callback); Disposable listenToDirectory(Path directory, final Subscriber<WatchEvent<?>> callback); ConfigurationReference<N> listenToConfiguration(final Function<Path, ConfigurationLoader<? extends N>> loaderFunc, final Path path); @Override void close();  }