[jboss-cvs] JBoss Messaging SVN: r6601 - in trunk/examples/jms/xa-with-jta: src/org/jboss/jms/example and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Apr 28 11:55:37 EDT 2009


Author: gaohoward
Date: 2009-04-28 11:55:37 -0400 (Tue, 28 Apr 2009)
New Revision: 6601

Modified:
   trunk/examples/jms/xa-with-jta/readme.html
   trunk/examples/jms/xa-with-jta/src/org/jboss/jms/example/XAwithJTAExample.java
Log:
improve xa-with-jta example, add a dummy resource to show transaction commit info


Modified: trunk/examples/jms/xa-with-jta/readme.html
===================================================================
--- trunk/examples/jms/xa-with-jta/readme.html	2009-04-28 14:33:13 UTC (rev 6600)
+++ trunk/examples/jms/xa-with-jta/readme.html	2009-04-28 15:55:37 UTC (rev 6601)
@@ -18,7 +18,8 @@
       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>
+      messages. Then we check that no more messages are to be received. In each transaction a dummy XAResource is also 
+      enlisted to show the transaction processing information.</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
@@ -100,10 +101,11 @@
           <code>XAResource xaRes = xaSession.getXAResource();</code>
        </pre>
 
-        <li>We enlist the resource in the Transaction work</li>
+        <li>We enlist the resources in the Transaction work</li>
         <pre>
           <code>
           Transaction transaction = txMgr.getTransaction();
+          transaction.enlistResource(new DummyXAResource());
           transaction.enlistResource(xaRes);
           </code>
        </pre>
@@ -126,11 +128,6 @@
           </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>
@@ -144,9 +141,12 @@
           </code>
        </pre>
 
-        <li>We enlist the resource to start the transaction work</li>
+        <li>We enlist the resources to start the transaction work</li>
         <pre>
-          <code>transaction.enlistResource(xaRes);</code>
+          <code>         
+          transaction.enlistResource(new DummyXAResource());
+          transaction.enlistResource(xaRes);
+          </code>
        </pre>
 
         <li>We receive those messages again</li>
@@ -159,11 +159,6 @@
             </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>

Modified: 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	2009-04-28 14:33:13 UTC (rev 6600)
+++ trunk/examples/jms/xa-with-jta/src/org/jboss/jms/example/XAwithJTAExample.java	2009-04-28 15:55:37 UTC (rev 6601)
@@ -31,7 +31,9 @@
 import javax.jms.XASession;
 import javax.naming.InitialContext;
 import javax.transaction.Transaction;
+import javax.transaction.xa.XAException;
 import javax.transaction.xa.XAResource;
+import javax.transaction.xa.Xid;
 
 import com.arjuna.ats.jta.TransactionManager;
 
@@ -100,6 +102,7 @@
          
          //Step 15. enlist the resource in the Transaction work
          Transaction transaction = txMgr.getTransaction();
+         transaction.enlistResource(new DummyXAResource());
          transaction.enlistResource(xaRes);
          
          //Step 16. Send two messages.
@@ -112,32 +115,27 @@
          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
+         //Step 18. Roll back the transaction
          txMgr.rollback();
          
-         //Step 20. Create another transaction
+         //Step 19. Create another transaction
          txMgr.begin();
          transaction = txMgr.getTransaction();
          
-         //Step 21. Enlist the resource to start the transaction work
+         //Step 20. Enlist the resources to start the transaction work
+         transaction.enlistResource(new DummyXAResource());
          transaction.enlistResource(xaRes);
          
-         //Step 22. receive those messages again
+         //Step 21. 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!
+         //Step 22. Commit!
          txMgr.commit();
          
-         //Step 25. Check no more messages are received.
+         //Step 23. Check no more messages are received.
          TextMessage rm3 = (TextMessage)xaConsumer.receive(2000);
          if (rm3 == null)
          {
@@ -152,7 +150,7 @@
       }
       finally
       {
-         //Step 26. Be sure to close our JMS resources!
+         //Step 24. Be sure to close our JMS resources!
          if (initialContext != null)
          {
             initialContext.close();
@@ -163,5 +161,64 @@
          }
       }
    }
+   
+   public class DummyXAResource implements XAResource
+   {
 
+      public DummyXAResource()
+      {
+      }
+
+      public void commit(Xid xid, boolean arg1) throws XAException
+      {
+         System.out.println("DummyXAResource commit() called, xid: " + xid);
+      }
+
+      public void end(Xid xid, int arg1) throws XAException
+      {
+         System.out.println("DummyXAResource end() called, xid: " + xid);
+      }
+
+      public void forget(Xid xid) throws XAException
+      {
+         System.out.println("DummyXAResource forget() called, xid: " + xid);
+      }
+
+      public int getTransactionTimeout() throws XAException
+      {
+         return 0;
+      }
+
+      public boolean isSameRM(XAResource arg0) throws XAException
+      {
+         return this == arg0;
+      }
+
+      public int prepare(Xid xid) throws XAException
+      {
+         return XAResource.XA_OK;
+      }
+
+      public Xid[] recover(int arg0) throws XAException
+      {
+         return null;
+      }
+
+      public void rollback(Xid xid) throws XAException
+      {
+         System.out.println("DummyXAResource rollback() called, xid: " + xid);
+      }
+
+      public boolean setTransactionTimeout(int arg0) throws XAException
+      {
+         return false;
+      }
+
+      public void start(Xid xid, int arg1) throws XAException
+      {
+         System.out.println("DummyXAResource start() called, Xid: " + xid);
+      }
+      
+   }
+
 }




More information about the jboss-cvs-commits mailing list