Red Hat Training

A Red Hat training course is available for Red Hat Fuse

9.2. Demarcation by Marking the Route

Overview

If the consumer endpoint at the start of a route does not support transactions, you can nevertheless initiate a transaction immediately after receiving an incoming message by inserting the transacted() command into your route.

Demarcation using transacted()

By default, the transacted() command uses the first transaction manager of type PlatformTransactionManager that it finds in the bean registry (which could either be an OSGi service, a bean defined in Spring XML, or a bean defined in blueprint). Because the PlatformTransactionManager interface is, by default, exported as an OSGi service, the transacted() command will automatically find the XA transaction manager.
When you use the transacted() command to mark a route as transacted, all of the processors following transacted() participate in the transaction; all of the processors preceding transacted() do not participate in the transaction. For example, you could use transacted() to make a route transactional, as follows:
// Java
import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder extends RouteBuilder {
    ...
    public void configure() {
        from("file:src/data?noop=true")
          .transacted()
          .beanRef("accountService","credit")
          .beanRef("accountService","debit")
          .to("jmstx:queue:processed");
    }
}
Important
If your container exports multiple OSGi services of PlatformTransactionManager type or if you register multiple TransactedPolicy objects in the bean registry (for example, by defining beans in Spring XML), you cannot be certain which transaction manager would be picked up by the transacted() command (see Default transaction manager and transacted policy). In such cases, it is recommended that you specify the transaction policy explicitly.

Specifying the transaction policy explicitly

To eliminate any ambiguity about which transaction manager is used, you can specify the transaction policy explicitly by passing the transaction policy's bean ID as an argument to the transacted() command. First of all, you need to define the transaction policy (of type, org.apache.camel.spring.spi.SpringTransactionPolicy), which encapsulates a reference to the transaction manager you want to use—for example:
<beans ...>
    ...
    <!-- access through Spring's PlatformTransactionManager -->
    <osgi:reference id="osgiPlatformTransactionManager"
                    interface="org.springframework.transaction.PlatformTransactionManager"/>
    ...
    <bean id="XA_TX_REQUIRED" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
        <property name="transactionManager" ref="osgiPlatformTransactionManager"/>
    </bean>
    ...
</beans>
After the transaction policy bean is defined, you can use it by passing its bean ID, XA_TX_REQUIRED, as a string argument to the transacted() command—for example:
// Java
import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder extends RouteBuilder {
    ...
    public void configure() {
        from("file:src/data?noop=true")
          .transacted("XA_TX_REQUIRED")
          .beanRef("accountService","credit")
          .beanRef("accountService","debit")
          .to("jmstx:queue:processed");
    }
}
For more details about transaction policies, see Propagation Policies.

XML syntax

You can also use the transacted command in Spring XML or blueprint files. For example, to demarcate an XA transaction in Spring XML:
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ... >

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
      <from uri="file:src/data?noop=true"/>
      <transacted ref="XA_TX_REQUIRED"/>
      <bean ref="accountService" method="credit"/>
      <bean ref="accountService" method="debit"/>
      <to uri="jmstx:queue:processed"/>
    </route>
  </camelContext>

</beans>