Search

1.3. Getting Started with Transactions

download PDF

1.3.1. Prerequisites

Overview

The following are required to complete this example:

Java Runtime

Apache Camel requires a Java 7 development kit (JDK 1.7.0). After installing the JDK, set your JAVA_HOME environment variable to point to the root directory of your JDK, and set your PATH environment variable to include the Java bin directory.

Apache Maven 3

The Apache Camel Maven tooling requires Apache Maven version 3. To download Apache Maven, go to http://maven.apache.org/download.html.
After installing Apache Maven do the following:
  1. Set your M2_HOME environment variable to point to the Maven root directory.
  2. Set your MAVEN_OPTS environment variable to -Xmx512M to increase the memory available for Maven builds.
  3. Set your PATH environment variable to include the Maven bin directory:
    PlatformPath
    Windows%M2_HOME%\bin
    UNIX$M2_HOME/bin

1.3.2. Generate a New Project

Overview

Use the Maven archetype, servicemix-camel-osgi-bundle, to generate a sample Java application which you can then use as a starting point for your application.

Steps

To generate the new project, perform the following steps:
  1. Open a new command window and change to the directory where you want to store the new Maven project.
  2. Enter the following command to generate the new Maven project:
    mvn archetype:generate
     -DarchetypeRepository=http://repo.fusesource.com/maven2
     -DarchetypeGroupId=org.apache.servicemix.tooling
     -DarchetypeArtifactId=servicemix-camel-osgi-bundle
     -DarchetypeVersion=2013.01.0.redhat-610379
     -DgroupId=tutorial
     -DartifactId=tx-jms-router
    Each time you are prompted for input, press Enter to accept the default.
    This command generates a basic router application under the tx-jms-router directory. You will customize this basic application to demonstrate transactions in Apache Camel.
    Note
    Maven accesses the Internet to download JARs and stores them in its local repository.
  3. Customize the project POM file, tx-jms-router/pom.xml, by adding some new project dependencies. First of all, define some properties for the dependency versions. Using your favorite text editor, open the POM file and add the following properties:
    <project ...>
        ...
        <properties>
           <camel.version>2.12.0.redhat-610379</camel.version>
           <spring.version>3.1.3.RELEASE</spring.version>
           <activemq.version>5.9.0.redhat-610379</activemq.version>
           <xbean.version>3.7</xbean.version>
        </properties>
        ...
    </project>
  4. Add dependencies on the artifacts that implement Spring transactions. Look for the dependencies element in the POM file and add the following dependency elements:
    <project ...>
        ...
        <dependencies>
          ...
          <!-- Spring transaction dependencies -->
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
          </dependency>
    
      </dependencies>
      ...
    </project>
  5. Add the JMS and ActiveMQ dependencies. Look for the dependencies element in the POM file and add the following dependency elements:
    <project ...>
        ...
        <dependencies>
          ...
          <!-- Persistence artifacts -->
          <dependency> 
            <groupId>org.apache.camel</groupId> 
            <artifactId>camel-jms</artifactId> 
            <version>${camel.version}</version>
          </dependency> 
          <dependency> 
            <groupId>org.apache.activemq</groupId> 
            <artifactId>activemq-client</artifactId> 
            <version>${activemq.version}</version> 
          </dependency>
          <!--  This is needed by the camel-jms component -->
          <dependency> 
            <groupId>org.apache.xbean</groupId> 
            <artifactId>xbean-spring</artifactId> 
            <version>${xbean.version}</version> 
          </dependency>
    
      </dependencies>
      ...
    </project>

1.3.3. Configure a Transaction Manager and a Camel Route

Overview

The basic requirements for writing a transactional application in Spring are a transaction manager bean and a resource bean (or, in some cases, multiple resource beans). You can then use the transaction manager bean either to create a transactional Apache Camel component (see Section 5.2, “Demarcation by Transactional Endpoints”) or to mark a route as transactional, using the transacted() Java DSL command (see Section 5.1, “Demarcation by Marking the Route”).

Steps

