[jboss-cvs] JBoss Messaging SVN: r6563 - in trunk: examples/jms and 8 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Apr 24 12:56:45 EDT 2009
Author: gaohoward
Date: 2009-04-24 12:56:45 -0400 (Fri, 24 Apr 2009)
New Revision: 6563
Added:
trunk/examples/jms/xa-with-jta/
trunk/examples/jms/xa-with-jta/build.xml
trunk/examples/jms/xa-with-jta/readme.html
trunk/examples/jms/xa-with-jta/server0/
trunk/examples/jms/xa-with-jta/server0/client-jndi.properties
trunk/examples/jms/xa-with-jta/server0/jbm-configuration.xml
trunk/examples/jms/xa-with-jta/server0/jbm-jms.xml
trunk/examples/jms/xa-with-jta/server0/jbm-queues.xml
trunk/examples/jms/xa-with-jta/server0/jbm-standalone-beans.xml
trunk/examples/jms/xa-with-jta/server0/jbm-users.xml
trunk/examples/jms/xa-with-jta/server0/ts/
trunk/examples/jms/xa-with-jta/src/
trunk/examples/jms/xa-with-jta/src/org/
trunk/examples/jms/xa-with-jta/src/org/jboss/
trunk/examples/jms/xa-with-jta/src/org/jboss/jms/
trunk/examples/jms/xa-with-jta/src/org/jboss/jms/example/
trunk/examples/jms/xa-with-jta/src/org/jboss/jms/example/XAwithJTAExample.java
Modified:
trunk/.classpath
trunk/examples/jms/common/build.xml
Log:
added xa-with-jta example
Modified: trunk/.classpath
===================================================================
--- trunk/.classpath 2009-04-24 16:42:20 UTC (rev 6562)
+++ trunk/.classpath 2009-04-24 16:56:45 UTC (rev 6563)
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/.svn/**/*" kind="src" path="src/main"/>
+ <classpathentry kind="src" path="examples/jms/xa-with-jta/src"/>
+ <classpathentry kind="src" path="examples/javaee/servlet-transport/src"/>
<classpathentry kind="src" path="build/src"/>
<classpathentry kind="src" path="tests/jms-tests/config"/>
<classpathentry kind="src" path="tests/config"/>
Modified: trunk/examples/jms/common/build.xml
===================================================================
--- trunk/examples/jms/common/build.xml 2009-04-24 16:42:20 UTC (rev 6562)
+++ trunk/examples/jms/common/build.xml 2009-04-24 16:56:45 UTC (rev 6563)
@@ -59,6 +59,9 @@
<property name="config.dir" value="${imported.basedir}/config"/>
<property name="example.config.dir" value="config"/>
+ <path id="extra.classpath">
+ </path>
+
<path id="jms.compilation.classpath">
<fileset dir="${jbm.jars.dir}">
<include name="**/*.jar"/>
@@ -67,6 +70,7 @@
<include name="**/*.jar"/>
<exclude name="**/*-sources.jar"/>
</fileset>
+ <path refid="extra.classpath"/>
</path>
<path id="jms.runtime.classpath">
@@ -74,7 +78,9 @@
<pathelement location="${config.dir}"/>
<pathelement location="${example.config.dir}"/>
<pathelement location="${classes.dir}"/>
+ <path refid="extra.classpath"/>
</path>
+
<target name="init">
<mkdir dir="${build.dir}"/>
<mkdir dir="${classes.dir}"/>
@@ -154,6 +160,7 @@
<delete dir="./server4/data" quiet="true" />
<delete dir="./server5/logs" quiet="true" />
<delete dir="./server5/data" quiet="true" />
+ <delete dir="./ObjectStore" quiet="true" />
</target>
<target name="all" description="Run all the JMS examples">
Added: trunk/examples/jms/xa-with-jta/build.xml
===================================================================
--- trunk/examples/jms/xa-with-jta/build.xml (rev 0)
+++ trunk/examples/jms/xa-with-jta/build.xml 2009-04-24 16:56:45 UTC (rev 6563)
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE project [
+ <!ENTITY libraries SYSTEM "../../../thirdparty/libraries.ent">
+ ]>
+
+<!-- =========================================================================================== -->
+<!-- -->
+<!-- JBoss, Home of Professional Open Source -->
+<!-- Copyright 2005, JBoss Inc., and individual contributors as indicated -->
+<!-- by the @authors tag. See the copyright.txt in the distribution for a -->
+<!-- full listing of individual contributors. -->
+<!-- -->
+<!-- This is free software; you can redistribute it and/or modify it -->
+<!-- under the terms of the GNU Lesser General Public License as -->
+<!-- published by the Free Software Foundation; either version 2.1 of -->
+<!-- the License, or (at your option) any later version. -->
+<!-- -->
+<!-- This software is distributed in the hope that it will be useful, -->
+<!-- but WITHOUT ANY WARRANTY; without even the implied warranty of -->
+<!-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -->
+<!-- Lesser General Public License for more details. -->
+<!-- -->
+<!-- You should have received a copy of the GNU Lesser General Public -->
+<!-- License along with this software; if not, write to the Free -->
+<!-- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA -->
+<!-- 02110-1301 USA, or see the FSF site: http://www.fsf.org. -->
+<!-- -->
+<!-- =========================================================================================== -->
+
+
+<project default="run" name="JBoss Messaging JMS XA with JTA Example">
+
+ <import file="../common/build.xml"/>
+
+ <path id="extra.classpath">
+ <fileset dir="build/jta">
+ <include name="**/*.jar"/>
+ </fileset>
+ </path>
+
+ <target name="download-jta">
+ <mkdir dir="build/jta"/>
+ <get usetimestamp="true" src="http://repository.jboss.com/jboss/jbossts/4.6.0.CR1/lib/jbossjta.jar" dest="build/jta/jbossjta.jar"/>
+ <get usetimestamp="true" src="http://repository.jboss.com/jboss/jbossts/4.6.0.CR1/lib/jbossts-common.jar" dest="build/jta/jbossts-common.jar"/>
+ </target>
+
+ <target name="run" depends="download-jta">
+ <antcall target="runExample">
+ <param name="example.classname" value="org.jboss.jms.example.XAwithJTAExample"/>
+ </antcall>
+ </target>
+
+ <target name="runRemote">
+ <antcall target="runExample">
+ <param name="example.classname" value="org.jboss.jms.example.XAwithJTAExample"/>
+ <param name="jbm.example.runServer" value="false"/>
+ </antcall>
+ </target>
+
+</project>
Added: trunk/examples/jms/xa-with-jta/readme.html
===================================================================
--- trunk/examples/jms/xa-with-jta/readme.html (rev 0)
+++ trunk/examples/jms/xa-with-jta/readme.html 2009-04-24 16:56:45 UTC (rev 6563)
@@ -0,0 +1,204 @@
+<html>
+ <head>
+ <title>JBoss Messaging JMS XA with JTA Example</title>
+ <link rel="stylesheet" type="text/css" href="../common/common.css">
+ </head>
+ <body>
+ <h1>JMS XA with JTA Example</h1>
+ <br>
+ <p>This example shows you how to use JTA interfaces to control transactions with JBoss Messaging. JTA provides
+ facilities to start and stop a transaction, enlist XA resources into a transaction.</p>
+
+ <p>JBoss Messaging is JTA aware, meaning you can use JBoss Messaging in a XA transactional environment
+ and participate in XA transactions. It provides the javax.transaction.xa.XAResource interface for that
+ purpose. Users can get a XAConnectionFactory to create XAConnections and XASessions.</p>
+
+ <p>In this example we get a transaction manager from JBoss JTA to control the transactions. First we create an XASession
+ for receiiving and a normal session for sending. Then it starts a new xa transaction and enlist the receiving
+ XASession through its XAResource. We then send two words, 'hello' and 'world', receive them, and let the
+ transaction roll back. The received messages are cancelled back to the queue. Next we start
+ a new transaction with the same XAResource enlisted, but this time we commit the transaction after receiving the
+ messages. Then we check that no more messages are to be received.</p>
+ <br>
+ <h2>Example step-by-step</h2>
+ <p><i>To run the example, simply type <code>ant</code> from this directory. It will download the JBoss JTA jars before
+ it launches the example.</i></p>
+ <br>
+ <ol>
+ <li>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the <code>client-jndi.properties</code> file in the directory <code>../common/config</code></li>
+ <pre>
+ <code>InitialContext initialContext = getContext(0);</code>
+ </pre>
+
+ <li>We look-up the JMS queue object from JNDI</li>
+ <pre>
+ <code>Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");</code>
+ </pre>
+
+ <li>We perform a lookup on the XA Connection Factory</li>
+ <pre>
+ <code>XAConnectionFactory cf = (XAConnectionFactory) initialContext.lookup("/XAConnectionFactory");</code>
+ </pre>
+
+ <li>We create a JMS XAConnection</li>
+ <pre>
+ <code>connection = cf.createXAConnection();</code>
+ </pre>
+
+ <li>We Start the connection</li>
+ <pre>
+ <code>connection.start();</code>
+ </pre>
+
+ <li>We create a JMS XASession</li>
+ <pre>
+ <code>XASession xaSession = connection.createXASession();</code>
+ </pre>
+
+ <li>We create a normal session</li>
+ <pre>
+ <code>Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
+ </pre>
+
+ <li>We create a normal Message Producer</li>
+ <pre>
+ <code>
+ MessageProducer normalProducer = normalSession.createProducer(queue);
+ </code>
+ </pre>
+
+ <li>We get the JMS Session</li>
+ <pre>
+ <code>Session session = xaSession.getSession();</code>
+ </pre>
+
+ <li>We create a message consumer</li>
+ <pre>
+ <code>MessageConsumer xaConsumer = session.createConsumer(queue); </code>
+ </pre>
+
+ <li>We create two Text Messages</li>
+ <pre>
+ <code>
+ TextMessage helloMessage = session.createTextMessage("hello");
+ TextMessage worldMessage = session.createTextMessage("world");
+ </code>
+ </pre>
+
+ <li>We get the Transaction Manager</li>
+ <pre>
+ <code>javax.transaction.TransactionManager txMgr = TransactionManager.transactionManager();</code>
+ </pre>
+
+ <li>We start a transaction</li>
+ <pre>
+ <code>txMgr.begin();</code>
+ </pre>
+
+ <li>We get the JMS XAResource</li>
+ <pre>
+ <code>XAResource xaRes = xaSession.getXAResource();</code>
+ </pre>
+
+ <li>We enlist the resource in the Transaction work</li>
+ <pre>
+ <code>
+ Transaction transaction = txMgr.getTransaction();
+ transaction.enlistResource(xaRes);
+ </code>
+ </pre>
+
+ <li>We send two messages.</li>
+ <pre>
+ <code>
+ normalProducer.send(helloMessage);
+ normalProducer.send(worldMessage);
+ </code>
+ </pre>
+
+ <li>We receive the messages</li>
+ <pre>
+ <code>
+ TextMessage rm1 = (TextMessage)xaConsumer.receive();
+ System.out.println("Message received: " + rm1.getText());
+ TextMessage rm2 = (TextMessage)xaConsumer.receive();
+ System.out.println("Message received: " + rm2.getText());
+ </code>
+ </pre>
+
+ <li>We stop the work</li>
+ <pre>
+ <code>transaction.delistResource(xaRes, XAResource.TMSUCCESS);</code>
+ </pre>
+
+ <li>We roll back the transaction</li>
+ <pre>
+ <code>txMgr.rollback();</code>
+ </pre>
+
+ <li>We create another transaction </li>
+ <pre>
+ <code>
+ txMgr.begin();
+ transaction = txMgr.getTransaction();
+ </code>
+ </pre>
+
+ <li>We enlist the resource to start the transaction work</li>
+ <pre>
+ <code>transaction.enlistResource(xaRes);</code>
+ </pre>
+
+ <li>We receive those messages again</li>
+ <pre>
+ <code>
+ rm1 = (TextMessage)xaConsumer.receive();
+ System.out.println("Message received again: " + rm1.getText());
+ rm2 = (TextMessage)xaConsumer.receive();
+ System.out.println("Message received again: " + rm2.getText());
+ </code>
+ </pre>
+
+ <li>We stop the work</li>
+ <pre>
+ <code>transaction.delistResource(xaRes, XAResource.TMSUCCESS);</code>
+ </pre>
+
+ <li>We commit</li>
+ <pre>
+ <code>txMgr.commit();</code>
+ </pre>
+
+ <li>We check that no more messages are received.</li>
+ <pre>
+ <code>
+ TextMessage rm3 = (TextMessage)xaConsumer.receive(2000);
+ if (rm3 == null)
+ {
+ System.out.println("No message received after commit.");
+ }
+ else
+ {
+ result = false;
+ }
+ </code>
+ </pre>
+
+ <li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
+
+ <pre>
+ <code>finally
+ {
+ if (initialContext != null)
+ {
+ initialContext.close();
+ }
+ if (connection != null)
+ {
+ connection.close();
+ }
+ }</code>
+ </pre>
+ </ol>
+ </body>
+</html>
Added: trunk/examples/jms/xa-with-jta/server0/client-jndi.properties
===================================================================
--- trunk/examples/jms/xa-with-jta/server0/client-jndi.properties (rev 0)
+++ trunk/examples/jms/xa-with-jta/server0/client-jndi.properties 2009-04-24 16:56:45 UTC (rev 6563)
@@ -0,0 +1,3 @@
+java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
+java.naming.provider.url=jnp://localhost:1099
+java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
Added: trunk/examples/jms/xa-with-jta/server0/jbm-configuration.xml
===================================================================
--- trunk/examples/jms/xa-with-jta/server0/jbm-configuration.xml (rev 0)
+++ trunk/examples/jms/xa-with-jta/server0/jbm-configuration.xml 2009-04-24 16:56:45 UTC (rev 6563)
@@ -0,0 +1,28 @@
+<deployment xmlns="urn:jboss:messaging"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-configuration.xsd">
+ <configuration>
+
+ <!-- Connectors -->
+ <connectors>
+ <connector name="netty-connector">
+ <factory-class>org.jboss.messaging.integration.transports.netty.NettyConnectorFactory</factory-class>
+ <param key="jbm.remoting.netty.port" value="5445" type="Integer"/>
+ </connector>
+ </connectors>
+
+ <!-- Acceptors -->
+ <acceptors>
+ <acceptor name="netty-acceptor">
+ <factory-class>org.jboss.messaging.integration.transports.netty.NettyAcceptorFactory</factory-class>
+ <param key="jbm.remoting.netty.port" value="5445" type="Integer"/>
+ </acceptor>
+ </acceptors>
+
+ <!-- Other config -->
+
+ <journal-min-files>2</journal-min-files>
+
+ </configuration>
+
+</deployment>
Added: trunk/examples/jms/xa-with-jta/server0/jbm-jms.xml
===================================================================
--- trunk/examples/jms/xa-with-jta/server0/jbm-jms.xml (rev 0)
+++ trunk/examples/jms/xa-with-jta/server0/jbm-jms.xml 2009-04-24 16:56:45 UTC (rev 6563)
@@ -0,0 +1,20 @@
+<deployment xmlns="urn:jboss:messaging"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-jms.xsd ">
+ <!--the connection factory used by the example-->
+ <connection-factory name="ConnectionFactory">
+ <connector-ref connector-name="netty-connector"/>
+ <entries>
+ <entry name="ConnectionFactory"/>
+ <entry name="XAConnectionFactory"/>
+ <entry name="java:/ConnectionFactory"/>
+ <entry name="java:/XAConnectionFactory"/>
+ </entries>
+ </connection-factory>
+
+ <!--the queue used by the example-->
+ <queue name="exampleQueue">
+ <entry name="/queue/exampleQueue"/>
+ </queue>
+
+</deployment>
\ No newline at end of file
Added: trunk/examples/jms/xa-with-jta/server0/jbm-queues.xml
===================================================================
--- trunk/examples/jms/xa-with-jta/server0/jbm-queues.xml (rev 0)
+++ trunk/examples/jms/xa-with-jta/server0/jbm-queues.xml 2009-04-24 16:56:45 UTC (rev 6563)
@@ -0,0 +1,15 @@
+<settings xmlns="urn:jboss:messaging"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-queues.xsd ">
+
+ <!--security for example queue-->
+ <security match="jms.queue.exampleQueue">
+ <permission type="createDurableQueue" roles="guest"/>
+ <permission type="deleteDurableQueue" roles="guest"/>
+ <permission type="createTempQueue" roles="guest"/>
+ <permission type="deleteTempQueue" roles="guest"/>
+ <permission type="consume" roles="guest"/>
+ <permission type="send" roles="guest"/>
+ </security>
+
+</settings>
Added: trunk/examples/jms/xa-with-jta/server0/jbm-standalone-beans.xml
===================================================================
--- trunk/examples/jms/xa-with-jta/server0/jbm-standalone-beans.xml (rev 0)
+++ trunk/examples/jms/xa-with-jta/server0/jbm-standalone-beans.xml 2009-04-24 16:56:45 UTC (rev 6563)
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<deployment xmlns="urn:jboss:bean-deployer:2.0">
+
+ <bean name="Naming" class="org.jnp.server.NamingBeanImpl"/>
+
+ <!-- JNDI server. Disable this if you don't want JNDI -->
+ <bean name="JNDIServer" class="org.jnp.server.Main">
+ <property name="namingInfo">
+ <inject bean="Naming"/>
+ </property>
+ <property name="port">1099</property>
+ <property name="bindAddress">localhost</property>
+ <property name="rmiPort">1098</property>
+ <property name="rmiBindAddress">localhost</property>
+ </bean>
+
+ <!-- MBean server -->
+ <bean name="MBeanServer" class="javax.management.MBeanServer">
+ <constructor factoryClass="java.lang.management.ManagementFactory"
+ factoryMethod="getPlatformMBeanServer"/>
+ </bean>
+
+ <!-- The core configuration -->
+ <bean name="Configuration" class="org.jboss.messaging.core.config.impl.FileConfiguration"/>
+
+ <!-- The security manager -->
+ <bean name="JBMSecurityManager" class="org.jboss.messaging.core.security.impl.JBMSecurityManagerImpl">
+ <start ignored="true"/>
+ <stop ignored="true"/>
+ </bean>
+
+ <!-- The core server -->
+ <bean name="MessagingServer" class="org.jboss.messaging.core.server.impl.MessagingServerImpl">
+ <start ignored="true"/>
+ <stop ignored="true"/>
+ <constructor>
+ <parameter>
+ <inject bean="Configuration"/>
+ </parameter>
+ <parameter>
+ <inject bean="MBeanServer"/>
+ </parameter>
+ <parameter>
+ <inject bean="JBMSecurityManager"/>
+ </parameter>
+ </constructor>
+ </bean>
+
+ <!-- The JMS server -->
+ <bean name="JMSServerManager" class="org.jboss.messaging.jms.server.impl.JMSServerManagerImpl">
+ <constructor>
+ <parameter>
+ <inject bean="MessagingServer"/>
+ </parameter>
+ </constructor>
+ </bean>
+
+</deployment>
Added: trunk/examples/jms/xa-with-jta/server0/jbm-users.xml
===================================================================
--- trunk/examples/jms/xa-with-jta/server0/jbm-users.xml (rev 0)
+++ trunk/examples/jms/xa-with-jta/server0/jbm-users.xml 2009-04-24 16:56:45 UTC (rev 6563)
@@ -0,0 +1,7 @@
+<deployment xmlns="urn:jboss:messaging" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-users.xsd ">
+ <!-- the default user. this is used where username is null-->
+ <defaultuser name="guest" password="guest">
+ <role name="guest"/>
+ </defaultuser>
+</deployment>
\ No newline at end of file
Added: trunk/examples/jms/xa-with-jta/src/org/jboss/jms/example/XAwithJTAExample.java
===================================================================
--- trunk/examples/jms/xa-with-jta/src/org/jboss/jms/example/XAwithJTAExample.java (rev 0)
+++ trunk/examples/jms/xa-with-jta/src/org/jboss/jms/example/XAwithJTAExample.java 2009-04-24 16:56:45 UTC (rev 6563)
@@ -0,0 +1,167 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jms.example;
+
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.jms.XAConnection;
+import javax.jms.XAConnectionFactory;
+import javax.jms.XASession;
+import javax.naming.InitialContext;
+import javax.transaction.Transaction;
+import javax.transaction.xa.XAResource;
+
+import com.arjuna.ats.jta.TransactionManager;
+
+/**
+ * A simple JMS example showing the JBoss Messaging XA support with JTA.
+ *
+ * @author <a href="hgao at redhat.com">Howard Gao</a>
+ */
+public class XAwithJTAExample extends JMSExample
+{
+ private volatile boolean result = true;
+
+ public static void main(String[] args)
+ {
+ new XAwithJTAExample().run(args);
+ }
+
+ public boolean runExample() throws Exception
+ {
+ XAConnection connection = null;
+ InitialContext initialContext = null;
+ try
+ {
+ //Step 1. Create an initial context to perform the JNDI lookup.
+ initialContext = getContext(0);
+
+ //Step 2. Lookup on the queue
+ Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
+
+ //Step 3. Perform a lookup on the XA Connection Factory
+ XAConnectionFactory cf = (XAConnectionFactory) initialContext.lookup("/XAConnectionFactory");
+
+ //Step 4.Create a JMS XAConnection
+ connection = cf.createXAConnection();
+
+ //Step 5. Start the connection
+ connection.start();
+
+ //Step 6. Create a JMS XASession
+ XASession xaSession = connection.createXASession();
+
+ //Step 7. Create a normal session
+ Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ //Step 8. Create a normal Message Producer
+ MessageProducer normalProducer = normalSession.createProducer(queue);
+
+ //Step 9. Get the JMS Session
+ Session session = xaSession.getSession();
+
+ //Step 10. Create a message consumer
+ MessageConsumer xaConsumer = session.createConsumer(queue);
+
+ //Step 11. Create two Text Messages
+ TextMessage helloMessage = session.createTextMessage("hello");
+ TextMessage worldMessage = session.createTextMessage("world");
+
+ //Step 12. Get the Transaction Manager
+ javax.transaction.TransactionManager txMgr = TransactionManager.transactionManager();
+
+ //Step 13. Start a transaction
+ txMgr.begin();
+
+ //Step 14. Get the JMS XAResource
+ XAResource xaRes = xaSession.getXAResource();
+
+ //Step 15. enlist the resource in the Transaction work
+ Transaction transaction = txMgr.getTransaction();
+ transaction.enlistResource(xaRes);
+
+ //Step 16. Send two messages.
+ normalProducer.send(helloMessage);
+ normalProducer.send(worldMessage);
+
+ //Step 17. Receive the message
+ TextMessage rm1 = (TextMessage)xaConsumer.receive();
+ System.out.println("Message received: " + rm1.getText());
+ TextMessage rm2 = (TextMessage)xaConsumer.receive();
+ System.out.println("Message received: " + rm2.getText());
+
+ //Step 18. Stop the work
+ transaction.delistResource(xaRes, XAResource.TMSUCCESS);
+
+ //Step 19. Roll back the transaction
+ txMgr.rollback();
+
+ //Step 20. Create another transaction
+ txMgr.begin();
+ transaction = txMgr.getTransaction();
+
+ //Step 21. Enlist the resource to start the transaction work
+ transaction.enlistResource(xaRes);
+
+ //Step 22. receive those messages again
+ rm1 = (TextMessage)xaConsumer.receive();
+ System.out.println("Message received again: " + rm1.getText());
+ rm2 = (TextMessage)xaConsumer.receive();
+ System.out.println("Message received again: " + rm2.getText());
+
+ //Step 23. Stop the work
+ transaction.delistResource(xaRes, XAResource.TMSUCCESS);
+
+ //Step 24. Commit!
+ txMgr.commit();
+
+ //Step 25. Check no more messages are received.
+ TextMessage rm3 = (TextMessage)xaConsumer.receive(2000);
+ if (rm3 == null)
+ {
+ System.out.println("No message received after commit.");
+ }
+ else
+ {
+ result = false;
+ }
+
+ return result;
+ }
+ finally
+ {
+ //Step 26. Be sure to close our JMS resources!
+ if (initialContext != null)
+ {
+ initialContext.close();
+ }
+ if(connection != null)
+ {
+ connection.close();
+ }
+ }
+ }
+
+}
More information about the jboss-cvs-commits
mailing list