【问题标题】:Stop watching a MongoDB change stream with the Java driver停止使用 Java 驱动程序查看 MongoDB 更改流
【发布时间】:2019-03-20 01:44:42
【问题描述】:

MongoDB Java 驱动程序清楚地解释了如何观察一个集合并启动/打开一个更改流,这是一个 ChangeStreamIterable:

http://mongodb.github.io/mongo-java-driver/3.9/driver/tutorials/change-streams/

MongoClient mongoClient = MongoClients.create(new ConnectionString("mongodb://localhost:27017,localhost:27018,localhost:27019"));
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("restaurants");

collection.watch().forEach(printBlock);

但是没有关于关闭更改流的解释,API似乎也不支持关闭操作。

为什么不可能?这不是必要的,还是只是疏忽?

适用于所有版本的标准、异步和响应式驱动程序。

【问题讨论】:

  • 嗨菲利普,你找到解决这个问题的方法了吗?我也有同样的情况。
  • 嗨@sixro,你只需要关闭数据库连接。

标签: java mongodb


【解决方案1】:

您可以从 ChangeStreamIterable 中获得一个可关闭的光标。

MongoCursor<ChangeStreamDocument<Document>> cursor = collection.watch().iterator();
cursor.close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    相关资源
    最近更新 更多