[jboss-cvs] JBoss Messaging SVN: r6397 - in trunk: examples/jms and 7 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Apr 10 06:08:55 EDT 2009


Author: gaohoward
Date: 2009-04-10 06:08:54 -0400 (Fri, 10 Apr 2009)
New Revision: 6397

Added:
   trunk/examples/jms/xa-transaction/
   trunk/examples/jms/xa-transaction/build.xml
   trunk/examples/jms/xa-transaction/readme.html
   trunk/examples/jms/xa-transaction/server0/
   trunk/examples/jms/xa-transaction/server0/client-jndi.properties
   trunk/examples/jms/xa-transaction/server0/jbm-configuration.xml
   trunk/examples/jms/xa-transaction/server0/jbm-jms.xml
   trunk/examples/jms/xa-transaction/server0/jbm-queues.xml
   trunk/examples/jms/xa-transaction/server0/jbm-security.xml
   trunk/examples/jms/xa-transaction/server0/jbm-standalone-beans.xml
   trunk/examples/jms/xa-transaction/src/
   trunk/examples/jms/xa-transaction/src/org/
   trunk/examples/jms/xa-transaction/src/org/jboss/
   trunk/examples/jms/xa-transaction/src/org/jboss/jms/
   trunk/examples/jms/xa-transaction/src/org/jboss/jms/example/
   trunk/examples/jms/xa-transaction/src/org/jboss/jms/example/XATransactionExample.java
Modified:
   trunk/.classpath
Log:
temporarily commit xa-transaction example



Modified: trunk/.classpath
===================================================================
--- trunk/.classpath	2009-04-10 10:06:21 UTC (rev 6396)
+++ trunk/.classpath	2009-04-10 10:08:54 UTC (rev 6397)
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
 	<classpathentry excluding="**/.svn/**/*" kind="src" path="src/main"/>
+	<classpathentry kind="src" path="examples/jms/xa-transaction/src"/>
 	<classpathentry kind="src" path="build/src"/>
 	<classpathentry kind="src" path="tests/jms-tests/config"/>
 	<classpathentry kind="src" path="tests/config"/>

Added: trunk/examples/jms/xa-transaction/build.xml
===================================================================
--- trunk/examples/jms/xa-transaction/build.xml	                        (rev 0)
+++ trunk/examples/jms/xa-transaction/build.xml	2009-04-10 10:08:54 UTC (rev 6397)
@@ -0,0 +1,48 @@
+<?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 Queue Example">
+
+   <import file="../common/build.xml"/>
+
+   <target name="run">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.XATransactionExample"/>
+      </antcall>
+   </target>
+
+   <target name="runRemote">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.XATransactionExample"/>
+         <param name="jbm.example.runServer" value="false"/>
+      </antcall>
+   </target>
+
+</project>
\ No newline at end of file

Added: trunk/examples/jms/xa-transaction/readme.html
===================================================================
--- trunk/examples/jms/xa-transaction/readme.html	                        (rev 0)
+++ trunk/examples/jms/xa-transaction/readme.html	2009-04-10 10:08:54 UTC (rev 6397)
@@ -0,0 +1,99 @@
+<html>
+  <head>
+    <title>JBoss Messaging JMS XA Transaction Example</title>
+    <link rel="stylesheet" type="text/css" href="../common/common.css">
+  </head>
+  <body>
+     <h1>JMS XA Transaction Example</h1>
+     <br>
+     <p>This example shows you the XA support in JBoss Messaging.</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 simulate a transaction manager to control the transactions. First we start a 
+     transaction and enlist two XAResources. We will send two words, 'Hello' and 'World', and let the transaction
+     guarantee both words are sent. Then we start another transaction to try to receive "Hello" and "World". We 
+     let the transaction guarantee that the two words are received.</p>
+     <br>
+     <h2>Example step-by-step</h2>
+     <p><i>To run the example, simply type <code>ant</code> from this directory</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();</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 look-up the JMS connection factory object from JNDI</li>
+        <pre>
+           <code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code>
+        </pre>
+
+        <li>We create a JMS connection</li>
+        <pre>
+           <code>connection = cf.createConnection();</code>
+        </pre>
+
+        <li>We create a JMS session. The session is created as non transacted and will auto acknowledge messages.</li>
+        <pre>
+           <code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
+        </pre>
+
+        <li>We create a JMS message producer on the session. This will be used to send the messages.</li>
+        <pre>
+          <code>MessageProducer messageProducer = session.createProducer(topic);</code>
+       </pre>
+
+        <li>We create a JMS text message that we are going to send.</li>
+        <pre>
+           <code>TextMessage message = session.createTextMessage("This is a text message");</code>
+        </pre>
+
+        <li>We send message to the queue</li>
+        <pre>
+           <code>messageProducer.send(message);</code>
+        </pre>
+
+        <li>We create a JMS Message Consumer to receive the message.</li>
+          <pre>
+           <code>MessageConsumer messageConsumer = session.createConsumer(queue);</code>
+        </pre>
+
+        <li>We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started</li>
+        <pre>
+           <code>connection.start();</code>
+        </pre>
+
+        <li>The message arrives at the consumer. In this case we use a timeout of 5000 milliseconds but we could use a blocking 'receive()'</li>
+        <pre>
+           <code>TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);</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>
\ No newline at end of file

