运行以下代码时,永远不会执行最终订阅者。为什么? 我正在使用rxJava 2.1.0;我怀疑有一些猴子业务与线程,但努力想出这个。
我的实际目标是将具有相同id值字段的所有Struct实例组合(减少)到单个Reduced实例中,然后将其向下泵送到管道中。
@Test
public void experiment2() throws InterruptedException {
PublishSubject<Struct> publishSubject = PublishSubject.create();
publishSubject.buffer(3000, TimeUnit.MILLISECONDS, 10000)
.flatMap(Observable::fromIterable)
.groupBy(Struct::getId)
.flatMap((GroupedObservable<String, Struct> t) -> {
return t.reduce(Optional.<Reduced>empty(), (Optional<Reduced> accumulator, Struct str) -> {
if (accumulator.isPresent()) {
accumulator.get().all.add(str.patternId);
return accumulator;
} else {
Reduced fresh = new Reduced();
fresh.all.add(str.patternId);
return Optional.of(fresh);
}
}).toObservable();
})
.filter(Optional::isPresent)
.map(Optional::get)
.subscribe((Reduced t) -> {
//WHY IS THIS NEVER REACHED???
System.out.println(t);
});
publishSubject.onNext(new Struct("sig1", "A"));
publishSubject.onNext(new Struct("sig1", "B"));
Thread.sleep(60000L);
}
public static class Reduced {
private final Set<String> all = ConcurrentHashMap.newKeySet();
@Override
public String toString() {
return all.toString();
}
}
public static class Struct {
private final String id;
private final String patternId;
public Struct(String id, String patternId) {
this.id = id;
this.patternId = patternId;
}
public String getId() {
return id;
}
public String getPatternId() {
return patternId;
}
}