rxjava2版本2.1.5
尝试理解RxJava2对observable的多个订阅。 有一个简单的文件监视服务,跟踪创建,修改,删除目录中的文件。 我添加了2个订阅者,并期望在两个订阅者上打印事件。 当我将文件复制到监视目录时,我看到一个订阅者打印出该事件。然后,当我删除文件时,我看到第二个订阅者打印出事件。 我期待两个订阅者都打印事件。我在这里缺少什么?
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 java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.concurrent.TimeUnit;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;
public class MyRxJava2DirWatcher {
public Flowable<WatchEvent<?>> createFlowable(WatchService watcher, Path path) {
return Flowable.create(subscriber -> {
boolean error = false;
WatchKey key;
try {
key = path.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
}
catch (IOException e) {
subscriber.onError(e);
error = true;
}
while (!error) {
key = watcher.take();
for (final WatchEvent<?> event : key.pollEvents()) {
subscriber.onNext(event);
}
key.reset();
}
}, BackpressureStrategy.BUFFER);
}
public static void main(String[] args) throws IOException, InterruptedException {
Path path = Paths.get("c:\\temp\\delete");
final FileSystem fileSystem = path.getFileSystem();
WatchService watcher = fileSystem.newWatchService();
MyRxJava2DirWatcher my = new MyRxJava2DirWatcher();
my.createFlowable(watcher, path).subscribeOn(Schedulers.computation()).subscribe(event -> {
System.out.println("1>>Event kind:" + event.kind() + ". File affected: " + event.context() + ". "
+ Thread.currentThread().getName());
}, onError -> {
System.out.println("1>>" + Thread.currentThread().getName());
onError.printStackTrace();
});
// MyRxJava2DirWatcher my2 = new MyRxJava2DirWatcher();
my.createFlowable(watcher, path).subscribeOn(Schedulers.computation()).subscribe(event -> {
System.out.println("2>>Event kind:" + event.kind() + ". File affected: " + event.context() + ". "
+ Thread.currentThread().getName());
}, onError -> {
System.out.println("2>>" + Thread.currentThread().getName());
onError.printStackTrace();
});
TimeUnit.MINUTES.sleep(1000);
}
}
输出如下所示
2>>Event kind:ENTRY_CREATE. File affected: 1.txt. RxCachedThreadScheduler-2
2>>Event kind:ENTRY_MODIFY. File affected: 1.txt. RxCachedThreadScheduler-2
1>>Event kind:ENTRY_DELETE. File affected: 1.txt. RxCachedThreadScheduler-1
答案 0 :(得分:3)
您会在两个WatchService
之间共享相同的Flowable
,并在其中竞争事件。如果您传入FileSystem
而是在newWatchService()
中致电Flowable.create
,则应该收到所有事件Subscriber
的次数:
public Flowable<WatchEvent<?>> createFlowable(FileSystem fs, Path path) {
return Flowable.create(subscriber -> {
WatchService watcher = fs.newWatchService();
subscriber.setCancellable(() -> watcher.close());
boolean error = false;
WatchKey key;
try {
key = path.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
}
catch (IOException e) {
subscriber.onError(e);
error = true;
}
while (!error) {
key = watcher.take();
for (final WatchEvent<?> event : key.pollEvents()) {
subscriber.onNext(event);
}
key.reset();
}
}, BackpressureStrategy.BUFFER);
}
另请注意,您应该使用subscribeOn(Schedulers.computation(), false)
来避免poll
与Subscriber
一起陷入僵局。
答案 1 :(得分:0)
您正在为两个不同的订阅者创建两个不同的Flowable。是否有一个Flowable订阅了两次,如下所示。
public static void main(String[] args) throws IOException, InterruptedException {
Path path = Paths.get("c:\\temp\\delete");
final FileSystem fileSystem = path.getFileSystem();
WatchService watcher = fileSystem.newWatchService();
MyRxJava2DirWatcher my = new MyRxJava2DirWatcher();
Flowable myFlowable = my.createFlowable(watcher, path);
myFlowable.subscribeOn(Schedulers.computation()).subscribe(event -> {
System.out.println("1>>Event kind:" + event.kind() + ". File affected: " + event.context() + ". "
+ Thread.currentThread().getName());
}, onError -> {
System.out.println("1>>" + Thread.currentThread().getName());
onError.printStackTrace();
});
myFlowable.subscribeOn(Schedulers.computation()).subscribe(event -> {
System.out.println("2>>Event kind:" + event.kind() + ". File affected: " + event.context() + ". "
+ Thread.currentThread().getName());
}, onError -> {
System.out.println("2>>" + Thread.currentThread().getName());
onError.printStackTrace();
});
TimeUnit.MINUTES.sleep(1000);
}
}