Example usage for java.nio.file WatchKey equals

List of usage examples for java.nio.file WatchKey equals

Introduction

In this page you can find the example usage for java.nio.file WatchKey equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:acromusashi.stream.ml.common.spout.WatchTextBatchSpout.java

/**
 * ???????????????????//from w w w  .  jav  a 2s.c  o m
 * 
 * @param collector Collector
 * @throws IOException 
 * @throws InterruptedException ?
 */
@SuppressWarnings({ "rawtypes" })
protected void checkDataFile(TridentCollector collector) throws IOException, InterruptedException {
    // ?????????????????
    if (this.isInitialReaded == false) {
        List<String> fileContents = FileUtils.readLines(this.targetFile);
        emitTuples(fileContents, collector);
        this.isInitialReaded = true;

        // 
        Path dirPath = new File(this.dataFileDir).toPath();
        FileSystem fileSystem = dirPath.getFileSystem();
        this.watcherService = fileSystem.newWatchService();
        this.watchKey = dirPath.register(this.watcherService,
                new Kind[] { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY });

        return;
    }

    // ????????
    WatchKey detectedKey = this.watcherService.poll(1, TimeUnit.SECONDS);

    // ???????????????????????
    if (detectedKey == null || detectedKey.equals(this.watchKey) == false) {
        return;
    }

    try {
        // ???????????????
        for (WatchEvent event : detectedKey.pollEvents()) {

            Path filePath = (Path) event.context();

            // ?????????????
            if (filePath == null
                    || this.targetFile.toPath().getFileName().equals(filePath.getFileName()) == false) {
                continue;
            }

            List<String> fileContents = FileUtils.readLines(this.targetFile);
            emitTuples(fileContents, collector);
        }
    } finally {
        detectedKey.reset();
    }
}