I'm using the Java mongodb driver version 3.8.0 and mongodb 3.6.3.
I created a watch on a collection with this:
MongoCursor<ChangeStreamDocument<Document>> cursor = collection.watch().maxAwaitTime(500, TimeUnit.MILLISECONDS).iterator();
The documentation here states about maxAwaitTime:
The maximum amount of time for the server to wait on new documents to satisfy a change stream query.
However, what I'm seeing is that cursor.hasNext() returns only if there is a change on the collection, never when the time passed to maxAwaitTime has elapsed.
When I turn on mongodb's verbose logging I see maxWaitTime set as expected in the getMore command.
How do I cause my watch to timeout when there are no changes? It seems as though maxAwaitTime should do this. Why doesn't it work?
what I’m seeing is that cursor.hasNext() returns only if there is a change on the collection, never when the time passed to maxAwaitTime has elapsed.
Hi Chris,
maxAwaitTimeMS is not to trigger a timeout on your change stream client cursor.
MongoDB drivers implement change stream as an abstraction of a TAILABLE_AWAIT cursor, and maxAwaitTimeMS is to specify a time limit for a getMore command on on TAILABLE_AWAIT cursor. The option is also called maxTimeMS option on getMore.
If maxAwaitTimeMS is set to a value, the server will wait for the maximum amount specified before returning on an empty getMore.
When I turn on mongodb’s verbose logging I see maxWaitTime set as expected in the getMore command.
You can test this by specifying a timeout of 30 seconds, and you’ll see the getMore being logged every 30 seconds. Compare this to a value lesser, or no value specified.
How do I cause my watch to timeout when there are no changes?
One idea is to create a separate thread for a timer. The timer resets whenever there’s an activity returned by change stream. If the timer reached a certain value an event is triggered.
Regards,
Wan.