MuleESB / 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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
