在 MongoDB Java Driver 中设置聚合管道执行超时的行为是什么?

What is the behavior of setting timeout in aggregation pipeline execution in MongoDB Java Driver?

我需要为聚合管道执行设置超时。我正在使用 MongoDB Java 驱动程序 3.2。我知道我必须使用的代码如下:

collection.aggregate(pipeline).maxTime(10, TimeUnit.SECONDS);

问题是一旦达到超时,我无法在任何地方找到程序的行为。它是否抛出异常?它会静默终止并返回 null 结果吗?

官方 MongoDB 文档什么也没说(参见 cursor.maxTimeMS()). Also the Java API does not refer to any particular behavior (see maxTime)。

怎么可能?!

好的,我知道了。如果聚合管道的执行超过通过方法 maxTime 表示的时间,则抛出 com.mongodb.MongoExecutionTimeoutException

异常的堆栈跟踪如下:

com.mongodb.MongoExecutionTimeoutException: operation exceeded time limit

    at com.mongodb.connection.ProtocolHelper.createSpecialException(ProtocolHelper.java:157)
    at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:111)
    at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:114)
    at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
    at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)
    at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:173)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:215)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:206)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:112)
    at com.mongodb.operation.AggregateOperation.call(AggregateOperation.java:227)
    at com.mongodb.operation.AggregateOperation.call(AggregateOperation.java:223)
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:239)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212)
    at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:223)
    at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:65)
    at com.mongodb.Mongo.execute(Mongo.java:772)
    at com.mongodb.Mongo.execute(Mongo.java:759)

希望对您有所帮助。