[英]Java Async MongoDB driver and RxJava2 Observables
我正在研究RxJava2的反应式编程,并且对它在MongoDB等异步数据库驱动程序中的用法有疑问。
如果我使用阻止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));
}
}
相反,使用MongoDB的异步驱动程序,我的读取操作返回类型为void(而不是Document或Future),其内部带有回调方法,例如:
collection.find().first(
(document, throwable) -> {
myService.myCallback(document);
}
);
因此,如何将可观察文档传递给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 ???????
}
}
当您在使用Observable.just()
public Observable<Document> getFirstOf(String collectionName){
return Observable.just(myDao.getFirstDocument(collectionName));
}
它等于下一个代码
public Observable<Document> getFirstOf(String collectionName){
Document doc = myDao.getFirstDocument(collectionName);
return Observable.just(doc);
}
您会注意到它不是async
代码,并且对DB的请求是在调用线程上执行的。 为了使代码async
您需要像这样重写它
public Observable<Document> getFirstOf(String collectionName){
return Observable.fromCallable(() -> myDao.getFirstDocument(collectionName));
}
如果您使用的是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.