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 リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
Overview リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
The following are required to complete this example:
- Internet connection (required by Maven)
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:
- Set your
M2_HOMEenvironment variable to point to the Maven root directory. - Set your
MAVEN_OPTSenvironment variable to-Xmx512Mto increase the memory available for Maven builds. - Set your
PATHenvironment variable to include the Mavenbindirectory:Expand Platform Path Windows %M2_HOME%\binUNIX $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:
- 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 Each time you are prompted for input, press Enter to accept the default.This command generates a basic router application under thetx-jms-routerdirectory. You will customize this basic application to demonstrate transactions in Apache Camel.NoteMaven 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 the following properties:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add dependencies on the artifacts that implement Spring transactions. Look for the
dependencieselement in the POM file and add the followingdependencyelements:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add the JMS and ActiveMQ dependencies. Look for the
dependencieselement in the POM file and add the followingdependencyelements:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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:
- Customize the Spring XML configuration. Using your favorite text editor, open the
tx-jms-router/src/main/resources/META-INF/spring/camel-context.xmlfile and replace the content of thebeanselement with the following XML code:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - In the
jmsConnectionFactorybean from the preceding Spring XML code, customize the values of theuserNameandpasswordproperty settings with one of the user credentials from the JBoss Fuse container. By default, the container's user credentials are normally defined in theetc/users.propertiesfile.
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:
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:
- To build the example, open a command prompt, change directory to
tx-jms-router, and enter the following Maven command:mvn install
mvn installCopy 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/targetdirectory. - 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
InstallDir/src/dataCopy to Clipboard Copied! Toggle word wrap Toggle overflow In thedatadirectory create the file,message.txt, with the following contents:Test message.
Test message.Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Start up the JBoss Fuse container. Open a new command prompt and enter the following commands:
cd InstallDir/bin ./fuse
cd InstallDir/bin ./fuseCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 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
JBossFuse:karaf@root> install -s mvn:tutorial/tx-jms-router/1.0-SNAPSHOTCopy to Clipboard Copied! Toggle word wrap Toggle overflow - To see the result of running the routes, open the container log using the
log:displaycommand, as follows:JBossFuse:karaf@root> log:display
JBossFuse:karaf@root> log:displayCopy to Clipboard Copied! Toggle word wrap Toggle overflow If all goes well, you should see about a dozen occurrences ofjava.lang.Exception: testin the log. This is the expected behaviour. - 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:creditandqueue:debitqueues. - 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
http://localhost:8181/hawtioCopy to Clipboard Copied! Toggle word wrap Toggle overflow You will be prompted to log in. Use one of the credentials configured for your container (usually defined in theInstallDir/etc/users.propertiesfile). - Click on the ActiveMQ tab to explore the JMS queues that are accessed by the example routes.
- Drill down to the
giroqueue. Notice that theEnqueueCount,DispatchCount, andDequeueCountforgiroare all equal to 1, which indicates that one message entered the queue and one message was pulled off the queue. - Click on the
debitsqueue. Notice that theEnqueueCount,DispatchCount, andDequeueCountfordebitsare all equal to 0. This is because thetestexception caused the enqueued message to be rolled back each time an exchange passed through the route. The same thing happened to thecreditsqueue. - Click on the
ActiveMQ.DLQqueue. TheDLQpart 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.DLQqueue that the number of enqueued messages,EnqueueCount, is equal to 1. This is where the failed message has ended up.