public void process(Exchange exchange) throws Exception { MongoDbOperation operation = endpoint.getOperation(); Object header = exchange.getIn().getHeader(MongoDbConstants.OPERATION_HEADER); if (header != null) { LOG.debug("Overriding default operation with operation specified on header: {}", header); try { if (header instanceof MongoDbOperation) { operation = ObjectHelper.cast(MongoDbOperation.class, header); } else { // evaluate as a String operation = MongoDbOperation.valueOf( exchange.getIn().getHeader(MongoDbConstants.OPERATION_HEADER, String.class)); } } catch (Exception e) { throw new CamelMongoDbException( "Operation specified on header is not supported. Value: " + header, e); } } try { invokeOperation(operation, exchange); } catch (Exception e) { throw MongoDbComponent.wrapInCamelMongoDbException(e); } }
private WriteConcern extractWriteConcern(Exchange exchange) throws CamelMongoDbException { Object o = exchange.getIn().getHeader(MongoDbConstants.WRITECONCERN); if (o == null) { return null; } else if (o instanceof WriteConcern) { return ObjectHelper.cast(WriteConcern.class, o); } else if (o instanceof String) { WriteConcern answer = WriteConcern.valueOf(ObjectHelper.cast(String.class, o)); if (answer == null) { throw new CamelMongoDbException( "WriteConcern specified in the " + MongoDbConstants.WRITECONCERN + " header, with value " + o + " could not be resolved to a WriteConcern type"); } } // should never get here LOG.warn("A problem occurred while resolving the Exchange's Write Concern"); return null; }
public int processBatch(Queue<Object> exchanges) throws Exception { int total = exchanges.size(); // limit if needed if (maxMessagesPerPoll > 0 && total > maxMessagesPerPoll) { LOG.debug( "Limiting to maximum messages to poll " + maxMessagesPerPoll + " as there were " + total + " messages in this poll."); total = maxMessagesPerPoll; } for (int index = 0; index < total && isBatchAllowed(); index++) { // only loop if we are started (allowed to run) DataHolder holder = ObjectHelper.cast(DataHolder.class, exchanges.poll()); EntityManager entityManager = holder.manager; Exchange exchange = holder.exchange; Object result = holder.result; // add current index and total as properties exchange.setProperty(Exchange.BATCH_INDEX, index); exchange.setProperty(Exchange.BATCH_SIZE, total); exchange.setProperty(Exchange.BATCH_COMPLETE, index == total - 1); // update pending number of exchanges pendingExchanges = total - index - 1; if (lockEntity(result, entityManager)) { // Run the @PreConsumed callback createPreDeleteHandler().deleteObject(entityManager, result, exchange); // process the current exchange LOG.debug("Processing exchange: {}", exchange); getProcessor().process(exchange); if (exchange.getException() != null) { // if we failed then throw exception throw exchange.getException(); } // Run the @Consumed callback getDeleteHandler().deleteObject(entityManager, result, exchange); } } return total; }
public int processBatch(Queue<Object> exchanges) throws Exception { int total = exchanges.size(); for (int index = 0; index < total && isBatchAllowed(); index++) { // only loop if we are started (allowed to run) final Exchange exchange = ObjectHelper.cast(Exchange.class, exchanges.poll()); // add current index and total as properties exchange.setProperty(Exchange.BATCH_INDEX, index); exchange.setProperty(Exchange.BATCH_SIZE, total); exchange.setProperty(Exchange.BATCH_COMPLETE, index == total - 1); // update pending number of exchanges pendingExchanges = total - index - 1; // add on completion to handle after work when the exchange is done exchange.addOnCompletion( new Synchronization() { public void onComplete(Exchange exchange) { processCommit(exchange); } public void onFailure(Exchange exchange) { processRollback(exchange); } @Override public String toString() { return "S3ConsumerOnCompletion"; } }); LOG.trace("Processing exchange [{}]...", exchange); getAsyncProcessor() .process( exchange, new AsyncCallback() { @Override public void done(boolean doneSync) { LOG.trace("Processing exchange [{}] done.", exchange); } }); } return total; }
@Override public int processBatch(Queue<Object> exchanges) throws Exception { int processedExchanges = 0; while (!exchanges.isEmpty()) { final Exchange exchange = ObjectHelper.cast(Exchange.class, exchanges.poll()); LOG.trace("Processing exchange [{}] started.", exchange); getAsyncProcessor() .process( exchange, new AsyncCallback() { @Override public void done(boolean doneSync) { LOG.trace("Processing exchange [{}] done.", exchange); } }); processedExchanges++; } return processedExchanges; }