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.10.4. Define a Transactional Route
Overview Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
This section describes how to create a transactional route and package it as an OSGi bundle. The route described here is based on the
AccountService
class (see Appendix A), implementing a transfer of funds from one account to another, where the account data is stored in an Apache Derby database instance.
Database schema Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The database schema for the accounts consists of just two columns: the
name
column (identifying the account holder) and the amount
column (specifying the amount of money left in the account). Formally, the schema is defined by the following SQL command:
CREATE TABLE accounts (name VARCHAR(50), amount INT);
CREATE TABLE accounts (name VARCHAR(50), amount INT);
Sample incoming message Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The following XML snippet demonstrates the format of a typical message that is processed by the route:
The message requests a transfer of money from one account to another. It specifies that 90 units should be subtracted from the
Major Clanger
account and 90 units should be added to the Tiny Clanger
account.
The transactional route Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The incoming messages are processed by the following transactional route:
Money is transferred by calling the
AccountService.credit
and AccountService.debit
bean methods (which access the Derby database). The AccountService.dumpTable
method then dumps the complete contents of the database table into the current exchange and the route sends this to the statusLog
queue.
Provoking a transaction rollback Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The
AccountService.debit
method imposes a limit of 100 on the amount that can be withdrawn from any account and throws an exception if this limit is exceeded. This provides a simple means of provoking a transaction rollback, by sending a message containing a transfer request that exceeds 100.
Steps to define a transactional route Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Perform the following steps to define a route that uses XA to coordinate global transactions across a JMS XA resource and an Apache Derby XA resource:
- Use the quickstart archetype to create a basic Maven project for the route bundle. Open a new command prompt, change directory to a convenient location, and enter the following command:
mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=org.fusesource.example -DartifactId=tx-xa
mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=org.fusesource.example -DartifactId=tx-xa
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The preceding command creates a new Maven project for theorg.fusesource.example/tx-xa
artifact under thetx-xa
directory. - Change the project packaging type to
bundle
. Under thetx-xa
directory, open thepom.xml
file with a text editor and change the contents of thepackaging
element fromjar
tobundle
, as shown in the following highlighted line:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add the bundle configuration to the POM. In the
pom.xml
file, add the followingbuild
element as a child of theproject
element:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Customize the Maven compiler plug-in to enforce JDK 1.6 coding syntax. In the
pom.xml
file, add the followingplugin
element as a child of theplugins
element, to configure the Maven compiler plug-in:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add the requisite dependencies to the POM. In the
pom.xml
file, add the following elements as children of theproject
element:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Customize the dependency versions as required (by editing the properties defined in theproperties
element). - Define the
AccountService
class. Under thetx-xa
project directory, create the following directory:src/main/java/org/fusesource/example/tx/xa
src/main/java/org/fusesource/example/tx/xa
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the file,AccountService.java
, in this directory and add the contents of the listing from Example A.1, “The AccountService Class” to this file. - Define the beans and resources needed by the route in a Spring XML file. Under the
tx-xa
project directory, create the following directory:src/main/resources/META-INF/spring
src/main/resources/META-INF/spring
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Using a text editor, create the file,beans.xml
, in this directory and add the following contents to the file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Define the transactional route. In the
src/main/resources/META-INF/spring
directory, create the new file,camelContext.xml
, and add the following contents:Copy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantReplace PathNameToMsgDir with the absolute path name of a temporary directory. When the application is running, you will use this directory as a convenient way of feeding XML messages into the route. - To build the
tx-xa
bundle and install it in the local Maven repository, enter the following Maven command from thetx-xa
directory:mvn install
mvn install
Copy to Clipboard Copied! Toggle word wrap Toggle overflow