我创建了一个应用程序,它读取 mongo 更改流以进行更新和插入,然后我们对更改的数据采取行动。下面是我的代码片段
private void listenChangeStream() {
Runnable changeListener = new Runnable() {
@Override
public void run() {
String fullDoc = null;
String updateInfo = null;
while (cursor.hasNext()) {
try {
ChangeStreamDocument<Document> next = cursor.next();
String id = next.getDocumentKey().getString("id").getValue();
LOGGER.debug("Change Stream recived:{}", next);
String operationType = next.getOperationType().getValue();
if ("insert".equals(operationType) || "replace".equals(operationType)) {
fullDoc = next.getFullDocument().toString();
if (fullDoc.contains("image_info")) {
kafkaProducer
.pushOfflineProcessingData(new DataPackets(Id, OfflineProcessType.IMAGE));
}
} else if ("update".equals(operationType)) {
updateInfo = next.getUpdateDescription().toString();
if (updateInfo.contains("image_info"))
kafkaProducer
.pushOfflineProcessingData(new DataPackets(Id, OfflineProcessType.IMAGE));
}
} catch (Exception ex) {
LOGGER.info("Exception has come in cahnge listener::", ex);
}
}
}
};
executor = Executors.newFixedThreadPool(1);
executor.execute(changeListener);
}
private MongoCursor<ChangeStreamDocument<Document>> getCursor(MongoCollection<Document> supplierCollection, List<Bson> pipeline) {
MongoCursor<ChangeStreamDocument<Document>> cursor;
cursor = supplierCollection.watch(pipeline).iterator();
return cursor;
}
这工作正常。我面临的问题是,当我启动服务器时,更改流开始读取旧提交的更改。我不想要。我希望在部署之后只选择新的更新。
任何人都可以建议如何做到这一点?