use of org.apache.camel.CamelExchangeException in project camel by apache.
the class AggregateProcessor method doProcess.
protected void doProcess(Exchange exchange) throws Exception {
if (getStatistics().isStatisticsEnabled()) {
totalIn.incrementAndGet();
}
//check for the special header to force completion of all groups (and ignore the exchange otherwise)
boolean completeAllGroups = exchange.getIn().getHeader(Exchange.AGGREGATION_COMPLETE_ALL_GROUPS, false, boolean.class);
if (completeAllGroups) {
forceCompletionOfAllGroups();
return;
}
// compute correlation expression
String key = correlationExpression.evaluate(exchange, String.class);
if (ObjectHelper.isEmpty(key)) {
// we have a bad correlation key
if (isIgnoreInvalidCorrelationKeys()) {
LOG.debug("Invalid correlation key. This Exchange will be ignored: {}", exchange);
return;
} else {
throw new CamelExchangeException("Invalid correlation key", exchange);
}
}
// is the correlation key closed?
if (closedCorrelationKeys != null && closedCorrelationKeys.containsKey(key)) {
throw new ClosedCorrelationKeyException(key, exchange);
}
// when optimist locking is enabled we keep trying until we succeed
if (optimisticLocking) {
List<Exchange> aggregated = null;
boolean exhaustedRetries = true;
int attempt = 0;
do {
attempt++;
// copy exchange, and do not share the unit of work
// the aggregated output runs in another unit of work
Exchange copy = ExchangeHelper.createCorrelatedCopy(exchange, false);
try {
aggregated = doAggregation(key, copy);
exhaustedRetries = false;
break;
} catch (OptimisticLockingAggregationRepository.OptimisticLockingException e) {
LOG.trace("On attempt {} OptimisticLockingAggregationRepository: {} threw OptimisticLockingException while trying to add() key: {} and exchange: {}", new Object[] { attempt, aggregationRepository, key, copy, e });
optimisticLockRetryPolicy.doDelay(attempt);
}
} while (optimisticLockRetryPolicy.shouldRetry(attempt));
if (exhaustedRetries) {
throw new CamelExchangeException("Exhausted optimistic locking retry attempts, tried " + attempt + " times", exchange, new OptimisticLockingAggregationRepository.OptimisticLockingException());
} else if (aggregated != null) {
// we are completed so submit to completion
for (Exchange agg : aggregated) {
onSubmitCompletion(key, agg);
}
}
} else {
// copy exchange, and do not share the unit of work
// the aggregated output runs in another unit of work
Exchange copy = ExchangeHelper.createCorrelatedCopy(exchange, false);
// when memory based then its fast using synchronized, but if the aggregation repository is IO
// bound such as JPA etc then concurrent aggregation per correlation key could
// improve performance as we can run aggregation repository get/add in parallel
List<Exchange> aggregated = null;
lock.lock();
try {
aggregated = doAggregation(key, copy);
} finally {
lock.unlock();
}
// we are completed so do that work outside the lock
if (aggregated != null) {
for (Exchange agg : aggregated) {
onSubmitCompletion(key, agg);
}
}
}
// check for the special header to force completion of all groups (inclusive of the message)
boolean completeAllGroupsInclusive = exchange.getIn().getHeader(Exchange.AGGREGATION_COMPLETE_ALL_GROUPS_INCLUSIVE, false, boolean.class);
if (completeAllGroupsInclusive) {
forceCompletionOfAllGroups();
}
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class CharlesSplitAndTryCatchRollbackIssueTest method testSplitWithTryCatchAndRollbackException.
public void testSplitWithTryCatchAndRollbackException() throws Exception {
MockEndpoint split = getMockEndpoint("mock:split");
MockEndpoint ile = getMockEndpoint("mock:ile");
MockEndpoint exception = getMockEndpoint("mock:exception");
split.expectedBodiesReceived("A", "B");
ile.expectedMessageCount(0);
exception.expectedMessageCount(1);
try {
template.sendBody("direct:start", "A,B,Kaboom,C");
fail("Should thrown an exception");
} catch (CamelExecutionException e) {
CamelExchangeException ee = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
assertTrue(ee.getMessage().startsWith("Sequential processing failed for number 2."));
RollbackExchangeException re = assertIsInstanceOf(RollbackExchangeException.class, ee.getCause());
assertTrue(re.getMessage().startsWith("Intended rollback"));
}
assertMockEndpointsSatisfied();
}
Aggregations