Search in sources :

Example 1 with MongoCursorNotFoundException

use of com.mongodb.MongoCursorNotFoundException in project mongo-java-driver by mongodb.

the class GetMoreProtocol method execute.

@Override
public QueryResult<T> execute(final InternalConnection connection) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("Getting more documents from namespace %s with cursor %d on connection [%s] to server %s", namespace, cursorId, connection.getDescription().getConnectionId(), connection.getDescription().getServerAddress()));
    }
    long startTimeNanos = System.nanoTime();
    GetMoreMessage message = new GetMoreMessage(namespace.getFullName(), cursorId, numberToReturn);
    QueryResult<T> queryResult = null;
    try {
        sendMessage(message, connection);
        ResponseBuffers responseBuffers = connection.receiveMessage(message.getId());
        try {
            if (responseBuffers.getReplyHeader().isCursorNotFound()) {
                throw new MongoCursorNotFoundException(message.getCursorId(), connection.getDescription().getServerAddress());
            }
            if (responseBuffers.getReplyHeader().isQueryFailure()) {
                BsonDocument errorDocument = new ReplyMessage<BsonDocument>(responseBuffers, new BsonDocumentCodec(), message.getId()).getDocuments().get(0);
                throw getQueryFailureException(errorDocument, connection.getDescription().getServerAddress());
            }
            queryResult = new QueryResult<T>(namespace, new ReplyMessage<T>(responseBuffers, resultDecoder, message.getId()), connection.getDescription().getServerAddress());
            if (commandListener != null) {
                sendCommandSucceededEvent(message, COMMAND_NAME, asGetMoreCommandResponseDocument(queryResult, responseBuffers), connection.getDescription(), startTimeNanos, commandListener);
            }
        } finally {
            responseBuffers.close();
        }
        LOGGER.debug("Get-more completed");
        return queryResult;
    } catch (RuntimeException e) {
        if (commandListener != null) {
            sendCommandFailedEvent(message, COMMAND_NAME, connection.getDescription(), startTimeNanos, e, commandListener);
        }
        throw e;
    }
}
Also used : BsonDocument(org.bson.BsonDocument) MongoCursorNotFoundException(com.mongodb.MongoCursorNotFoundException) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 2 with MongoCursorNotFoundException

use of com.mongodb.MongoCursorNotFoundException in project camel by apache.

the class MongoDbTailingProcess method doRun.

/**
     * The heart of the tailing process.
     */
private void doRun() {
    int counter = 0;
    int persistRecords = endpoint.getPersistRecords();
    boolean persistRegularly = persistRecords > 0;
    // while the cursor has more values, keepRunning is true and the cursorId is not 0, which symbolizes that the cursor is dead
    try {
        while (cursor.hasNext() && keepRunning) {
            //cursor.getCursorId() != 0 &&
            DBObject dbObj = cursor.next();
            Exchange exchange = endpoint.createMongoDbExchange(dbObj);
            try {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Sending exchange: {}, ObjectId: {}", exchange, dbObj.get("_id"));
                }
                consumer.getProcessor().process(exchange);
            } catch (Exception e) {
            // do nothing
            }
            tailTracking.setLastVal(dbObj);
            if (persistRegularly && counter++ % persistRecords == 0) {
                tailTracking.persistToStore();
            }
        }
    } catch (MongoCursorNotFoundException e) {
        // waiting for more data to arrive
        if (keepRunning) {
            LOG.debug("Cursor not found exception from MongoDB, will regenerate cursor. This is normal behaviour with tailable cursors.", e);
        }
    }
    // the loop finished, persist the lastValue just in case we are shutting down
    // TODO: perhaps add a functionality to persist every N records
    tailTracking.persistToStore();
}
Also used : Exchange(org.apache.camel.Exchange) MongoCursorNotFoundException(com.mongodb.MongoCursorNotFoundException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MongoCursorNotFoundException(com.mongodb.MongoCursorNotFoundException)

Example 3 with MongoCursorNotFoundException

use of com.mongodb.MongoCursorNotFoundException in project camel by apache.

the class MongoDbTailingProcess method doRun.

/**
     * The heart of the tailing process.
     */
private void doRun() {
    // cursorId is not 0, which symbolizes that the cursor is dead
    try {
        while (cursor.hasNext() && keepRunning) {
            // cursor.getCursorId() !=
            // 0 &&
            Document dbObj = cursor.next();
            Exchange exchange = endpoint.createMongoDbExchange(dbObj);
            try {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Sending exchange: {}, ObjectId: {}", exchange, dbObj.get(MONGO_ID));
                }
                consumer.getProcessor().process(exchange);
            } catch (Exception e) {
            // do nothing
            }
            tailTracking.setLastVal(dbObj);
        }
    } catch (MongoCursorNotFoundException e) {
        // waiting for more data to arrive
        if (keepRunning) {
            LOG.debug("Cursor not found exception from MongoDB, will regenerate cursor. This is normal behaviour with tailable cursors.", e);
        }
    }
    // the loop finished, persist the lastValue just in case we are shutting
    // down
    // TODO: perhaps add a functionality to persist every N records
    tailTracking.persistToStore();
}
Also used : Exchange(org.apache.camel.Exchange) MongoCursorNotFoundException(com.mongodb.MongoCursorNotFoundException) Document(org.bson.Document) MongoCursorNotFoundException(com.mongodb.MongoCursorNotFoundException)

Aggregations

MongoCursorNotFoundException (com.mongodb.MongoCursorNotFoundException)3 Exchange (org.apache.camel.Exchange)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 BsonDocument (org.bson.BsonDocument)1 Document (org.bson.Document)1 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)1