似乎手表没有立即动作,以下代码仅在睡眠后输出插入。我怎样才能等到更改流连接?
public class Mongodb4Test {
public static void main(String[] args) {
MongoCollection<Document> col = XXX;
ChangeStreamIterable<Document> watch = col.watch();
new Thread(() -> {
col.insertOne(new Document("key", "val1"));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
col.insertOne(new Document("key", "val2"));
}).start();
for (ChangeStreamDocument<Document> change : watch) {
if (change.getUpdateDescription() != null)
System.out.println(change.getUpdateDescription().getUpdatedFields());
System.out.println(change.getOperationType());
System.out.println(change.getFullDocument());
}
}
}