[jboss-cvs] JBoss Messaging SVN: r6409 - in trunk/examples/jms/xa-send: src/org/jboss/jms/example and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Sun Apr 12 11:41:31 EDT 2009
Author: gaohoward
Date: 2009-04-12 11:41:30 -0400 (Sun, 12 Apr 2009)
New Revision: 6409
Modified:
trunk/examples/jms/xa-send/build.xml
trunk/examples/jms/xa-send/readme.html
trunk/examples/jms/xa-send/src/org/jboss/jms/example/XASendExample.java
Log:
document for xa-send
Modified: trunk/examples/jms/xa-send/build.xml
===================================================================
--- trunk/examples/jms/xa-send/build.xml 2009-04-12 15:11:03 UTC (rev 6408)
+++ trunk/examples/jms/xa-send/build.xml 2009-04-12 15:41:30 UTC (rev 6409)
@@ -34,15 +34,15 @@
<target name="run">
<antcall target="runExample">
- <param name="example.classname" value="org.jboss.jms.example.XATransactionExample"/>
+ <param name="example.classname" value="org.jboss.jms.example.XASendExample"/>
</antcall>
</target>
<target name="runRemote">
<antcall target="runExample">
- <param name="example.classname" value="org.jboss.jms.example.XATransactionExample"/>
+ <param name="example.classname" value="org.jboss.jms.example.XASendExample"/>
<param name="jbm.example.runServer" value="false"/>
</antcall>
</target>
-</project>
\ No newline at end of file
+</project>
Modified: trunk/examples/jms/xa-send/readme.html
===================================================================
--- trunk/examples/jms/xa-send/readme.html 2009-04-12 15:11:03 UTC (rev 6408)
+++ trunk/examples/jms/xa-send/readme.html 2009-04-12 15:41:30 UTC (rev 6409)
@@ -1,21 +1,21 @@
<html>
<head>
- <title>JBoss Messaging JMS XA Transaction Example</title>
+ <title>JBoss Messaging JMS XA Send Example</title>
<link rel="stylesheet" type="text/css" href="../common/common.css">
</head>
<body>
- <h1>JMS XA Transaction Example</h1>
+ <h1>JMS XA Send Example</h1>
<br>
- <p>This example shows you the XA support in JBoss Messaging.</p>
+ <p>This example shows you how message sending behaves in an XA transaction 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>
+ <p>In this example we simulate a transaction manager to control the transactions. First we create an XASession
+ and enlist it in a transaction through its XAResource. We then send two words, 'hello' and 'world', with
+ the session, let the transaction roll back. The messages are discarded and never be received. Next we start
+ a new transaction with the same XAResource, but this time we commit the transaction. Both messages 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>
@@ -23,7 +23,7 @@
<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>
+ <code>InitialContext initialContext = getContext(0);</code>
</pre>
<li>We look-up the JMS queue object from JNDI</li>
@@ -31,51 +31,148 @@
<code>Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");</code>
</pre>
- <li>We look-up the JMS connection factory object from JNDI</li>
+ <li>We perform a lookup on the XA Connection Factory</li>
<pre>
- <code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code>
+ <code>XAConnectionFactory cf = (XAConnectionFactory) initialContext.lookup("/XAConnectionFactory");</code>
</pre>
- <li>We create a JMS connection</li>
+ <li>We create a JMS XAConnection</li>
<pre>
- <code>connection = cf.createConnection();</code>
+ <code>connection = cf.createXAConnection();</code>
</pre>
- <li>We create a JMS session. The session is created as non transacted and will auto acknowledge messages.</li>
+ <li>We Start the connection</li>
<pre>
- <code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
+ <code>connection.start();</code>
</pre>
- <li>We create a JMS message producer on the session. This will be used to send the messages.</li>
+ <li>We create a JMS XASession</li>
<pre>
- <code>MessageProducer messageProducer = session.createProducer(topic);</code>
+ <code>XASession xaSession = connection.createXASession();</code>
</pre>
- <li>We create a JMS text message that we are going to send.</li>
+ <li>We create a normal session</li>
<pre>
- <code>TextMessage message = session.createTextMessage("This is a text message");</code>
- </pre>
+ <code>Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
+ </pre>
- <li>We send message to the queue</li>
+ <li>We create a normal Message Consumer</li>
<pre>
- <code>messageProducer.send(message);</code>
- </pre>
+ <code>
+ MessageConsumer normalConsumer = normalSession.createConsumer(queue);
+ normalConsumer.setMessageListener(new SimpleMessageListener());
+ </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 get the JMS Session</li>
+ <pre>
+ <code>Session session = xaSession.getSession();</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>
+ <li>We create a message producer</li>
<pre>
- <code>connection.start();</code>
- </pre>
+ <code>MessageProducer producer = session.createProducer(queue);</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>
+ <li>We create two Text Messages</li>
<pre>
- <code>TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);</code>
- </pre>
+ <code>
+ TextMessage helloMessage = session.createTextMessage("hello");
+ TextMessage worldMessage = session.createTextMessage("world");
+ </code>
+ </pre>
+ <li>We create a transaction</li>
+ <pre>
+ <code>Xid xid1 = new XidImpl("xa-example1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());</code>
+ </pre>
+
+ <li>We get the JMS XAResource</li>
+ <pre>
+ <code>XAResource xaRes = xaSession.getXAResource();</code>
+ </pre>
+
+ <li>We begin the Transaction work</li>
+ <pre>
+ <code>xaRes.start(xid1, XAResource.TMNOFLAGS);</code>
+ </pre>
+
+ <li>We do work, sending two messages.</li>
+ <pre>
+ <code>
+ producer.send(helloMessage);
+ producer.send(worldMessage);
+ </code>
+ </pre>
+
+ <li>We check the result, it should receive none!</li>
+ <pre>
+ <code>checkNoMessageReceived();</code>
+ </pre>
+
+ <li>We stop the work</li>
+ <pre>
+ <code>xaRes.end(xid1, XAResource.TMSUCCESS);</code>
+ </pre>
+
+ <li>We prepare</li>
+ <pre>
+ <code>xaRes.prepare(xid1);</code>
+ </pre>
+
+ <li>We roll back the transaction </li>
+ <pre>
+ <code>xaRes.rollback(xid1);</code>
+ </pre>
+
+ <li>We check no messages should be received! </li>
+ <pre>
+ <code>checkNoMessageReceived();</code>
+ </pre>
+
+ <li>We create another transaction</li>
+ <pre>
+ <code>Xid xid2 = new XidImpl("xa-example2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());</code>
+ </pre>
+
+ <li>We start the transaction</li>
+ <pre>
+ <code>xaRes.start(xid2, XAResource.TMNOFLAGS);</code>
+ </pre>
+
+ <li>We re-send those messages</li>
+ <pre>
+ <code>
+ producer.send(helloMessage);
+ producer.send(worldMessage);
+ </code>
+ </pre>
+
+ <li>We stop the work</li>
+ <pre>
+ <code>xaRes.end(xid2, XAResource.TMSUCCESS);</code>
+ </pre>
+
+ <li>We prepare</li>
+ <pre>
+ <code>xaRes.prepare(xid2);</code>
+ </pre>
+
+ <li>We check that no messages should be received at this moment</li>
+ <pre>
+ <code>checkNoMessageReceived();</code>
+ </pre>
+
+ <li>We commit!</li>
+ <pre>
+ <code>xaRes.commit(xid2, false);</code>
+ </pre>
+
+ <li>We check that all messages are received.</li>
+ <pre>
+ <code>checkAllMessageReceived();</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>
@@ -91,9 +188,6 @@
}
}</code>
</pre>
-
-
-
</ol>
</body>
</html>
\ No newline at end of file
Modified: trunk/examples/jms/xa-send/src/org/jboss/jms/example/XASendExample.java
===================================================================
--- trunk/examples/jms/xa-send/src/org/jboss/jms/example/XASendExample.java 2009-04-12 15:11:03 UTC (rev 6408)
+++ trunk/examples/jms/xa-send/src/org/jboss/jms/example/XASendExample.java 2009-04-12 15:41:30 UTC (rev 6409)
@@ -65,7 +65,7 @@
//Step 1. Create an initial context to perform the JNDI lookup.
initialContext = getContext(0);
- //Step 2. Perfom a lookup on the queue
+ //Step 2. Lookup on the queue
Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
//Step 3. Perform a lookup on the XA Connection Factory
@@ -77,88 +77,88 @@
//Step 5. Start the connection
connection.start();
- //Step 5. Create a JMS XASession
+ //Step 6. Create a JMS XASession
XASession xaSession = connection.createXASession();
- //Step 6. Create a normal session
+ //Step 7. Create a normal session
Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//Step 8. Create a normal Message Consumer
MessageConsumer normalConsumer = normalSession.createConsumer(queue);
normalConsumer.setMessageListener(new SimpleMessageListener());
- //Step 6. Get the JMS Session
+ //Step 9. Get the JMS Session
Session session = xaSession.getSession();
- //Step 8. Create a message producer
+ //Step 10. Create a message producer
MessageProducer producer = session.createProducer(queue);
- //Step 7. Create two Text Messages
+ //Step 11. Create two Text Messages
TextMessage helloMessage = session.createTextMessage("hello");
TextMessage worldMessage = session.createTextMessage("world");
- //Step 10. create a transaction
+ //Step 12. create a transaction
Xid xid1 = new XidImpl("xa-example1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
- //Step 12. Get the JMS XAResource
+ //Step 13. Get the JMS XAResource
XAResource xaRes = xaSession.getXAResource();
- //Step 12. Begin the Transaction work
+ //Step 14. Begin the Transaction work
xaRes.start(xid1, XAResource.TMNOFLAGS);
- //Step 19. do work, sending two messages.
+ //Step 15. do work, sending two messages.
producer.send(helloMessage);
producer.send(worldMessage);
Thread.sleep(2000);
- //Step 17. Check the result, it should receive none!
+ //Step 16. Check the result, it should receive none!
checkNoMessageReceived();
- //Step 19. Stop the work
+ //Step 17. Stop the work
xaRes.end(xid1, XAResource.TMSUCCESS);
- //Step 20. Prepare
+ //Step 18. Prepare
xaRes.prepare(xid1);
- //Step 18. Roll back the transaction
+ //Step 19. Roll back the transaction
xaRes.rollback(xid1);
- //Step. No messages should be received!
+ //Step 20. No messages should be received!
checkNoMessageReceived();
- //Step 19. Create another transaction
+ //Step 21. Create another transaction
Xid xid2 = new XidImpl("xa-example2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
- //Step 20. Start the transaction
+ //Step 22. Start the transaction
xaRes.start(xid2, XAResource.TMNOFLAGS);
- //Step 21. Re-send those messages
+ //Step 23. Re-send those messages
producer.send(helloMessage);
producer.send(worldMessage);
- //Step 22. Stop the work
+ //Step 24. Stop the work
xaRes.end(xid2, XAResource.TMSUCCESS);
- //Step 23. Prepare
+ //Step 25. Prepare
xaRes.prepare(xid2);
- //Step 23. No messages should be received at this moment
+ //Step 26. No messages should be received at this moment
checkNoMessageReceived();
- //Step 23. Commit!
+ //Step 27. Commit!
xaRes.commit(xid2, true);
Thread.sleep(2000);
- //Step 21. Check the result, all message received
+ //Step 28. Check the result, all message received
checkAllMessageReceived();
return result;
}
finally
{
- //Step 12. Be sure to close our JMS resources!
+ //Step 29. Be sure to close our JMS resources!
if (initialContext != null)
{
initialContext.close();
@@ -197,7 +197,7 @@
{
try
{
- System.out.println("-----Message received: " + message);
+ System.out.println("Message received: " + message);
receiveHolder.add(((TextMessage)message).getText());
}
catch (JMSException e)
More information about the jboss-cvs-commits
mailing list