Example usage for com.mongodb.client MongoCursor tryNext

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

Introduction

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

Prototype

@Nullable
TResult tryNext();

Source Link

Document

A special next() case that returns the next element in the iteration if available or null.

Usage

From source file:com.hurence.logisland.service.mongodb.MongoDBControllerService.java

License:Apache License

@Override
public Document findOne(Document query) {
    MongoCursor<Document> cursor = this.col.find(query).limit(1).iterator();
    Document retVal = cursor.tryNext();
    cursor.close();/*  w w w .ja  v  a  2  s .  co m*/

    return retVal;
}

From source file:com.hurence.logisland.service.mongodb.MongoDBControllerService.java

License:Apache License

@Override
public Document findOne(Document query, Document projection) {
    MongoCursor<Document> cursor = projection != null
            ? this.col.find(query).projection(projection).limit(1).iterator()
            : this.col.find(query).limit(1).iterator();
    Document retVal = cursor.tryNext();
    cursor.close();//www .j a  v a 2  s  .  c o  m

    return retVal;
}

From source file:es.omarall.mtc.TailingTask.java

License:Apache License

/**
 * Cursor LOGIC. A built cursor can be iterated until lost or until the
 * state is changed to a no started state.
 * //  w w w  . j  a  va2 s.  co  m
 * @throws NotStartedException
 *             to signal state changed to a non started state
 */
private void iterateCursor(final MongoCursor<Document> cursor) {

    if (cursor == null)
        return;

    // stores the id of the last document fetched by THIS cursor...
    ObjectId lastProcessedId = null;

    try {

        while (true) {

            // Is there a new document to be processed?
            Document next = cursor.tryNext();

            if (next == null) {

                // No doc to be processed ...
                // It is likely we come from a burst of processing ...
                // This is a chance to persist last processed
                // id...go for it

                if (tracker != null && lastProcessedId != null) {

                    tracker.persistLastTrackedEventId(lastProcessedId);
                    lastTrackedId = lastProcessedId;
                }

                // Wait for a new document to be processed
                if (!cursor.hasNext()) {
                    LOG.debug("INNER has NEXT returned no data");
                    if (cursor != null)
                        cursor.close();
                }

            } else {

                // There is a document to be processed
                try {

                    documentHandler.handleDocument(next);
                    lastProcessedId = next.getObjectId("_id");
                } catch (Exception e) {
                    LOG.error("DocumentHandler raised an exception", e);
                    // Notifiy but keep going
                }
            }

            // Check whether to keep execution
            if (getStatus().equals(ServiceStatus.STOPPED))
                throw new NotStartedException("Cursor Changed its state to not started");
        } // while

    } catch (MongoSocketException e) {
        // The cursor was closed
        LOG.error("\n\nMONGOESB - NETWORK problems: Server Address: {}", e.getServerAddress().toString(), e);

        // Not recoverable. Do not regenerate the cursor
        throw new MTCException(String.format("Network Problemns detected. Server address: %s",
                e.getServerAddress().toString()));
    } catch (MongoQueryException e) {
        // MongoCursorNotFoundException
        // The cursor was closed
        // Recoverable: Do regenerate the cursor
        LOG.info("Cursor {} has been closed.", e);
    } catch (IllegalStateException e) {
        // .hasNext(): Cursor was closed by other THREAD (documentHandler
        // cleaningup)?)
        // Recoverable. Do regenerate the cursor.
        LOG.info("Cursor being iterated was closed", e);
    } catch (NotStartedException e) {
        // Not recoverable: Do not regenerate the cursor.
        throw e;
    } finally {

        // persist tracking state
        if (tracker != null && lastProcessedId != null) {
            tracker.persistLastTrackedEventId(lastProcessedId);
            lastTrackedId = lastProcessedId;
        }

        // Cleanup resources.
        if (cursor != null)
            cursor.close();
    }
}