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 リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
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 リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
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 リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
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 リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
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 リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
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 リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
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:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=org.fusesource.example -DartifactId=tx-xa
mvn archetype:generate -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.7 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 JBoss Fuse Bill of Materials (BOM) as the parent POM. The JBoss Fuse BOM defines version properties (for example,
camel-version
,spring-version
, and so on) for all of the JBoss Fuse components, which makes it easy to specify the correct versions for the Maven dependencies. Add the followingparent
element near the top of your POM and (if necessary) customize the version of the BOM:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add the required Maven dependencies to the POM and specify the
derby-version
property. In thepom.xml
file, add the following elements as children of theproject
element:Copy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantRemember to customize thederby-version
property to the version of Derby you are using. - 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 B.1, “The AccountService Class” to this file. - Define the beans and resources needed by the route in a Blueprint XML file. Under the
tx-xa
project directory, create the following directory:src/main/resources/OSGI-INF/blueprint
src/main/resources/OSGI-INF/blueprint
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 ImportantIn thejmsConnectionFactory
bean and in thejmsXaConnectionFactory
bean, you must customize the JAAS user credentials,UserName
andPassword
, that are used to log into the broker. You can use any JAAS user with theadmin
role (usually defined in theetc/users.properties
file of your JBoss Fuse installation). - Define the transactional route. In the
src/main/resources/OSGI-INF/blueprint
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