private void saveEvents(Map<String, List<DBObject>> eventMap) { if (eventMap.isEmpty()) { logger.debug("eventMap is empty"); return; } for (String eventCollection : eventMap.keySet()) { List<DBObject> docs = eventMap.get(eventCollection); if (logger.isDebugEnabled()) { logger.debug("collection: {}, length: {}", eventCollection, docs.size()); } int separatorIndex = eventCollection.indexOf(NAMESPACE_SEPARATOR); String eventDb = eventCollection.substring(0, separatorIndex); String collectionName = eventCollection.substring(separatorIndex + 1); // Warning: please change the WriteConcern level if you need high datum consistence. CommandResult result = mongo .getDB(eventDb) .getCollection(collectionName) .insert(docs, WriteConcern.NORMAL) .getLastError(); if (result.ok()) { String errorMessage = result.getErrorMessage(); if (errorMessage != null) { logger.error("can't insert documents with error: {} ", errorMessage); logger.error("with exception", result.getException()); throw new MongoException(errorMessage); } } else { logger.error("can't get last error"); } } }
private void processAndTransferWriteResult(WriteResult result, Exchange exchange) { // if invokeGetLastError is set, or a WriteConcern is set which implicitly calls getLastError, // then we have the chance to populate // the MONGODB_LAST_ERROR header, as well as setting an exception on the Exchange if one // occurred at the MongoDB server if (endpoint.isInvokeGetLastError() || (endpoint.getWriteConcern() != null ? endpoint.getWriteConcern().callGetLastError() : false)) { CommandResult cr = result.getCachedLastError() == null ? result.getLastError() : result.getCachedLastError(); exchange.getOut().setHeader(MongoDbConstants.LAST_ERROR, cr); if (!cr.ok()) { exchange.setException(MongoDbComponent.wrapInCamelMongoDbException(cr.getException())); } } // determine where to set the WriteResult: as the OUT body or as an IN message header if (endpoint.isWriteResultAsHeader()) { exchange.getOut().setHeader(MongoDbConstants.WRITERESULT, result); } else { exchange.getOut().setBody(result); } }