在mongodb中取消异步FindIterable

时间:2018-03-09 17:10:48

标签: java mongodb project-reactor

我在mongodb中有一个返回大量结果的查询。我将FindIterable封装到一个反应式Flowable中,并使用spring-boot-webflux流式传输结果。

当用户断开连接时,我在Flowable中收到了取消事件但是,我找不到停止mongo迭代的方法。

我像这样创建Flowable:

private static Flowable toFlowable(FindIterable cursor){

    return Flowable.create(emitter -> {

        cursor
            .forEach(new Block<Document>() {
                @Override
                public void apply(Document doc) {
                    log.debug("Next Block :" + doc.toJson());
                    emitter.onNext(doc);                    
                }
            } , new SingleResultCallback<Void>() {
                @Override
                public void onResult(Void result, Throwable t) {
                    log.debug("Completed");
                    emitter.onComplete();
                }
            });         
    }, BackpressureStrategy.BUFFER);
}

我以这种方式创建查询:

public Flowable<SomeType> find(String value){

    Bson filter = Filters.eq("key", value);
    FindIterable<Document> iterable =  collection.find(filter);

    return toFlowable(iterable)
            .map(doc -> adapter.convert(doc))
            .doOnCancel(() -> {
                log.info("Cancel Flowable");
                **--> iterable.stop????**
            });

我正在使用'org.mongodb:mongodb-driver-async:3.6.3'。

有没有办法在mongo中停止异步迭代?

0 个答案:

没有答案