[jboss-cvs] JBoss Messaging SVN: r6235 - in trunk/examples/jms: queue/src/org/jboss/jms/example and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Mar 31 08:59:27 EDT 2009


Author: ataylor
Date: 2009-03-31 08:59:27 -0400 (Tue, 31 Mar 2009)
New Revision: 6235

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

Modified: trunk/examples/jms/queue/readme.html
===================================================================
--- trunk/examples/jms/queue/readme.html	2009-03-31 12:03:10 UTC (rev 6234)
+++ trunk/examples/jms/queue/readme.html	2009-03-31 12:59:27 UTC (rev 6235)
@@ -45,10 +45,10 @@
 
         <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>
+           <code>TextMessage message = session.createTextMessage("This is a text message");</code>
         </pre>
 
-        <li>We send message to the topic</li>
+        <li>We send message to the queue</li>
         <pre>
            <code>messageProducer.send(message);</code>
         </pre>
@@ -63,7 +63,7 @@
            <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>
+        <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>
         <pre>
            <code>TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);</code>
         </pre>

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 12:03:10 UTC (rev 6234)
+++ trunk/examples/jms/queue/src/org/jboss/jms/example/QueueExample.java	2009-03-31 12:59:27 UTC (rev 6235)
@@ -68,7 +68,7 @@
          MessageProducer producer = session.createProducer(queue);
 
          //Step 7. Create a Text Message
-         TextMessage message = session.createTextMessage("This is a text message!");
+         TextMessage message = session.createTextMessage("This is a text message");
          
          System.out.println("Sent message: " + message.getText());
 

Added: trunk/examples/jms/topic/readme.html
===================================================================
--- trunk/examples/jms/topic/readme.html	                        (rev 0)
+++ trunk/examples/jms/topic/readme.html	2009-03-31 12:59:27 UTC (rev 6235)
@@ -0,0 +1,101 @@
+<html>
+  <head>
+    <title>JBoss Messaging JMS Topic Example</title>
+  </head>
+  <body>
+     <h1>JMS Topic Example</h1>
+     <br>
+     <p>This example shows you how to send and receive a message to a JMS Topic 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 Topic is used to send messages using the publish-subscribe model, from a producer to 1 or more consumers.</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 topic object from JNDI</li>
+        <pre>
+           <code>Topic topic = (Topic) initialContext.lookup("/topic/exampleTopic");</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 Message Consumer, messageConsumer1, to receive the message.</li>
+         <pre>
+           <code>MessageConsumer messageConsumer = session.createConsumer(topic);</code>
+         </pre>
+
+        <li>We create a JMS Message Consumer, messageConsumer2, to also receive the message.</li>
+        <pre>
+          <code>MessageConsumer messageConsumer2 = session.createConsumer(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");</code>
+        </pre>
+
+        <li>We send message to the topic</li>
+        <pre>
+           <code>messageProducer.send(message);</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 at the first consumer</li>
+        <pre>
+           <code>TextMessage messageReceived = (TextMessage) messageConsumer1.receive();</code>
+        </pre>
+
+        <li>The message arrives at the second consumer</li>
+        <pre>
+           <code>messageReceived = (TextMessage) messageConsumer2.receive();</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 14. Be sure to close our JMS resources!
+            connection.close();
+         }
+      }
+           </code>
+        </pre>
+
+
+
+     </ol>
+  </body>
+</html>
\ No newline at end of file

Modified: trunk/examples/jms/topic/src/org/jboss/jms/example/TopicExample.java
===================================================================
--- trunk/examples/jms/topic/src/org/jboss/jms/example/TopicExample.java	2009-03-31 12:03:10 UTC (rev 6234)
+++ trunk/examples/jms/topic/src/org/jboss/jms/example/TopicExample.java	2009-03-31 12:59:27 UTC (rev 6235)
@@ -35,8 +35,7 @@
 import java.util.concurrent.CountDownLatch;
 
 /**
- * A simple JMS Topic example that creates a producer and consumer on a queue and sends and receives a message via a
- * Message Listener..
+ * A simple JMS Topic example that creates a producer and consumer on a queue and sends and receives a message.
  *
  * @author <a href="ataylor at redhat.com">Andy Taylor</a>
  */
@@ -52,59 +51,57 @@
       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. perform a lookup on the topic
          Topic topic = (Topic) initialContext.lookup("/topic/exampleTopic");
+
+         //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);
-         MessageProducer messageProducer = session.createProducer(topic);
 
-         MessageConsumer messageConsumer = session.createConsumer(topic);
-         Message message = session.createTextMessage("This is a text message!");
-         final CountDownLatch latch = new CountDownLatch(1);
-         messageConsumer.setMessageListener(new MessageListener()
-         {
-            public void onMessage(Message message)
-            {
-               try
-               {
-                  log.info("message received from topic");
-                  TextMessage textMessage = (TextMessage) message;
-                  log.info("message = " + textMessage.getText());
-               }
-               catch (JMSException e)
-               {
-                  e.printStackTrace();
-               }
-               latch.countDown();
-            }
-         });
+         //Step 6. Create a Message Producer
+         MessageProducer producer = session.createProducer(topic);
+
+         //Step 7. Create a JMS Message Consumer
+         MessageConsumer messageConsumer1 = session.createConsumer(topic);
+
+         //Step 8. Create a JMS Message Consumer
+         MessageConsumer messageConsumer2 = session.createConsumer(topic);
+         
+         //Step 9. Create a Text Message
+         TextMessage message = session.createTextMessage("This is a text message");
+
+         System.out.println("Sent message: " + message.getText());
+
+         //Step 10. Send the Message
+         producer.send(message);
+         
+         //Step 11. Start the Connection
          connection.start();
 
-         log.info("publishing message to topic");
-         messageProducer.send(message);
+         //Step 12. Receive the message
+         TextMessage messageReceived = (TextMessage) messageConsumer1.receive();
 
-         try
-         {
-            latch.await();
-         }
-         catch (InterruptedException e)
-         {
-         }
+         System.out.println("Consumer 1 Received message: " + messageReceived.getText());
+
+         //Step 13. Receive the message
+         messageReceived = (TextMessage) messageConsumer2.receive();
+
+         System.out.println("Consumer 2 Received message: " + messageReceived.getText());
       }
       finally
       {
-         if (connection != null)
+         //Step 14. Be sure to close our JMS resources!
+         if(connection != null)
          {
-            try
-            {
-               connection.close();
-            }
-            catch (JMSException e)
-            {
-               //ignore
-            }
+            connection.close();
          }
       }
    }




More information about the jboss-cvs-commits mailing list