|
1 | 1 | # java-watch |
2 | | -a java file watcher that works across platforms and supports recursion and single file watches |
| 2 | +a java file watcher that works across platforms and supports recursion, single file watches, and tries to make sure no events are missed. |
3 | 3 |
|
| 4 | +## Features |
| 5 | + |
| 6 | +Currently working features in java-watch: |
| 7 | + |
| 8 | +- Recursive watches, even if platform doesn't support it natively. |
| 9 | +- Recursive watches also work inside directories created after the watch started |
| 10 | +- On overflow events no **new** directories (and it's recursive files) are missed, modification events will however not be simulated |
| 11 | +- Single file watches |
| 12 | +- Multiple watches for the same directory are merged to avoid overloading the kernel |
| 13 | +- Events are process on a worker pool, which you can customize. |
| 14 | + |
| 15 | +Future features: |
| 16 | + |
| 17 | +- Avoid poll based watcher in macOS/OSX that only detects changes every 2 seconds |
| 18 | +- Support file watches natively in linux |
| 19 | +- Monitor only specific events (such as only CREATES) |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +Import dependency in pom.xml: |
| 24 | + |
| 25 | +```xml |
| 26 | +<dependency> |
| 27 | + <groupId>engineering.swat</groupId> |
| 28 | + <artifactId>java-watch</artifactId> |
| 29 | + <version>${java-watch-version}</version> |
| 30 | +</dependency> |
| 31 | +``` |
| 32 | + |
| 33 | +Start using java-watch: |
| 34 | + |
| 35 | +```java |
| 36 | +var directory = Path.of("tmp", "test-dir"); |
| 37 | +var watcherSetup = Watcher.watch(directory, WatchScope.PATH_AND_CHILDREN) |
| 38 | + .withExecutor(Executors.newCachedThreadPool()) // optionally configure a custom thread pool |
| 39 | + .onEvent(watchEvent -> { |
| 40 | + System.err.println(watchEvent); |
| 41 | + }); |
| 42 | + |
| 43 | +try(var active = watcherSetup.start()) { |
| 44 | + System.out.println("Monitoring files, press any key to stop"); |
| 45 | + System.in.read(); |
| 46 | +} |
| 47 | +// after active.close(), the watch is stopped and |
| 48 | +// no new events will be scheduled on the threadpool |
| 49 | +``` |
4 | 50 |
|
5 | 51 | ## Related work |
| 52 | + |
| 53 | +Before starting this library, we wanted to use existing libraries, but they all lacked proper support for recursive file watches or lacked configurability. This library now has a growing collection of tests and a small API that should allow for future improvements without breaking compatibility. |
| 54 | + |
| 55 | +The following section describes the related work research on the libraries and underlying limitations. |
| 56 | + |
6 | 57 | After reading the documentation of the following discussion on file system watches: |
7 | 58 |
|
8 | 59 | - [Paul Millr's nodejs chokidar](https://github.com/paulmillr/chokidar) |
|
0 commit comments