Search in sources :

Example 81 with CamelExchangeException

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();
    }
}
Also used : Exchange(org.apache.camel.Exchange) CamelExchangeException(org.apache.camel.CamelExchangeException) OptimisticLockingAggregationRepository(org.apache.camel.spi.OptimisticLockingAggregationRepository) Endpoint(org.apache.camel.Endpoint)

Example 82 with CamelExchangeException

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();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) CamelExchangeException(org.apache.camel.CamelExchangeException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RollbackExchangeException(org.apache.camel.RollbackExchangeException)

Aggregations

CamelExchangeException (org.apache.camel.CamelExchangeException)82 IApplication (com.openshift.client.IApplication)23 Exchange (org.apache.camel.Exchange)17 IOException (java.io.IOException)10 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)8 IEnvironmentVariable (com.openshift.client.IEnvironmentVariable)5 InputStream (java.io.InputStream)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 AsyncCallback (org.apache.camel.AsyncCallback)5 CamelExecutionException (org.apache.camel.CamelExecutionException)5 Message (org.apache.camel.Message)5 File (java.io.File)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Processor (org.apache.camel.Processor)4 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 IEmbeddedCartridge (com.openshift.client.cartridge.IEmbeddedCartridge)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Serializable (java.io.Serializable)3 URI (java.net.URI)3 List (java.util.List)3