Added: trunk/examples/jms/xa-transaction/server0/client-jndi.properties
===================================================================
--- trunk/examples/jms/xa-transaction/server0/client-jndi.properties	                        (rev 0)
+++ trunk/examples/jms/xa-transaction/server0/client-jndi.properties	2009-04-10 10:08:54 UTC (rev 6397)
@@ -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-transaction/server0/jbm-configuration.xml
===================================================================
--- trunk/examples/jms/xa-transaction/server0/jbm-configuration.xml	                        (rev 0)
+++ trunk/examples/jms/xa-transaction/server0/jbm-configuration.xml	2009-04-10 10:08:54 UTC (rev 6397)
@@ -0,0 +1,27 @@
+<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 -->
+
+      <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>
+
+      <!-- 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>
+
+
+      <!-- Other config -->
+
+      <journal-min-files>2</journal-min-files>
+
+   </configuration>
+
+</deployment>

Added: trunk/examples/jms/xa-transaction/server0/jbm-jms.xml
===================================================================
--- trunk/examples/jms/xa-transaction/server0/jbm-jms.xml	                        (rev 0)
+++ trunk/examples/jms/xa-transaction/server0/jbm-jms.xml	2009-04-10 10:08:54 UTC (rev 6397)
@@ -0,0 +1,18 @@
+<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"/>
+      <entry name="ConnectionFactory"/>
+      <entry name="XAConnectionFactory"/>
+      <entry name="java:/ConnectionFactory"/>
+      <entry name="java:/XAConnectionFactory"/>
+   </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-transaction/server0/jbm-queues.xml
===================================================================
--- trunk/examples/jms/xa-transaction/server0/jbm-queues.xml	                        (rev 0)
+++ trunk/examples/jms/xa-transaction/server0/jbm-queues.xml	2009-04-10 10:08:54 UTC (rev 6397)
@@ -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-transaction/server0/jbm-security.xml
===================================================================
--- trunk/examples/jms/xa-transaction/server0/jbm-security.xml	                        (rev 0)
+++ trunk/examples/jms/xa-transaction/server0/jbm-security.xml	2009-04-10 10:08:54 UTC (rev 6397)
@@ -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-security.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-transaction/server0/jbm-standalone-beans.xml
===================================================================
--- trunk/examples/jms/xa-transaction/server0/jbm-standalone-beans.xml	                        (rev 0)
+++ trunk/examples/jms/xa-transaction/server0/jbm-standalone-beans.xml	2009-04-10 10:08:54 UTC (rev 6397)
@@ -0,0 +1,165 @@
+<?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>
+
+   <bean name="DeploymentManager" class="org.jboss.messaging.core.deployers.impl.FileDeploymentManager">
+      <constructor>
+         <!-- The scan time in milliseconds -->
+         <parameter>5000</parameter>
+      </constructor>
+   </bean>
+   
+   <bean name="Configuration" class="org.jboss.messaging.core.config.impl.FileConfiguration"/>
+
+   <!--<bean name="JBMSecurityManager" class="org.jboss.messaging.core.security.impl.JBossASSecurityManager"/>-->
+
+   <bean name="JBMSecurityManager" class="org.jboss.messaging.core.security.impl.JBMSecurityManagerImpl">
+   </bean>
+
+   <bean name="BasicSecurityDeployer" class="org.jboss.messaging.core.deployers.impl.BasicSecurityDeployer">
+      <constructor>
+         <parameter>
+            <inject bean="DeploymentManager"/>
+         </parameter>
+      </constructor>
+      <property name="jbmSecurityManager">
+         <inject bean="JBMSecurityManager"/>
+      </property>
+   </bean>
+
+   <bean name="MessagingServer" class="org.jboss.messaging.core.server.impl.MessagingServerImpl">
+      <depends>AddressSettingsDeployer</depends>
+      <depends>QueueDeployer</depends>
+      
+      <property name="storageManager">
+         <inject bean="StorageManager"/>
+      </property>
+      <property name="remotingService">
+         <inject bean="RemotingService"/>
+      </property>
+      <property name="configuration">
+         <inject bean="Configuration"/>
+      </property>
+      <property name="securityManager">
+         <inject bean="JBMSecurityManager"/>
+      </property>
+      <property name="managementService">
+         <inject bean="ManagementService"/>
+      </property>      
+   </bean>
+
+   <bean name="StorageManager" class="org.jboss.messaging.core.persistence.impl.journal.JournalStorageManager">
+      <constructor>
+         <parameter>
+            <inject bean="Configuration"/>
+         </parameter>
+      </constructor>
+   </bean>
+
+   <bean name="RemotingService" class="org.jboss.messaging.core.remoting.server.impl.RemotingServiceImpl">
+      <constructor>
+         <parameter>
+            <inject bean="Configuration"/>
+         </parameter>
+      </constructor>
+      <property name="managementService">
+         <inject bean="ManagementService"/>
+      </property>
+   </bean>
+   
+   <bean name="JMSServerManager" class="org.jboss.messaging.jms.server.impl.JMSServerManagerImpl">
+      <constructor>
+         <parameter>
+            <inject bean="MessagingServer" property="serverManagement"/>
+         </parameter>
+         <parameter>
+            <inject bean="JMSManagementService"/>
+         </parameter>
+      </constructor>
+   </bean>
+
+   <bean name="JMSServerDeployer" class="org.jboss.messaging.jms.server.impl.JMSServerDeployer">
+      <constructor>
+         <parameter>
+            <inject bean="DeploymentManager"/>
+         </parameter>
+          <parameter>
+            <inject bean="Configuration"/>
+         </parameter>
+      </constructor>
+      <property name="jmsServerManager">
+         <inject bean="JMSServerManager"/>
+      </property>
+   </bean>
+
+   <bean name="AddressSettingsDeployer" class="org.jboss.messaging.core.deployers.impl.AddressSettingsDeployer">
+      <constructor>
+         <parameter>
+            <inject bean="DeploymentManager"/>
+         </parameter>
+         <parameter>
+            <inject bean="MessagingServer" property="addressSettingsRepository" state="Configured"/>
+         </parameter>
+      </constructor>
+   </bean>
+
+   <bean name="QueueDeployer" class="org.jboss.messaging.core.deployers.impl.QueueDeployer">
+      <constructor>
+         <parameter>
+            <inject bean="DeploymentManager"/>
+         </parameter>
+         <parameter>
+            <inject bean="Configuration"/>
+         </parameter>
+      </constructor>
+   </bean>
+   
+   <bean name="SecurityDeployer" class="org.jboss.messaging.core.deployers.impl.SecurityDeployer">
+      <constructor>
+         <parameter>
+            <inject bean="DeploymentManager"/>
+         </parameter>
+         <parameter>
+            <inject bean="MessagingServer" property="securityRepository"/>
+         </parameter>
+      </constructor>
+   </bean>
+
+   <bean name="ManagementService" class="org.jboss.messaging.core.management.impl.ManagementServiceImpl">
+      <constructor>
+         <parameter>
+            <inject bean="MBeanServer"/>
+         </parameter>
+         <parameter>
+            <inject bean="Configuration" property="JMXManagementEnabled" />
+         </parameter>
+      </constructor>
+   </bean>
+   
+   <bean name="JMSManagementService" class="org.jboss.messaging.jms.server.management.impl.JMSManagementServiceImpl">
+      <constructor>
+         <parameter>
+            <inject bean="ManagementService"/>
+         </parameter>
+      </constructor>
+   </bean>
+   
+   <bean name="MBeanServer" class="javax.management.MBeanServer">
+      <constructor factoryClass="java.lang.management.ManagementFactory"
+                   factoryMethod="getPlatformMBeanServer"/>
+   </bean> 
+   
+</deployment>
\ No newline at end of file

