[jboss-cvs] JBoss Messaging SVN: r6233 - in trunk/examples/jms: queue and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Mar 31 07:57:55 EDT 2009


Author: ataylor
Date: 2009-03-31 07:57:54 -0400 (Tue, 31 Mar 2009)
New Revision: 6233

Added:
   trunk/examples/jms/queue/readme.html
Modified:
   trunk/examples/jms/durable/readme.html
   trunk/examples/jms/queue/src/org/jboss/jms/example/QueueExample.java
Log:
updated example

Modified: trunk/examples/jms/durable/readme.html
===================================================================
--- trunk/examples/jms/durable/readme.html	2009-03-31 10:53:10 UTC (rev 6232)
+++ trunk/examples/jms/durable/readme.html	2009-03-31 11:57:54 UTC (rev 6233)
@@ -13,7 +13,7 @@
      <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>jndi.properties</code> file in the directory <code>../config</code></li>
+        <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>

Added: trunk/examples/jms/queue/readme.html
===================================================================
--- trunk/examples/jms/queue/readme.html	                        (rev 0)
+++ trunk/examples/jms/queue/readme.html	2009-03-31 11:57:54 UTC (rev 6233)
@@ -0,0 +1,91 @@
+<html>
+  <head>
+    <title>JBoss Messaging JMS Queue Example</title>
+  </head>
+  <body>
+     <h1>JMS Queue Example</h1>
+     <br>
+     <p>This example shows you how to send and receive a message to a JMS Queue with JBoss Messaging.</p>
+     <p>Queues are a standard part of JMS, please consult the JMS 1.1 specification for full details.</p>
+     <p>A Queue is used to send messages point to point, from a producer to a consumer. The queue gaurauntees message ordering between these 2 points.</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 1");</code>
+        </pre>
+
+        <li>We send message to the topic</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 in the subscription, and we consume the message from the subscription. In this case we use a timeout of 5000 mili seconds 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 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 (connection != null)
+         {
+            // Step 12. Be sure to close our JMS resources!
+            connection.close();
+         }
+      }
+           </code>
+        </pre>
+
+
+
+     </ol>
+  </body>
+</html>
\ No newline at end of file

Modified: trunk/examples/jms/queue/src/org/jboss/jms/example/QueueExample.java
===================================================================
--- trunk/examples/jms/queue/src/org/jboss/jms/example/QueueExample.java	2009-03-31 10:53:10 UTC (rev 6232)
+++ trunk/examples/jms/queue/src/org/jboss/jms/example/QueueExample.java	2009-03-31 11:57:54 UTC (rev 6233)
@@ -49,37 +49,49 @@
       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. Perfom a lookup on the queue
          Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
+
+         //Step 3. Perform a lookup on the Connection Factory
          ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
-         
+
+         //Step 4.Create a JMS Connection
          connection = cf.createConnection();
+
+         //Step 5. Create a JMS Session
          Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+         //Step 6. Create a JMS Message Producer
          MessageProducer producer = session.createProducer(queue);
-         Message message = session.createTextMessage("This is a text message!");
+
+         //Step 7. Create a Text Message
+         TextMessage message = session.createTextMessage("This is a text message!");
          
-         log.info("sending message to queue");
+         System.out.println("Sent message: " + message.getText());
+
+         //Step 8. Send the Message
          producer.send(message);
-         
+
+         //Step 9. Create a JMS Message Consumer
          MessageConsumer messageConsumer = session.createConsumer(queue);
+
+         //Step 10. Start the Connection
          connection.start();
-         TextMessage message2 = (TextMessage) messageConsumer.receive(5000);
-         log.info("message received from queue");
-         log.info("message = " + message2.getText());
+
+         //Step 11. Receive the message
+         TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
+
+         System.out.println("Received message: " + messageReceived.getText());
       }
       finally
       {
+         //Step 12. Be sure to close our JMS resources!
          if(connection != null)
          {
-            try
-            {
-               connection.close();
-            }
-            catch (JMSException e)
-            {
-               e.printStackTrace();
-            }
+            connection.close();
          }
       }
    }




More information about the jboss-cvs-commits mailing list