Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.1.3. Getting Started with Transactions
1.3.1. Prerequisites Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Overview Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The following are required to complete this example:
- Internet connection (required by Maven)
Java Runtime Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Apache Camel requires a Java 6 development kit (JDK 1.6.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 2 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The Apache Camel Maven tooling requires Apache Maven version 2.2.1 or later. To download Apache Maven, go to http://maven.apache.org/download.html.
After installing Apache Maven do the following:
- Set your
M2_HOME
environment variable to point to the Maven root directory. - Set your
MAVEN_OPTS
environment variable to-Xmx512M
to increase the memory available for Maven builds. - Set your
PATH
environment variable to include the Mavenbin
directory:Expand Platform Path Windows %M2_HOME%\bin
UNIX $M2_HOME/bin
1.3.2. Generate a New Project Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Overview Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Use the Maven archetype,
camel-archetype-java
, to generate a sample Java application which you can then use as a starting point for your application.
Steps Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
To generate the new project, perform the following steps:
- Open a new command window and change to the directory where you want to store the new Maven project.
- Enter the following command to generate the new Maven project:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This command generates a basic router application under thetx-jms-router
directory. You will customize this basic application to demonstrate transactions in .Apache CamelNoteMaven accesses the Internet to download JARs and stores them in its local repository. - 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 aspring-version
property and anactivemq-version
property as follows:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add dependencies on the artifacts that implement Spring transactions. Look for the
dependencies
element in the POM file and add the followingdependency
elements:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add the JMS and ActiveMQ dependencies. Look for the
dependencies
element in the POM file and add the followingdependency
elements:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.3.3. Configure a Transaction Manager and a Resource Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Overview Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
To configure the JMS transaction manager and the JMS resource, perform the following steps:
- 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 add the following content to thebeans
element:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This configuration creates a custom JMS component, with bean ID equal tojmstx
, that you can use to define transactional JMS endpoints in your routes. The underlying JMS system is an embedded Apache ActiveMQ broker, which takes its configuration from the file,tutorial/activemq.xml
. - Create the ActiveMQ configuration file,
activemq.xml
. First create the new directory,tx-jms-router/src/main/resources/tutorial
. Next, using your favorite text editor, create the file,activemq.xml
, under thetutorial
directory, and add the following text:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.3.4. Define a Route Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Overview Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
With the transactional JMS component,
jmstx
, you can define a transactional route written in Java DSL.
Note
It is also possible to define the route using Spring XML syntax.
Steps Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
To define a route using the transactional JMS component,
jmstx
, perform the following steps:
- Define a route in the Java domain specific language (DSL). Using your favorite text editor, open the file,
tx-jms-router/src/main/java/tutorial/MyRouteBuilder.java
. Edit the body of theMyRouteBuilder.configure()
method, discarding the existing route and replacing it with the following code:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Near the top of the
MyRouteBuilder.java
file, add the requisite import statements, as follows:// Java ... import org.apache.camel.Processor; import org.apache.camel.Exchange; ...
// Java ... import org.apache.camel.Processor; import org.apache.camel.Exchange; ...
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.3.5. Build and Run the Example Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Overview Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
After building and running the example using Maven, you can use JMX to examine what has happened to the JMS queues involved in the application.
Steps Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
To build and run the transactional JMS example, perform the following steps:
- To build the example, open a command prompt, change directory to
tx-jms-router
, and enter the following Maven command:mvn install
mvn install
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the build is successful, you should see the file,tx-jms-router-1.0-SNAPSHOT.jar
, appear under thetx-jms-router/target
directory. - To run the example using the
camel-maven-plugin
, enter the following Maven command:mvn camel:run
mvn camel:run
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If all goes well, you should see about a dozen occurrences ofjava.lang.Exception: test
scrolling past, before activity in the console window comes to a halt. Do not kill the running application at this point!NoteBut make sure that the exceptions you are seeing in the console do not indicate a failure to download and install thecamel-maven-plugin
. Normally, the plug-in should download and install without any problems, because the generated POM file,tx-jms-router/pom.xml
, contains all of the requisite settings. - 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
andqueue:debit
queues.To gain a better insight into what occurred, open a JMX console and point it at the ActiveMQ broker. Open a new command prompt and enter the following command:jconsole
jconsole
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThejconsole
utility is a standard tool provided with Sun's J2SE distribution (JDK). - To open a JMX connection to the ActiveMQ broker (which is embedded in the running example application), click on the Remote tab of the JConsole: Connect to Agent dialog and enter the Port number, 1099 (the default JMX port for ActiveMQ). Click Connect.NoteIt is possible to customize the JMX port used by ActiveMQ. See http://activemq.apache.org/jmx.html for details.
- If the connection succeeds, the JConsole window shows you a summary of the Virtual Machine (VM) instance that you are connected to. Click the MBeans tab and drill down to the
giro
queue, inTree/org.apache.activemq/broker1/Queue
.Notice that theEnqueueCount
,DispatchCount
, andDequeueCount
forgiro
are all equal to 2, which indicates that two messages entered the queue and two messages were pulled off the queue. - Click on the
debits
queue. Notice that theEnqueueCount
,DispatchCount
, andDequeueCount
fordebits
are all equal to 0. This is because thetest
exception caused the enqueued message to be rolled back each time an exchange passed through the route. The same thing happened to thecredits
queue. - Click on the
ActiveMQ.DLQ
queue. TheDLQ
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:- The consumer endpoint attempts to redeliver the message. Redelivery attempts can be repeated up to a configurable maximum number of times.
- 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 theActiveMQ.DLQ
queue that the number of enqueued messages,EnqueueCount
, is equal to 2. This is where the failed messages have ended up. - You can now kill the example application by typing Ctrl-C in its command window.