mongodb 个反应流中的订阅者实例

Subscriber instances in mongodb reactive streams

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

中的这个片段
// 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)),
                new InsertOneModel<>(new Document("_id", 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()
}

My query is whether we should create a different subscriber instance for every operation we do

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

A Subscriber should only subscribe once to a single Publisher.