Example usage for com.mongodb BulkWriteException getWriteConcernError

List of usage examples for com.mongodb BulkWriteException getWriteConcernError

Introduction

In this page you can find the example usage for com.mongodb BulkWriteException getWriteConcernError.

Prototype

public WriteConcernError getWriteConcernError() 

Source Link

Document

The write concern error, which may be null (in which case the list of errors will not be empty).

Usage

From source file:com.everydots.kafka.connect.mongodb.MongoDbSinkTask.java

License:Apache License

@Override
public void put(Collection<SinkRecord> records) {

    if (records.isEmpty()) {
        logger.debug("no records to write for current poll operation");
        return;//from   w  w  w  . j ava2s  .  com
    }

    MongoCollection<BsonDocument> mongoCollection = database.getCollection(
            sinkConfig.getString(MongoDbSinkConnectorConfig.MONGODB_COLLECTION_CONF), BsonDocument.class);

    List<? extends WriteModel<BsonDocument>> docsToWrite = sinkConfig.isUsingCdcHandler()
            ? buildWriteModelCDC(records)
            : buildWriteModel(records);

    try {
        logger.debug("#records to write: {}", docsToWrite.size());
        if (!docsToWrite.isEmpty()) {
            BulkWriteResult result = mongoCollection.bulkWrite(docsToWrite, BULK_WRITE_OPTIONS);
            logger.debug("write result: " + result.toString());
        }
    } catch (MongoException mexc) {

        if (mexc instanceof BulkWriteException) {
            BulkWriteException bwe = (BulkWriteException) mexc;
            logger.error("mongodb bulk write (partially) failed", bwe);
            logger.error(bwe.getWriteResult().toString());
            logger.error(bwe.getWriteErrors().toString());
            logger.error(bwe.getWriteConcernError().toString());
        } else {
            logger.error("error on mongodb operation", mexc);
            logger.error("writing {} record(s) failed -> remaining retries ({})", records.size(),
                    remainingRetries);
        }

        if (remainingRetries-- <= 0) {
            throw new ConnectException(
                    "couldn't successfully process records" + " despite retrying -> giving up :(", mexc);
        }

        logger.debug("deferring retry operation for {}ms", deferRetryMs);
        context.timeout(deferRetryMs);
        throw new RetriableException(mexc.getMessage(), mexc);
    }

}