简体   繁体   English

Java Async MongoDB驱动程序和RxJava2 Observables

[英]Java Async MongoDB driver and RxJava2 Observables

I'm studying reactive programming with RxJava2 and I have a question about its usage with an async database driver like MongoDB. 我正在研究RxJava2的反应式编程,并且对它在MongoDB等异步数据库驱动程序中的用法有疑问。

If I use blocking MongoDB driver to get a collection the approach would be this: 如果我使用阻止MongoDB驱动程序来获取集合,则方法如下:

public class MyDao{
   ...
   public Document getFirstDocument(String collectionName){
      MongoCollection<Document> collection = database.getCollection(collectionName);
      return collection.find().first();
   }
}



public class MyService {
   ...
   public Observable<Document> getFirstOf(String collectionName){
       return Observable.just(myDao.getFirstDocument(collectionName)); 
   }
}

Instead, working with the async Driver of MongoDB, my return type for a read operation is a void (and not a Document, or a Future)with a callback method inside, for example: 相反,使用MongoDB的异步驱动程序,我的读取操作返回类型为void(而不是Document或Future),其内部带有回调方法,例如:

collection.find().first(
        (document, throwable) -> {
            myService.myCallback(document);
        }
);

So, how can I pass my Observable Documents to MyService? 因此,如何将可观察文档传递给MyService?

public class MyDao{
   ...
   public void getFirstDocument(String collectionName){
      MongoCollection<Document> collection = database.getCollection(collectionName);
      collection.find().first(
        (document, throwable) -> {
            //SOME SORT OF CALLBACK
        }
     );
   }
}



public class MyService {
   ...
   public Observable<Document> getFirstOf(String collectionName){
       return ??????? 
   }
}

When you are using Observable.just() in 当您在使用Observable.just()

public Observable<Document> getFirstOf(String collectionName){
    return Observable.just(myDao.getFirstDocument(collectionName)); 
}

it equals to next code 它等于下一个代码

public Observable<Document> getFirstOf(String collectionName){
    Document doc = myDao.getFirstDocument(collectionName);
    return Observable.just(doc); 
}

You can notice that it's not async code and request to DB is performed on calling thread. 您会注意到它不是async代码,并且对DB的请求是在调用线程上执行的。 To make that code async you need to rewrite it like that 为了使代码async您需要像这样重写它

public Observable<Document> getFirstOf(String collectionName){
    return Observable.fromCallable(() -> myDao.getFirstDocument(collectionName)); 
}

If you are using async MongoDB driver and would like to wrap it in Observable , you can write in that way 如果您使用的是async MongoDB驱动程序,并且希望将其包装在Observable ,则可以用这种方式编写

public Observable<Document> getFirstDocument(String collectionName) {
    return Observable.create(emitter -> {
        MongoCollection<Document> collection = database.getCollection(collectionName);
        collection.find().first((document, throwable) -> {
            if(document != null) {
                emitter.onNext(document);
                emitter.onComplete();
            } else if(throwable != null) {
                emitter.onError(throwable);
            }
        });
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM