[jboss-cvs] JBoss Messaging SVN: r6242 - 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
Tue Mar 31 12:14:49 EDT 2009


Author: gaohoward
Date: 2009-03-31 12:14:49 -0400 (Tue, 31 Mar 2009)
New Revision: 6242

Added:
   trunk/examples/jms/transactional/readme.html
Removed:
   trunk/examples/jms/transactional/READ_ME.txt
Modified:
   trunk/examples/jms/transactional/src/org/jboss/jms/example/TransactionalExample.java
Log:
change to follow the example template


Deleted: trunk/examples/jms/transactional/READ_ME.txt
===================================================================

Added: trunk/examples/jms/transactional/readme.html
===================================================================
--- trunk/examples/jms/transactional/readme.html	                        (rev 0)
+++ trunk/examples/jms/transactional/readme.html	2009-03-31 16:14:49 UTC (rev 6242)
@@ -0,0 +1,103 @@
+<html>
+  <head>
+    <title>JBoss Messaging JMS Transactional Session Example</title>
+  </head>
+  <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>
+
+     <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 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>We create a JMS session. The session is created as transacted.</li>
+        <pre>
+           <code>Session session = connection.createSession(true, Session.SESSION_TRANSACTED);</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(queue);</code>
+        </pre>
+
+        <li>We create a JMS text message to be sent</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 commit the session to let the message be delivered. After commit, the session will start a new transaction automatically.</li>
+        <pre>
+           <code>session.commit();</code>
+        </pre>
+
+        <li>We create a message consumer on the queue</li>
+	<pre>
+           <code>TextMessage message2 = session.createTextMessage("This is a text message 2");</code>
+	   <br>
+           <code>MessageConsumer messageConsumer = session.createConsumer(queue);</code>
+        </pre>
+         
+        <li>We receive the message</li>
+
+        <pre>
+           <code>message = (TextMessage) messageConsumer.receive(5000);</code>
+        </pre>
+         
+        <li>We now commit the transaction. It will finish the message delivery.</li>
+
+        <pre> 
+           <code>session.commit();</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>
+           <code>
+
+      finally
+      {
+         if (connection != null)
+         {
+            // Be sure to close our JMS resources!
+            connection.close();
+         }
+      }
+           </code>
+        </pre>
+         
+     </ol>
+  </body>
+</html>
\ No newline at end of file

Modified: trunk/examples/jms/transactional/src/org/jboss/jms/example/TransactionalExample.java
===================================================================
--- trunk/examples/jms/transactional/src/org/jboss/jms/example/TransactionalExample.java	2009-03-31 15:46:46 UTC (rev 6241)
+++ trunk/examples/jms/transactional/src/org/jboss/jms/example/TransactionalExample.java	2009-03-31 16:14:49 UTC (rev 6242)
@@ -24,7 +24,6 @@
 import javax.jms.Connection;
 import javax.jms.ConnectionFactory;
 import javax.jms.JMSException;
-import javax.jms.Message;
 import javax.jms.MessageConsumer;
 import javax.jms.MessageProducer;
 import javax.jms.Queue;
@@ -49,31 +48,48 @@
       Connection connection = null;
       try
       {
-         //create an initial context, env will be picked up from client-jndi.properties
+         // Step 1. Create an initial context to perform the JNDI lookup.
          InitialContext initialContext = getContext();
+
+         // Step 2. Look-up the JMS topic
          Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
-         ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
+
+         // Step 3. Look-up the JMS connection factory
+         ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
          
+         // Step 4. Create a JMS connection
          connection = cf.createConnection();
+
+         // Step 5. Start the connection
+         connection.start();
+
+         // Step 6. Create a transactional JMS session
          Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
          
-         MessageProducer producer = session.createProducer(queue);
-         Message message = session.createTextMessage("This is a text message!");
-         
-         log.info("sending message to queue");
-         producer.send(message);
-         
+         // 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 9. Send the text message to the queue
+         messageProducer.send(message);
+
+         System.out.println("Sent message: " + message.getText());
+
+         // Step 10. Commit the session
          session.commit();
          
+         // Step 11. Create a message consumer
          MessageConsumer messageConsumer = session.createConsumer(queue);
-         connection.start();
-         TextMessage message2 = (TextMessage) messageConsumer.receive(5000);
+
+         // Step 12. Receive the message from the queue
+         message = (TextMessage) messageConsumer.receive(5000);
+
+         System.out.println("Received message: " + message.getText());
          
+         // Step 13. Commit the session again
          session.commit();
-
-         log.info("message received from queue");
-         log.info("message = " + message2.getText());
-
       }
       finally
       {
@@ -81,6 +97,7 @@
          {
             try
             {
+               // Step 19. Be sure to close our JMS resources!
                connection.close();
             }
             catch (JMSException e)




More information about the jboss-cvs-commits mailing list