[jboss-cvs] JBoss Messaging SVN: r6268 - in trunk/examples/jms/transactional: src/org/jboss/jms/example and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Apr 2 03:25:35 EDT 2009
Author: gaohoward
Date: 2009-04-02 03:25:35 -0400 (Thu, 02 Apr 2009)
New Revision: 6268
Modified:
trunk/examples/jms/transactional/readme.html
trunk/examples/jms/transactional/src/org/jboss/jms/example/TransactionalExample.java
Log:
refactor transactional example to be more illustrative of transaction usage.
Modified: trunk/examples/jms/transactional/readme.html
===================================================================
--- trunk/examples/jms/transactional/readme.html 2009-04-01 22:56:58 UTC (rev 6267)
+++ trunk/examples/jms/transactional/readme.html 2009-04-02 07:25:35 UTC (rev 6268)
@@ -5,9 +5,10 @@
<body>
<h1>JMS Transactional Session Example</h1>
<br>
- <p>This example shows you how to use a transactional Session with JBoss Messaging.</p>
- <p>Messages can be sent and received over transactional sessions, please consult the JMS 1.1 specification for full details.</p>
+ <p>This example shows you how to use a transactional Session with JBoss Messaging. It creates a transactional session. At first it sends out two messages and tries to receive without session commit. Then it commits the sending session and receives only one messages before it rolls back the receiving session. It then receives all the messages and commits the session.</p>
+ <p>Messages can be sent and received over transactional sessions. Messages in a transactional session will not be sent or acknowledged until the session is committed. It a session is rolled back, the produced messages will be destroyed and consumed messages will be recovered. Please consult the JMS 1.1 specification for full details.</p>
+
<br>
<h2>Example step-by-step</h2>
<p><i>To run the example, simply type <code>ant</code> from this directory</i></p>
@@ -48,40 +49,60 @@
<code>MessageProducer messageProducer = session.createProducer(queue);</code>
</pre>
- <li>We create a JMS text message to be sent</li>
+ <li>We create a message consumer</li>
<pre>
- <code>TextMessage message = session.createTextMessage("This is a text message");</code>
+ <code>MessageConsumer messageConsumer = session.createConsumer(queue);</code>
</pre>
+
+ <li>We create 2 text messages</li>
+ <pre>
+ <code>TextMessage message1 = session.createTextMessage("This is a text message1");</code>
+ <code>TextMessage message2 = session.createTextMessage("This is a text message2");</code>
+ </pre>
- <li>We send message to the queue</li>
+ <li>We send the text messages to the queue</li>
<pre>
- <code>messageProducer.send(message);</code>
+ <code>messageProducer.send(message1);</code>
+ <code>messageProducer.send(message2);</code>
</pre>
- <li>We commit the session to let the message be delivered. After commit, the session will start a new transaction automatically.</li>
+ <li>We receive the message, it will return null as the transaction is not committed.</li>
<pre>
- <code>session.commit();</code>
+ <code>TextMessage receivedMessage = (TextMessage) messageConsumer.receive(5000);</code>
</pre>
- <li>We create a message consumer on the queue</li>
+ <li>We commit the session</li>
<pre>
- <code>TextMessage message2 = session.createTextMessage("This is a text message 2");</code>
- <br>
- <code>MessageConsumer messageConsumer = session.createConsumer(queue);</code>
+ <code>session.commit();</code>
</pre>
- <li>We receive the message</li>
-
+ <li>We receive the messages again</li>
<pre>
- <code>message = (TextMessage) messageConsumer.receive(5000);</code>
+ <code>receivedMessage = (TextMessage) messageConsumer.receive(5000);</code>
</pre>
-
- <li>We now commit the transaction. It will finish the message delivery.</li>
+ <li>We roll back the session, this will cause the received message canceled and redelivered again</li>
<pre>
+ <code>session.rollback();</code>
+ </pre>
+
+ <li>We receive the message again, we will get two messages. Nothing more, nothing less</li>
+ <pre>
+ <code>receivedMessage = (TextMessage) messageConsumer.receive(5000);</code>
+ <code>receivedMessage = (TextMessage) messageConsumer.receive(5000);</code>
+ </pre>
+
+ <li>We commit the session</li>
+ <pre>
<code>session.commit();</code>
</pre>
+ <li>We receive the message again. Nothing should be received</li>
+ <pre>
+ <code>receivedMessage = (TextMessage) messageConsumer.receive(5000);</code>
+ </pre>
+
+
<li>And finally (no pun intended), <b>always</b> remember to close your JMS connections after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its session, consumer, producer and browser objects</li>
<pre>
@@ -100,4 +121,4 @@
</ol>
</body>
-</html>
\ No newline at end of file
+</html>
Modified: trunk/examples/jms/transactional/src/org/jboss/jms/example/TransactionalExample.java
===================================================================
--- trunk/examples/jms/transactional/src/org/jboss/jms/example/TransactionalExample.java 2009-04-01 22:56:58 UTC (rev 6267)
+++ trunk/examples/jms/transactional/src/org/jboss/jms/example/TransactionalExample.java 2009-04-02 07:25:35 UTC (rev 6268)
@@ -69,27 +69,57 @@
// Step 7. Create a JMS message producer
MessageProducer messageProducer = session.createProducer(queue);
- // Step 8. Create a text message
- TextMessage message = session.createTextMessage("This is a text message");
+ // Step 8. Create a message consumer
+ MessageConsumer messageConsumer = session.createConsumer(queue);
- // Step 9. Send the text message to the queue
- messageProducer.send(message);
+ // Step 9. Create 2 text messages
+ TextMessage message1 = session.createTextMessage("This is a text message1");
+ TextMessage message2 = session.createTextMessage("This is a text message2");
- System.out.println("Sent message: " + message.getText());
+ // Step 10. Send the text messages to the queue
+ messageProducer.send(message1);
+ messageProducer.send(message2);
- // Step 10. Commit the session
- session.commit();
+ System.out.println("Sent message: " + message1.getText());
+ System.out.println("Sent message: " + message2.getText());
+
+ // Step 11. Receive the message, it will return null as the transaction is not committed.
+ TextMessage receivedMessage = (TextMessage) messageConsumer.receive(5000);
- // Step 11. Create a message consumer
- MessageConsumer messageConsumer = session.createConsumer(queue);
+ System.out.println("Message received before send commit: " + receivedMessage);
- // Step 12. Receive the message from the queue
- message = (TextMessage) messageConsumer.receive(5000);
+ // Step 12. Commit the session
+ session.commit();
- System.out.println("Received message: " + message.getText());
+ // Step 13. Receive the messages again
+ receivedMessage = (TextMessage) messageConsumer.receive(5000);
- // Step 13. Commit the session again
+ System.out.println("Message received after send commit: " + receivedMessage.getText());
+
+ // Step 14. Roll back the session, this will cause the received message canceled and redelivered again.
+ session.rollback();
+
+ // Step 15. Receive the message again, we will get two messages
+ receivedMessage = (TextMessage) messageConsumer.receive(5000);
+
+ System.out.println("Message1 received after receive rollback: " + receivedMessage.getText());
+
+ receivedMessage = (TextMessage) messageConsumer.receive(5000);
+
+ System.out.println("Message2 received after receive rollback: " + receivedMessage.getText());
+
+ receivedMessage = (TextMessage) messageConsumer.receive(5000);
+
+ System.out.println("Message3 received after receive rollback: " + receivedMessage);
+
+ //Step 16. Commit the session
session.commit();
+
+ //Step 17. Receive the message again. Nothing should be received.
+ receivedMessage = (TextMessage) messageConsumer.receive(5000);
+
+ System.out.println("Message received after receive commit: " + receivedMessage);
+
}
finally
{
@@ -97,7 +127,7 @@
{
try
{
- // Step 19. Be sure to close our JMS resources!
+ // Step 18. Be sure to close our JMS resources!
connection.close();
}
catch (JMSException e)
More information about the jboss-cvs-commits
mailing list