Example usage for com.mongodb.client MongoCursor getServerCursor

List of usage examples for com.mongodb.client MongoCursor getServerCursor

Introduction

In this page you can find the example usage for com.mongodb.client MongoCursor getServerCursor.

Prototype

@Nullable
ServerCursor getServerCursor();

Source Link

Document

Returns the server cursor, which can be null if the no cursor was created or if the cursor has been exhausted or killed.

Usage

From source file:flipkart.mongo.replicator.node.ReplicationTask.java

License:Apache License

@Override
public void run() {
    String shardId = rsConfig.shardName;
    Node masterNode = rsConfig.getMasterNode().get();
    MongoClient client = MongoConnector.getMongoClient(Lists.newArrayList(masterNode));

    MongoDatabase database = client.getDatabase("local");
    lastCp = taskContext.checkPointHandler.getCheckPoint(shardId);

    logger.info(String.format("######## START REPLICATOR FOR MongoURI: %s. LastCheckpoint: %s #######",
            client.getAddress(), lastCp));
    MongoCollection<Document> collection = database.getCollection("oplog.rs");
    FindIterable<Document> iterable;
    MongoCursor<Document> cursor;
    do {//from  w ww .ja  va 2 s . c  o  m
        if (lastCp == null) {
            iterable = collection.find();
        } else {
            iterable = collection.find(new Document("ts", new Document("$gt", lastCp)));
        }
        cursor = iterable.sort(new Document("$natural", 1)).noCursorTimeout(true)
                .cursorType(CursorType.TailableAwait).batchSize(3000).iterator();
        try {
            executeCursor(cursor);
            Thread.sleep(WAIT_FOR_NEXT_ITERATION);
        } catch (MongoCursorNotFoundException e) {
            logger.info("Cursor has been closed. About to open a new cursor. ID: "
                    + cursor.getServerCursor().getId());
        } catch (Exception e) {
            logger.error("Exception while replicating", e);
            throw new RuntimeException(e);
        } finally {
            cursor.close();
        }
    } while (true);
}

From source file:io.mandrel.timeline.impl.MongoTimelineRepository.java

License:Apache License

@Override
public void pool(Listener listener) {

    LocalDateTime date = LocalDateTime.now();
    Bson query = Filters.gt("time", date);

    try {/* w  w w  .j a va 2s.c o m*/
        while (true) {
            MongoCursor<Document> cursor = timeline.find(query).cursorType(CursorType.TailableAwait).iterator();

            while (true) {
                if (!cursor.hasNext()) {
                    if (cursor.getServerCursor() == null) {
                        break;
                    }
                    continue;
                }

                Document result = cursor.next();
                try {
                    Event event = mapper.readValue(result.toJson(), Event.class);
                    date = event.getTime();

                    listener.on(event);
                } catch (Exception e) {
                    log.warn("Error while getting the event", e);
                }
            }

            query = Filters.gt("time", date);
        }
    } catch (Exception e) {
        log.warn("Event pool process is down!", e);
    }
}

From source file:org.springframework.data.mongodb.core.messaging.CursorReadingTask.java

License:Apache License

private static boolean isValidCursor(@Nullable MongoCursor<?> cursor) {

    if (cursor == null) {
        return false;
    }//from   w  w  w  .j av a 2  s. c  o  m

    if (cursor.getServerCursor() == null || cursor.getServerCursor().getId() == 0) {
        return false;
    }

    return true;
}