Integration / Apache Camel Interview Questions
How do you implement transactions in Apache Camel?
Camel supports JMS and JDBC transactions via the Spring PlatformTransactionManager. A transactional route marks a unit of work: if any step throws an exception, the transaction is rolled back and the message is redelivered by the broker (JMS) or a savepoint is rolled back (JDBC). The key moving parts are the JMS ConnectionFactory, a JmsTransactionManager, and the transacted=true route option.
// Spring Boot config (application.properties):
spring.activemq.broker-url=tcp://localhost:61616
// Transaction bean configuration:
@Bean
public PlatformTransactionManager jtaTransactionManager(ConnectionFactory cf) {
return new JmsTransactionManager(cf);
}
// Transactional route:
@Component
public class TxRoute extends RouteBuilder {
@Override
public void configure() {
from("jms:queue:orders?transacted=true")
.transacted() // enlist in Spring tx
.to("sql:INSERT INTO order_log VALUES (:#orderId)") // JDBC in same tx
.to("jms:queue:processed"); // JMS produce in same tx
}
}The .transacted() DSL method enlists the route in a Spring-managed transaction. Any exception causes rollback, and the JMS broker returns the message to the queue for redelivery. For XA (two-phase commit) across JMS + JDBC, replace JmsTransactionManager with a JTA-capable manager (Atomikos, Narayana). The TransactionErrorHandler is recommended alongside transacted() to control redelivery attempts before DLQ routing.
More Related questions...