Skip to content

Cantara/PathWatcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Path Watcher

GitHub tag (latest SemVer) Build Status Project Status: Active – The project has reached a stable, usable state and is being actively developed. Known Vulnerabilities

Short doc PathWatcher is:

  • a singleton that currently is limited to listening to one watchDir only
  • it uses worker threads to and event based i/o to produce new file discoveries
  • discovered files are handled in a separate consumer thread that invokes call back events
  • callbacks are not thread-safe!
  • supports both Native and Poll based FileSystem. Polling is used to support MacOS/OSX (because NativeFileSystem support is not available in JDKs!)
  • By default:
    • PatchWatcher will determine if Native I/O is possible. If not, it will automatically go for PollBased
    • Native and Poll based mode comes with two different configurations
  • In test it writes to the directory target/watcher
  • To understand the polling feature, study the test cases for polling configuration and (!!) => FilePollEventsProducer and algo inFileDetermineCompletionWorker

Known limitations:

  • Does not support multiple watch dirs

Use

Default

PathWatcher pw = PathWatcher.getInstance();

pw.watch(watchDir);

pw.registerCreatedHandler(new CreatedHandler());
pw.registerModifiedHandler(new ModifiedHandler());
pw.registerRemovedHandler(new RemovedHandler());
pw.registerFileCompletelyCreatedHandler(new FileCompletelyCreatedHandler());

pw.start();
Thread.sleep(5000);
pw.stop();

Native FileSystem

PathWatcher pw = PathWatcher.getInstance();
pw.forceFileSystemScannerMode(PathWatchScanner.NATIVE_FILE_SYSTEM); // not necessary on Linux and Windows

pw.watch(watchDir);

pw.registerCreatedHandler(new CreatedHandler());
pw.registerModifiedHandler(new ModifiedHandler());
pw.registerRemovedHandler(new RemovedHandler());
pw.registerFileCompletelyCreatedHandler(new FileCompletelyCreatedHandler());
pw.setScanForExistingFilesAtStartup(true); // create createEvent and FileCompletelyCreated event for existing files at startup


pw.start();
Thread.sleep(5000);
pw.stop();

Poll FileSystem

PathWatcher pw = PathWatcher.getInstance();
pw.forceFileSystemScannerMode(PathWatchScanner.POLL_FILE_SYSTEM); // will always be the case on MacOS
pw.setPathScanInterval(1000);
pw.setThreadPollInterval(150);
pw.setWorkerMode(FileCompletionWorkerMode.TIMEOUT);
pw.setTimeoutOrRetryInterval(500);

pw.watch(watchDir);

pw.registerCreatedHandler(new CreatedHandler());
pw.registerModifiedHandler(new ModifiedHandler());
pw.registerRemovedHandler(new RemovedHandler());

pw.start();
Thread.sleep(5000);
pw.stop();

CreateHandler

public static class CreatedHandler implements FileWatchHandler {
    @Override
    public void invoke(FileWatchEvent event) {
        Path file = event.getFile();
        log.trace("OnCreatedFileAction - Received FileWatchEvent from Consumer: {}", file);
        try {
            if (Files.exists(file))
                Files.delete(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ModifyHandler

public static class ModifiedHandler implements FileWatchHandler {
    @Override
    public void invoke(FileWatchEvent event) {
        Path file = event.getFile();
        log.trace("OnModifiedFileAction - Received FileWatchEvent from Consumer: {}", file);
    }
}

RemoveHandler

public static class RemovedHandler implements FileWatchHandler {
    @Override
    public void invoke(FileWatchEvent event) {
        Path file = event.getFile();
        log.trace("OnRemovedFileHandler - Received FileWatchEvent from Consumer: {}", file);
    }
}

FileCompletelyCreatedHandler

public static class FileCompletelyCreatedHandler implements FileWatchHandler {
    @Override
    public void invoke(FileWatchEvent event) {
        Path file = event.getFile();
        log.trace("OnFileCompletelyCreatedFileAction - Received FileWatchEvent from Consumer: {}", file);
        try {
            if (Files.exists(file))
                Files.delete(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages