我正在尝试构建一种算法,以使用RxJava探索树。
我使用RxJava的原因是我的节点处理功能已经在使用RxJava将RPC发送到其他服务。请注意,浏览树的不同分支的顺序并不重要。
理想情况下,我希望有一个类似QueueFlowable<Node> extends Flowable<Node>
的东西,它将公开一个push
函数,Observers
可用于在处理新节点后在队列中添加新节点。
private static main(String[] args) {
QueueFlowable<Node> nodes = new QueueFlowable<>();
nodes.push(/* the root */);
nodes.concatMap(node -> process(node)).map(nodes::push));
nodes.blockingSubscribe();
}
// Processes the node and returns a Flowable of other nodes to process.
private static Flowable<Node> process(Node node) { ... }
这听起来像是相对普遍的事情(因为我希望网络爬虫能够实现类似的功能),但我仍然无法使其正常工作。
我最近的尝试是使用PublishProcessor
如下:
PublishProcessor<Mode> nodes = PublishProcessor.create();
nodes.concatMap(node -> process(node)).subscribe(nodes);
nodes.onNext(/* the root node */);
nodes.blockingSubscribe();
当然,这不会因处理器未完成而终止。
任何帮助将不胜感激!