mongodb反应流中的订阅者实例

我遇到过mongodb反应流驱动程序,它们对于异步操作似乎非常有用。此外,对于我们执行的每个操作,我们都需要为此指定一个订阅者。我的疑问是,是否应该为我们执行的每个操作创建不同的订户实例。例如,请考虑使用mongodb docs

中的此代码段
// 1. Ordered bulk operation - order is guaranteed
subscriber = new PrintSubscriber<BulkWriteResult>("Bulk write results: %s");
collection.bulkWrite(
  Arrays.asList(new InsertOneModel<>(new Document("_id",4)),new InsertOneModel<>(new Document("_id",5)),6)),new UpdateoneModel<>(new Document("_id",1),new Document("$set",new Document("x",2))),new DeleteoneModel<>(new Document("_id",2)),new ReplaceoneModel<>(new Document("_id",3),new Document("_id",3).append("x",4)))
  ).subscribe(subscriber);
subscriber.await();

在此,它仅执行一些批量写入操作。如果我在这样的循环中对批处理执行这些操作

while(someresultset.hasnext()) {
 list.add(someresultset.getNext())

 if(list.size() >= 10000)
   doWrites() // can I use same subscriber instance declared outside of this loop or I should create the subscriber instance every time?

 list = new list()
}
iCMS 回答:mongodb反应流中的订阅者实例

我的查询是我们是否应该为我们执行的每个操作创建不同的订阅者实例

是的,您每次订阅都必须创建一个不同的订阅者实例。您正在订阅反应式流发布者which states

Subscriber只能订阅一个Publisher

本文链接:https://www.f2er.com/2289691.html

大家都在问