Added: trunk/examples/jms/xa-transaction/src/org/jboss/jms/example/XATransactionExample.java
===================================================================
--- trunk/examples/jms/xa-transaction/src/org/jboss/jms/example/XATransactionExample.java	                        (rev 0)
+++ trunk/examples/jms/xa-transaction/src/org/jboss/jms/example/XATransactionExample.java	2009-04-10 10:08:54 UTC (rev 6397)
@@ -0,0 +1,424 @@
+/*
+   * 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 java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
+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.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.RollbackException;
+import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.xa.XAException;
+import javax.transaction.xa.XAResource;
+import javax.transaction.xa.Xid;
+
+import org.jboss.messaging.core.transaction.impl.XidImpl;
+import org.jboss.messaging.utils.UUIDGenerator;
+
+/**
+ * A simple JMS example showing the usage of XA support in JMS.
+ *
+ * @author <a href="hgao at redhat.com">Howard Gao</a>
+ */
+public class XATransactionExample extends JMSExample
+{
+   private volatile boolean result = true;
+   private ArrayList<String> receiveHolder = new ArrayList<String>();
+   
+   public static void main(String[] args)
+   {
+      new XATransactionExample().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. Perfom a 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 5. Create a JMS XASession
+         XASession xaSession = connection.createXASession();
+
+         //Step 6. Get the JMS Session
+         Session session = xaSession.getSession();
+         
+         //Step 7. Create two Text Messages
+         TextMessage helloMessage = session.createTextMessage("hello");
+         TextMessage worldMessage = session.createTextMessage("world");
+         
+         //Step 8. Create a message producer
+         MessageProducer producer = session.createProducer(queue);
+         
+         //Step 9. Create a message consumer
+         MessageConsumer consumer = session.createConsumer(queue);
+         consumer.setMessageListener(new SimpleMessageListener());
+         
+         //Step 10. Create a fake transaction
+         Transaction fakeTransaction = new SimpleTransaction();
+         
+         //Step 11. Create a fake XAResource
+         SimpleXAResource xaRes1 = new SimpleXAResource();
+         
+         //Step 12. Get the JMS XAResource
+         XAResource xaRes2 = xaSession.getXAResource();
+         
+         //Step 13. Enlist the resources
+         fakeTransaction.enlistResource(xaRes1);
+         fakeTransaction.enlistResource(xaRes2);
+         
+         //Step 14. Now do the work
+         producer.send(helloMessage);
+         xaRes1.sentMessage(helloMessage.getText());
+         producer.send(worldMessage);
+         
+         //Step 15. Delist resources
+         fakeTransaction.delistResource(xaRes1, XAResource.TMSUCCESS);
+         fakeTransaction.delistResource(xaRes2, XAResource.TMSUCCESS);
+         
+         //Step 16. Now finish the transaction, it will result in rollback!
+         try
+         {
+            fakeTransaction.commit();
+            result = false;
+         }
+         catch (RollbackException e)
+         {
+            System.out.println("Transaction rolled back, correct!");
+         }
+         
+         //Step 17. Check the result, it should receive none!
+         checkNoMessageReceived();
+
+         //Step 17. Now create a new Transaction
+         fakeTransaction = new SimpleTransaction();
+         
+         //Step 18. enlist the resources again
+         fakeTransaction.enlistResource(xaRes1);
+         fakeTransaction.enlistResource(xaRes2);
+         
+         //Step 19. do work
+         producer.send(helloMessage);
+         xaRes1.sentMessage(helloMessage.getText());
+         producer.send(worldMessage);
+         xaRes1.sentMessage(worldMessage.getText());
+         
+         //Step 15. Delist resources
+         fakeTransaction.delistResource(xaRes1, XAResource.TMSUCCESS);
+         fakeTransaction.delistResource(xaRes2, XAResource.TMSUCCESS);
+         
+         //Step 20. Now commit, should be ok.
+         fakeTransaction.commit();
+         
+         //Step 21. Check the result, all message received
+         checkAllMessageReceived();
+         
+         //Step 22. Now create new transaction, to show XA at the receiving end
+
+         initialContext.close();
+         
+         return true;
+      }
+      finally
+      {
+         //Step 12. Be sure to close our JMS resources!
+         if (initialContext != null)
+         {
+            initialContext.close();
+         }
+         if(connection != null)
+         {
+            connection.close();
+         }
+      }
+   }
+   
+   private void checkAllMessageReceived()
+   {
+      if (receiveHolder.size() != 2)
+      {
+         System.out.println("Message received not correct!");
+      }
+      receiveHolder.clear();
+   }
+
+   private void checkNoMessageReceived()
+   {
+      if (receiveHolder.size() > 0)
+      {
+         System.out.println("Message received, wrong!");
+         result = false;
+      }
+      receiveHolder.clear();
+   }
+
+
+   //A simple XAResource used to create different transaction decisions
+   public class SimpleXAResource implements XAResource
+   {
+      String helloWorld = "";
+      
+      public void sentMessage(String msg)
+      {
+         helloWorld = helloWorld + " " + msg;
+      }
+
+      public void commit(Xid arg0, boolean arg1) throws XAException
+      {
+      }
+
+      public void end(Xid arg0, int arg1) throws XAException
+      {
+      }
+
+      public void forget(Xid arg0) throws XAException
+      {
+      }
+
+      public int getTransactionTimeout() throws XAException
+      {
+         return 0;
+      }
+
+      public boolean isSameRM(XAResource arg0) throws XAException
+      {
+         return false;
+      }
+
+      public int prepare(Xid arg0) throws XAException
+      {
+         if (helloWorld.equals("hello world"))
+         {
+            return XA_RDONLY;
+         }
+         throw new XAException();
+      }
+
+      public Xid[] recover(int arg0) throws XAException
+      {
+         return null;
+      }
+
+      public void rollback(Xid arg0) throws XAException
+      {
+      }
+
+      public boolean setTransactionTimeout(int arg0) throws XAException
+      {
+         return false;
+      }
+
+      public void start(Xid arg0, int arg1) throws XAException
+      {
+      }
+      
+   }
+   
+   public static class SimpleTransaction implements Transaction
+   {
+      List<XAResource> txResources = new ArrayList<XAResource>();
+      List<Xid> xids = new ArrayList<Xid>();
+      
+//      Xid xid = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
+
+      public void commit() throws RollbackException,
+                          HeuristicMixedException,
+                          HeuristicRollbackException,
+                          SecurityException,
+                          SystemException
+      {
+         boolean ifCommit = true;
+         for (int i = 0; i < txResources.size(); i++)
+         {
+            XAResource res = txResources.get(i);
+            Xid xid = xids.get(i);
+            try
+            {
+               int n = res.prepare(xid);
+            }
+            catch (XAException e)
+            {
+               ifCommit = false;
+            }
+         }
+        
+         if (ifCommit)
+         {
+            try
+            {
+               doCommit();
+            }
+            catch (XAException e)
+            {
+               throw new HeuristicMixedException();
+            }
+         }
+         else
+         {
+            try
+            {
+               doRollback();
+               throw new RollbackException();
+            }
+            catch (XAException e)
+            {
+               throw new HeuristicRollbackException();
+            }
+         }
+      }
+      
+      private void doCommit() throws XAException
+      {
+         for (int i = 0; i < txResources.size(); i++)
+         {
+            XAResource res = txResources.get(i);
+            Xid xid = xids.get(i);
+            res.commit(xid, false);
+         }
+         
+      }
+      
+      private void doRollback() throws XAException
+      {
+         for (int i = 0; i < txResources.size(); i++)
+         {
+            XAResource res = txResources.get(i);
+            Xid xid = xids.get(i);
+            res.rollback(xid);
+         }
+      }
+
+      public boolean delistResource(XAResource res, int arg1) throws IllegalStateException, SystemException
+      {
+         boolean result = false;
+         for (int i = 0; i < txResources.size(); i++)
+         {
+            try
+            {
+               if (txResources.get(i).isSameRM(res)) {
+                  XAResource deRes = txResources.remove(i);
+                  deRes.end(xids.get(i), XAResource.TMSUCCESS);
+                  xids.remove(i);
+                  result = true;
+                  break;
+               }
+            }
+            catch (XAException e)
+            {
+            }
+         }
+         return result;
+      }
+
+      public boolean enlistResource(XAResource res) throws RollbackException, IllegalStateException, SystemException
+      {
+         txResources.add(res);
+         Xid xid = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes()); 
+         xids.add(xid);
+         try
+         {
+            res.start(xid, XAResource.TMNOFLAGS);
+         }
+         catch (XAException e)
+         {
+            //ignore
+         }
+         return true;
+      }
+
+      public int getStatus() throws SystemException
+      {
+         return 0;
+      }
+
+      public void registerSynchronization(Synchronization arg0) throws RollbackException,
+                                                               IllegalStateException,
+                                                               SystemException
+      {
+      }
+
+      public void rollback() throws IllegalStateException, SystemException
+      {
+         try
+         {
+            doRollback();
+         }
+         catch (XAException e)
+         {
+            throw new SystemException();
+         }
+      }
+
+      public void setRollbackOnly() throws IllegalStateException, SystemException
+      {
+      }
+
+   }
+   
+   public class SimpleMessageListener implements MessageListener
+   {
+
+      /* (non-Javadoc)
+       * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
+       */
+      public void onMessage(Message arg0)
+      {
+         // TODO Auto-generated method stub
+         
+      }
+      
+   }
+
+}




More information about the jboss-cvs-commits mailing list