To configure a JMS transaction manager and a Camel route in Spring XML, perform the following steps:
  1. Customize the Spring XML configuration. Using your favorite text editor, open the tx-jms-router/src/main/resources/META-INF/spring/camel-context.xml file and replace the content of the beans element with the following XML code:
    ...
    <beans ... >
      ...
      <osgi:camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
          <from uri="file:src/data?noop=true"/>
          <convertBodyTo type="java.lang.String"/>
          <to uri="jmstx:queue:giro"/>
        </route>
        <route>
          <from uri="jmstx:queue:giro"/>
          <to uri="jmstx:queue:credits"/>
          <to uri="jmstx:queue:debits"/>
          <bean ref="myTransform" method="transform"/>
        </route>
      </osgi:camelContext>
    
      <bean id="myTransform" class="tutorial.MyTransform"/>
    
      <bean id="jmstx" class="org.apache.camel.component.jms.JmsComponent"> 
        <property name="configuration" ref="jmsConfig" /> 
      </bean> 
    
      <bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"> 
          <property name="connectionFactory" ref="jmsConnectionFactory"/> 
          <property name="transactionManager" ref="jmsTransactionManager"/> 
          <property name="transacted" value="true"/> 
      </bean> 
    
      <bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager">
        <property name="connectionFactory" ref="jmsConnectionFactory" />
      </bean>
      
      <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616"/>
        <property name="userName" value="Username"/>
        <property name="password" value="Password"/>
      </bean>
    
    </beans>
  2. In the jmsConnectionFactory bean from the preceding Spring XML code, customize the values of the userName and password property settings with one of the user credentials from the JBoss Fuse container. By default, the container's user credentials are normally defined in the etc/users.properties file.

1.3.4. Customize the MyTransform Bean

Overview

The purpose of the MyTransform bean class is to force a rollback of the current transaction, by throwing an exception. The bean gets called at the end of the second transactional route. This enables you to verify the behaviour of a rolled back transaction.

Steps

Customize the MyTransform bean class. Using your favorite text editor, open the tx-jms-router/src/main/java/tutorial/MyTransform.java file and replace the contents of the file with the following Java code:
package tutorial;

import java.util.Date;
import java.util.logging.Logger;

public class MyTransform {
    private static final transient Logger LOGGER = Logger.getLogger(MyTransform.class.getName());

    public String transform(String body)
    throws java.lang.Exception
    {
        // should be printed n times due to redeliveries 
        LOGGER.info("message body = " + body); 
        // force rollback 
        throw new java.lang.Exception("test");
    }

}

1.3.5. Build and Run the Example

Overview

After building and running the example using Maven, you can use the Fuse Management Console to examine what has happened to the JMS queues involved in the application.

Steps

To build and run the transactional JMS example, perform the following steps:
  1. To build the example, open a command prompt, change directory to tx-jms-router, and enter the following Maven command:
    mvn install
    If the build is successful, you should see the file, tx-jms-router-1.0-SNAPSHOT.jar, appear under the tx-jms-router/target directory.
  2. Create a sample message for the routes to consume when they are running in the container. Create the following directory path in the containers installation directory:
    InstallDir/src/data
    In the data directory create the file, message.txt, with the following contents:
    Test message.
  3. Start up the JBoss Fuse container. Open a new command prompt and enter the following commands:
    cd InstallDir/bin
    ./fuse
  4. To install and start the example in the container, enter the following console command:
    JBossFuse:karaf@root> install -s mvn:tutorial/tx-jms-router/1.0-SNAPSHOT
  5. To see the result of running the routes, open the container log using the log:display command, as follows:
    JBossFuse:karaf@root> log:display
    If all goes well, you should see about a dozen occurrences of java.lang.Exception: test in the log. This is the expected behaviour.
  6. What happened? The series of runtime exceptions thrown by the application is exactly what we expect to happen, because the route is programmed to throw an exception every time an exchange is processed by the route. The purpose of throwing the exception is to trigger a transaction rollback, causing the current exchange to be un-enqueued from the queue:credit and queue:debit queues.
  7. To gain a better insight into what occurred, user your browser to connect to the Fuse Management Console. Navigate to the following URL in your browser:
    http://localhost:8181/hawtio
    You will be prompted to log in. Use one of the credentials configured for your container (usually defined in the InstallDir/etc/users.properties file).
  8. Click on the ActiveMQ tab to explore the JMS queues that are accessed by the example routes.
  9. Drill down to the giro queue. Notice that the EnqueueCount, DispatchCount, and DequeueCount for giro are all equal to 1, which indicates that one message entered the queue and one message was pulled off the queue.
  10. Click on the debits queue. Notice that the EnqueueCount, DispatchCount, and DequeueCount for debits are all equal to 0. This is because the test exception caused the enqueued message to be rolled back each time an exchange passed through the route. The same thing happened to the credits queue.
  11. Click on the ActiveMQ.DLQ queue. The DLQ part of this name stands for Dead Letter Queue and it is an integral part of the way ActiveMQ deals with failed message dispatches. In summary, the default behavior of ActiveMQ when it fails to dispatch a message (that is, when an exception reaches the JMS consumer endpoint, jmstx:queue:giro), is as follows:
    1. The consumer endpoint attempts to redeliver the message. Redelivery attempts can be repeated up to a configurable maximum number of times.
    2. If the redeliveries limit is exceeded, the consumer endpoint gives up trying to deliver the message and enqueues it on the dead letter queue instead (by default, ActiveMQ.DLQ).
    You can see from the status of the ActiveMQ.DLQ queue that the number of enqueued messages, EnqueueCount, is equal to 1. This is where the failed message has ended up.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.