Example usage for java.nio.file WatchService poll

List of usage examples for java.nio.file WatchService poll

Introduction

In this page you can find the example usage for java.nio.file WatchService poll.

Prototype

WatchKey poll(long timeout, TimeUnit unit) throws InterruptedException;

Source Link

Document

Retrieves and removes the next watch key, waiting if necessary up to the specified wait time if none are yet present.

Usage

From source file:cz.muni.fi.pb138.cvmanager.service.PDFgenerator.java

/**
 * By external calling pdflatex function of laTex generates pdf curriculum vitae document from .tex file
 * @param username name of user whose is the CV
 * @return language to export (sk/en)/*from ww w .  ja  va  2 s. com*/
 * @throws IOException
 * @throws InterruptedException
 * @throws NullPointerException
 */
public InputStream latexToPdf(String username) throws IOException, InterruptedException, NullPointerException {
    ProcessBuilder pb = new ProcessBuilder("pdflatex", username + "_cv.tex", "--output-directory=");
    File file = new File("cvxml/");
    pb.directory(file);
    Process p = pb.start();

    WatchService watcher = FileSystems.getDefault().newWatchService();
    Path dir = Paths.get("cvxml/");
    dir.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);

    while (true) {
        // wait for a key to be available for 10 seconds
        WatchKey key = watcher.poll(10000L, TimeUnit.MILLISECONDS);
        if (key == null) {
            break;
        }

        for (WatchEvent<?> event : key.pollEvents()) {
            // get event type
            WatchEvent.Kind<?> kind = event.kind();

            // get file name
            @SuppressWarnings("unchecked")
            WatchEvent<Path> ev = (WatchEvent<Path>) event;
            Path fileName = ev.context();

            System.out.println(kind.name() + ": " + fileName);
        }

        boolean valid = key.reset();
        if (!valid) {
            break;
        }
    }

    System.out.println("end of cycle");

    File pdf = new File("cvxml/" + username + "_cv.pdf");

    return new FileInputStream(pdf);
}

From source file:org.springframework.cloud.gcp.stream.binder.pubsub.PubSubEmulator.java

/**
 * Wait until a PubSub emulator configuration file is updated.
 * Fail if the file does not update after 1 second.
 * @param watchService the watch-service to poll
 * @throws InterruptedException which should interrupt the peaceful slumber and bubble up
 * to fail the test.//from   w w w . j a v a 2s  . co m
 */
private void updateConfig(WatchService watchService) throws InterruptedException {
    int attempts = 10;
    while (--attempts >= 0) {
        WatchKey key = watchService.poll(100, TimeUnit.MILLISECONDS);

        if (key != null) {
            Optional<Path> configFilePath = key.pollEvents().stream().map((event) -> (Path) event.context())
                    .filter((path) -> ENV_FILE_NAME.equals(path.toString())).findAny();
            if (configFilePath.isPresent()) {
                return;
            }
        }
    }

    fail("Configuration file update could not be detected");
}