I have below code sample where I am calling a direct transacted route from "interceptFrom" of InputQueue route (main route). Here, main route is also transacted.
When running this, I can see from the log that a transaction is being started and committed in interceptFrom, while in main route another transaction is starting and getting committed.
My requirement is, I need to have a single transaction which should cover both fromIntercept and main route processing as I am doing some DB operations in fromIntercept and want it to rollback in case any exception happens in main route. Below is the log:
@Component
public class DemoTransactionRoute extends SpringRouteBuilder {
private static Logger logger = LoggerFactory.getLogger(DemoTransactionRoute.class);
@Override
public void configure() throws Exception {
final String FROM_INTERCEPT_ROUTE = "direct://demoFromInterceptor";
interceptFrom("activemq:*")
.to(FROM_INTERCEPT_ROUTE);
from(FROM_INTERCEPT_ROUTE)
.routeId(FROM_INTERCEPT_ROUTE)
.transacted("JPA_TRANSACTION") // defined in service xml
.log(LoggingLevel.INFO, logger, "Inside From Interceptor");
from("activemq:queue:InputQueue")
.routeId("activemq:queue:InputQueue")
.transacted("JPA_TRANSACTION") // defined in service xml
.log("On service route");
}
}
Log
2020-05-28 12:09:06.742 DEBUG 15936 --- [mer[InputQueue]] o.a.c.c.jms.EndpointMessageListener : Received Message has JMSCorrelationID []
2020-05-28 12:09:06.785 DEBUG 15936 --- [mer[InputQueue]] o.apache.camel.processor.SendProcessor : >>>> direct://demoFromInterceptor Exchange[ID-XYZA-Z123456-1590631725657-0-1]
2020-05-28 12:09:06.787 DEBUG 15936 --- [mer[InputQueue]] o.a.c.s.spi.TransactionErrorHandler : Transaction begin (0x78d9f51b) redelivered(false) for (MessageId: ID:XYZA-Z123456-52145-1590554620554-4:5:1:1:1 on ExchangeId: ID-XYZA-Z123456-1590631725657-0-1))
2020-05-28 12:09:06.792 INFO 15936 --- [mer[InputQueue]] c.c.c.routes.DemoTransactionRoute : Inside From Interceptor
2020-05-28 12:09:06.793 DEBUG 15936 --- [mer[InputQueue]] o.a.c.s.spi.TransactionErrorHandler : Transaction commit (0x78d9f51b) redelivered(false) for (MessageId: ID:XYZA-Z123456-52145-1590554620554-4:5:1:1:1 on ExchangeId: ID-XYZA-Z123456-1590631725657-0-1))
2020-05-28 12:09:06.793 DEBUG 15936 --- [mer[InputQueue]] o.a.c.s.spi.TransactionErrorHandler : Transaction begin (0x78d9f51b) redelivered(false) for (MessageId: ID:XYZA-Z123456-52145-1590554620554-4:5:1:1:1 on ExchangeId: ID-XYZA-Z123456-1590631725657-0-1))
2020-05-28 12:09:06.793 INFO 15936 --- [mer[InputQueue]] activemq:queue:InputQueue : On service route
2020-05-28 12:09:06.793 DEBUG 15936 --- [mer[InputQueue]] o.a.c.s.spi.TransactionErrorHandler : Transaction commit (0x78d9f51b) redelivered(false) for (MessageId: ID:XYZA-Z123456-52145-1590554620554-4:5:1:1:1 on ExchangeId: ID-XYZA-Z123456-1590631725657-0-1))