gpt4 book ai didi

mongodb - 使用 mongodb java 驱动程序,maxAwaitTime 无法处理更改流

转载 作者:可可西里 更新时间:2023-11-01 10:10:11 25 4
gpt4 key购买 nike

我使用的是 Java mongodb 驱动程序版本 3.8.0 和 mongodb 3.6.3。

我用这个在集合上创建了一个 watch :

MongoCursor<ChangeStreamDocument<Document>> cursor = collection.watch().maxAwaitTime(500, TimeUnit.MILLISECONDS).iterator();

文档 here关于 maxAwaitTime 的状态:

The maximum amount of time for the server to wait on new documents to satisfy a change stream query.

但是,我看到的是 cursor.hasNext() 仅在集合发生变化时返回,而不是在传递给 maxAwaitTime 的时间过去后返回。

当我打开 mongodb 的详细日志记录时,我看到在 getMore 命令中按预期设置了 maxWaitTime。

如何在没有变化时让我的 watch 超时?似乎 maxAwaitTime 应该这样做。为什么它不起作用?

最佳答案

MongoDB 驱动程序将更改流实现为 TAILABLE_AWAIT 游标的抽象,maxAwaitTimeMS 用于指定 getMore 的时间限制TAILABLE_AWAIT 游标上的命令。

它的工作方式,MongoCursor继续在循环中向服务器发送 getMore 命令,直到:

  • 找到一份文件
  • 游标关闭
  • 发生异常

只有当上述任一事件发生时,游标的方法next()hasNext()将返回。

虽然上述事件均未发生,但服务器的 getMore 命令将继续由 Iterator 接口(interface)调用。 maxAwaitTime 指定等待多长时间后 getMore 命令在等待文档时超时并返回,即使没有找到文档也是如此。

How do I cause my watch to timeout when there are no changes?

如果您的应用程序在 maxAwaitTime 之后需要超时,mongo-java-driver提供 tryNext() MongoCursor 上的方法。如果没有找到文档,此方法将在 maxAwaitTime 后返回 null,并且可以由应用程序重复调用。另见 JAVA-2965 .

关于mongodb - 使用 mongodb java 驱动程序,maxAwaitTime 无法处理更改流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52239996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com