Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fixed] Not deposed error, and upgrade to rx2, add gradle build script. #7

Merged
merged 5 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
target
*.iml
.idea
.idea
/out
/build
/.gradle*
/gradle*
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# Reactive Wrapper for Java8 WatchService

**RxFileWatcher** allows you to observe directories (recursively or not) for file system events with a [RxJava](https://github.com/ReactiveX/RxJava) observable. It is based on the [JDK WatchService](https://docs.oracle.com/javase/8/docs/api/java/nio/file/WatchService.html), but it is much more convenient.

Usage
-------
# What's New?

1. `Sep 12, 2018` Uploaded to Rx2, fix some bugs.
2. `Jul 18, 2015` Initial project, first release by [Helmbold](https://github.com/helmbold).


# Usage

The following example creates an observable that watches the given directory and all its subdirectories for file system events. Directories which are created later are watched, too. Each event will be emitted as a [WatchEvent](https://docs.oracle.com/javase/8/docs/api/java/nio/file/WatchEvent.html).

Expand All @@ -10,7 +17,7 @@ PathObservables
.watchRecursive(Paths.get("some/directory/"))
.subscribe(event -> System.out.println(event));
```

To watch only the top-level directory, you call `watchNonRecursive` instead of `watchRecursive`:

```java
Expand All @@ -22,3 +29,5 @@ PathObservables
That's it!

See [RxJava Documentation](https://github.com/ReactiveX/RxJava/wiki) for more information, e. g. how you can filter certain types of events.

`- eof -`
23 changes: 23 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apply plugin: 'java-library'
apply plugin: 'maven'

group = 'de.helmbold'
description = """fileConvenient file watcher with an RxJava interface, based on JDK WatchService watcher"""

sourceCompatibility = 1.8
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
mavenCentral()
maven { url "http://repo1.maven.org/maven2/" }
}

dependencies {
compile 'io.reactivex.rxjava2:rxjava:2.2.2'
testCompile group: 'org.testng', name: 'testng', version: '6.9.4'
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.0.0'
testCompile group: 'commons-io', name: 'commons-io', version: '2.4'
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'rx-filewatcher'
31 changes: 21 additions & 10 deletions src/main/java/de/helmbold/rxfilewatcher/PathObservables.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package de.helmbold.rxfilewatcher;

import rx.Observable;
import rx.Subscriber;

import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.FileSystem;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;

import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.nio.file.StandardWatchEventKinds.*;

public final class PathObservables {

private PathObservables() {
Expand Down Expand Up @@ -68,12 +77,14 @@ private Observable<WatchEvent<?>> create() {
subscriber.onError(exception);
errorFree = false;
}
while (errorFree) {
while (errorFree && !subscriber.isDisposed()) {
final WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException exception) {
subscriber.onError(exception);
if (!subscriber.isDisposed()) {
subscriber.onError(exception);
}
errorFree = false;
break;
}
Expand All @@ -93,7 +104,7 @@ private Observable<WatchEvent<?>> create() {
}
}
if (errorFree) {
subscriber.onCompleted();
subscriber.onComplete();
}
});
}
Expand All @@ -119,7 +130,7 @@ private void register(final Path dir) throws IOException {

// register newly created directory to watching in recursive mode
private void registerNewDirectory(
final Subscriber<? super WatchEvent<?>> subscriber,
final ObservableEmitter<WatchEvent<?>> subscriber,
final Path dir,
final WatchEvent<?> event) {
final Kind<?> kind = event.kind();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.helmbold.rxfilewatcher;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import org.apache.commons.io.FileUtils;
import org.testng.annotations.Test;
import rx.Observable;
import rx.schedulers.Schedulers;

import java.io.IOException;
import java.nio.file.Files;
Expand Down