Search in sources :

Example 1 with RollbackExchangeException

use of org.apache.camel.RollbackExchangeException in project camel by apache.

the class CharlesSplitAndTryCatchRollbackIssueTest method testSplitWithTryCatchAndRollbacILEAndException.

public void testSplitWithTryCatchAndRollbacILEAndException() throws Exception {
    MockEndpoint split = getMockEndpoint("mock:split");
    MockEndpoint ile = getMockEndpoint("mock:ile");
    MockEndpoint exception = getMockEndpoint("mock:exception");
    split.expectedBodiesReceived("A", "B");
    ile.expectedMessageCount(1);
    exception.expectedMessageCount(1);
    try {
        template.sendBody("direct:start", "A,Forced,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 3."));
        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)

Example 2 with RollbackExchangeException

use of org.apache.camel.RollbackExchangeException in project camel by apache.

the class SqlConsumer method processBatch.

@Override
public int processBatch(Queue<Object> exchanges) throws Exception {
    int total = exchanges.size();
    if (maxMessagesPerPoll > 0 && total == maxMessagesPerPoll) {
        log.debug("Maximum messages to poll is {} and there were exactly {} messages in this poll.", maxMessagesPerPoll, total);
    }
    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());
        Exchange exchange = holder.exchange;
        Object data = holder.data;
        // 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;
        // process the current exchange
        try {
            getProcessor().process(exchange);
        } catch (Exception e) {
            exchange.setException(e);
        }
        if (getEndpoint().isTransacted() && exchange.isFailed()) {
            // break out as we are transacted and should rollback
            Exception cause = exchange.getException();
            if (cause != null) {
                throw cause;
            } else {
                throw new RollbackExchangeException("Rollback transaction due error processing exchange", exchange);
            }
        }
        // pick the on consume to use
        String sql = exchange.isFailed() ? onConsumeFailed : onConsume;
        try {
            // we can only run on consume if there was data
            if (data != null && sql != null) {
                int updateCount;
                if (namedJdbcTemplate != null && sqlProcessingStrategy instanceof SqlNamedProcessingStrategy) {
                    SqlNamedProcessingStrategy namedProcessingStrategy = (SqlNamedProcessingStrategy) sqlProcessingStrategy;
                    updateCount = namedProcessingStrategy.commit(getEndpoint(), exchange, data, namedJdbcTemplate, parameterSource, sql);
                } else {
                    updateCount = sqlProcessingStrategy.commit(getEndpoint(), exchange, data, jdbcTemplate, sql);
                }
                if (expectedUpdateCount > -1 && updateCount != expectedUpdateCount) {
                    String msg = "Expected update count " + expectedUpdateCount + " but was " + updateCount + " executing query: " + sql;
                    throw new SQLException(msg);
                }
            }
        } catch (Exception e) {
            if (breakBatchOnConsumeFail) {
                throw e;
            } else {
                handleException("Error executing onConsume/onConsumeFailed query " + sql, e);
            }
        }
    }
    try {
        if (onConsumeBatchComplete != null) {
            int updateCount;
            if (namedJdbcTemplate != null && sqlProcessingStrategy instanceof SqlNamedProcessingStrategy) {
                SqlNamedProcessingStrategy namedProcessingStrategy = (SqlNamedProcessingStrategy) sqlProcessingStrategy;
                updateCount = namedProcessingStrategy.commitBatchComplete(getEndpoint(), namedJdbcTemplate, parameterSource, onConsumeBatchComplete);
            } else {
                updateCount = sqlProcessingStrategy.commitBatchComplete(getEndpoint(), jdbcTemplate, onConsumeBatchComplete);
            }
            log.debug("onConsumeBatchComplete update count {}", updateCount);
        }
    } catch (Exception e) {
        if (breakBatchOnConsumeFail) {
            throw e;
        } else {
            handleException("Error executing onConsumeBatchComplete query " + onConsumeBatchComplete, e);
        }
    }
    return total;
}
Also used : Exchange(org.apache.camel.Exchange) SQLException(java.sql.SQLException) RollbackExchangeException(org.apache.camel.RollbackExchangeException) DataAccessException(org.springframework.dao.DataAccessException) RollbackExchangeException(org.apache.camel.RollbackExchangeException) SQLException(java.sql.SQLException)

Example 3 with RollbackExchangeException

use of org.apache.camel.RollbackExchangeException in project camel by apache.

the class TransactionalClientDataSourceWithOnExceptionRollbackTest method testTransactionRollback.

public void testTransactionRollback() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:error");
    mock.expectedMessageCount(1);
    try {
        template.sendBody("direct:fail", "Hello World");
        fail("Should have thrown exception");
    } catch (RuntimeCamelException e) {
        // expected as we fail
        assertIsInstanceOf(RuntimeCamelException.class, e.getCause());
        RollbackExchangeException rollback = assertIsInstanceOf(RollbackExchangeException.class, e.getCause().getCause());
        assertEquals("Donkey in Action", rollback.getExchange().getIn().getBody());
    }
    assertMockEndpointsSatisfied();
    int count = jdbc.queryForObject("select count(*) from books", Integer.class);
    assertEquals("Number of books", 1, count);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RollbackExchangeException(org.apache.camel.RollbackExchangeException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 4 with RollbackExchangeException

use of org.apache.camel.RollbackExchangeException in project camel by apache.

the class MyBatisConsumer method processBatch.

public int processBatch(Queue<Object> exchanges) throws Exception {
    final MyBatisEndpoint endpoint = getEndpoint();
    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());
        Exchange exchange = holder.exchange;
        Object data = holder.data;
        // 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;
        // process the current exchange
        LOG.debug("Processing exchange: {} with properties: {}", exchange, exchange.getProperties());
        getProcessor().process(exchange);
        try {
            if (onConsume != null) {
                endpoint.getProcessingStrategy().commit(endpoint, exchange, data, onConsume);
            }
        } catch (Exception e) {
            handleException(e);
        }
        if (getEndpoint().isTransacted() && exchange.isFailed()) {
            // break out as we are transacted and should rollback
            Exception cause = exchange.getException();
            if (cause != null) {
                throw cause;
            } else {
                throw new RollbackExchangeException("Rollback transaction due error processing exchange", exchange);
            }
        }
    }
    return total;
}
Also used : Exchange(org.apache.camel.Exchange) RollbackExchangeException(org.apache.camel.RollbackExchangeException) RollbackExchangeException(org.apache.camel.RollbackExchangeException)

Example 5 with RollbackExchangeException

use of org.apache.camel.RollbackExchangeException 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

RollbackExchangeException (org.apache.camel.RollbackExchangeException)5 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 CamelExchangeException (org.apache.camel.CamelExchangeException)2 CamelExecutionException (org.apache.camel.CamelExecutionException)2 Exchange (org.apache.camel.Exchange)2 SQLException (java.sql.SQLException)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 DataAccessException (org.springframework.dao.DataAccessException)1