[jboss-cvs] JBoss Messaging SVN: r6229 - in trunk/examples/jms/durable: 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 06:20:31 EDT 2009


Author: timfox
Date: 2009-03-31 06:20:30 -0400 (Tue, 31 Mar 2009)
New Revision: 6229

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

Added: trunk/examples/jms/durable/readme.html
===================================================================
--- trunk/examples/jms/durable/readme.html	                        (rev 0)
+++ trunk/examples/jms/durable/readme.html	2009-03-31 10:20:30 UTC (rev 6229)
@@ -0,0 +1,134 @@
+<html>
+  <head>
+    <title>JBoss Messaging JMS Durable Subscription Example</title>
+  </head>
+  <body>
+     <h1>JMS Durable Subscription Example</h1>
+     <br>
+     <p>This example shows you how to use a durable subscription with JBoss Messaging.</p>
+     <p>Durable subscriptions are a standard part of JMS, please consult the JMS 1.1 specification for full details.</p>
+     <p>Unlike non durable subscriptions, the key function of durable subscriptions is that the messages contained in them persist longer than the lifetime of the subscriber - i.e. they will accumulate messages sent to the topic even if there is no active subscriber on them. They will also survive server restarts. Note that for the messages to be persisted, the messages sent to them must be marked as persistent messages.</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>jndi.properties</code> file in the directory <code>../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 set the client-id on the connection. This must be the <b>first operation</b> performed on the connection object.
+        The combination of client-id and durable subscription name uniquely identifies the durable subscription. Maybe different durable subscritions can have the same name if they belong to different client-id values</li>
+        <pre>
+           <code>connection.setClientID("durable-client");</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 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 the durable subscriberon the topic, specifying it's name. Since this is the first time the subscriber is created and a subscription with that name and for this clien-id does not already exist, then the underlying durable subscription will be created, and a subscriber will be created and returned for that subscription.</li>
+	<pre>
+           <code>TopicSubscriber subscriber = session.createDurableSubscriber(topic, "subscriber-1");</code>
+        </pre>
+         
+        <li>We create a JMS text message, message 1, that we are going to send. Note that it must be a persistent message in order to survive server restart.</li>
+        <pre>
+           <code>TextMessage message1 = session.createTextMessage("This is a text message 1");</code>
+        </pre>
+   
+        <li>We send message 1 to the topic</li>
+        <pre>
+           <code>messageProducer.send(message1);</code>
+        </pre>
+
+        <li>The message arrives in the subscription, and we consume the message from the subscription.</li>
+        <pre>
+           <code>TextMessage messageReceived = (TextMessage)subscriber.receive();</code>
+        </pre>
+
+        <li>We create and send another text message, message 2, to the same topic</li>
+	<pre>
+           <code>TextMessage message2 = session.createTextMessage("This is a text message 2");</code>
+	   <br>
+           <code>messageProducer.send(message2);</code>
+        </pre>
+         
+        <li>Now we close the subscriber. Since the subscription is durable it will continue to survive even though there is no subscriber attached to it. At this point you could even stop and restart the server and the subscription would survive!</li>
+
+        <pre>
+           <code>subscriber.close();</code>
+        </pre>
+         
+        <li>We now create another durable subscriber, with the same name and same client-id on the same topic. Since the durable subscrition already exists, it will simply return a new subscriber consuming from the <i>same</i> durable subscription instance as before</li>
+
+        <pre> 
+           <code>subscriber = session.createDurableSubscriber(topic, "subscriber-1");</code>
+        </pre>
+         
+        <li>We consume message 2 which was sent before the first subscriber was closed.</li>
+
+        <pre>
+           <code>messageReceived = (TextMessage)subscriber.receive();</code>
+        </pre>
+
+        <li>We close the second subscriber.</li>
+
+        <pre>
+           <code>subscriber.close();</code>
+        </pre>
+
+        <li>Now we <i>delete</i> the underlying durable subscription. This will delete any remaining unacknowledged messages in the subscription and a new subscriber will not be able to access them</li>
+
+        <pre>
+           <code>session.unsubscribe("subscriber-1");</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 sessions, consumers, producer and browser objects</li>
+
+        <pre>
+           <code>
+
+      finally
+      {
+         if (connection != null)
+         {
+            // Step 20. Be sure to close our JMS resources!
+            connection.close();
+         }
+      }
+           </code>
+        </pre>
+
+
+         
+     </ol>
+  </body>
+</html>
\ No newline at end of file

Modified: trunk/examples/jms/durable/src/org/jboss/jms/example/DurableSubscriberExample.java
===================================================================
--- trunk/examples/jms/durable/src/org/jboss/jms/example/DurableSubscriberExample.java	2009-03-31 09:59:55 UTC (rev 6228)
+++ trunk/examples/jms/durable/src/org/jboss/jms/example/DurableSubscriberExample.java	2009-03-31 10:20:30 UTC (rev 6229)
@@ -23,17 +23,12 @@
 
 import javax.jms.Connection;
 import javax.jms.ConnectionFactory;
-import javax.jms.JMSException;
-import javax.jms.Message;
-import javax.jms.MessageConsumer;
-import javax.jms.MessageListener;
 import javax.jms.MessageProducer;
 import javax.jms.Session;
 import javax.jms.TextMessage;
 import javax.jms.Topic;
 import javax.jms.TopicSubscriber;
 import javax.naming.InitialContext;
-import java.util.concurrent.CountDownLatch;
 
 /**
  * A simple JMS example that shows how to use a durable subcriber.
@@ -52,80 +47,80 @@
       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
          Topic topic = (Topic) initialContext.lookup("/topic/exampleTopic");
+         
+         //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. Set the client-id on the connection
          connection.setClientID("durable-client");
+         
+         //Step 6. Start the connection
+         connection.start();
+         
+         //Step 7. Create a JMS session
          Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
          
+         //Step 8. Create a JMS message producer
          MessageProducer messageProducer = session.createProducer(topic);
 
-         TopicSubscriber subscriber1 = session.createDurableSubscriber(topic, "subscriber-1");
-         TopicSubscriber subscriber2 = session.createDurableSubscriber(topic, "subscriber-2");
-         Message message = session.createTextMessage("This is a text message!");
-         final CountDownLatch latch = new CountDownLatch(2);
-         subscriber1.setMessageListener(new MessageListener()
-         {
-            public void onMessage(Message message)
-            {
-               try
-               {
-                  log.info("message received by subscriber1 from topic");
-                  TextMessage textMessage = (TextMessage) message;
-                  log.info("message = " + textMessage.getText());
-               }
-               catch (JMSException e)
-               {
-                  e.printStackTrace();
-               }
-               latch.countDown();
-            }
-         });
-         subscriber2.setMessageListener(new MessageListener()
-         {
-            public void onMessage(Message message)
-            {
-               try
-               {
-                  log.info("message received by subscriber2 from topic");
-                  TextMessage textMessage = (TextMessage) message;
-                  log.info("message = " + textMessage.getText());
-               }
-               catch (JMSException e)
-               {
-                  e.printStackTrace();
-               }
-               latch.countDown();
-            }
-         });
-         connection.start();
-
-         log.info("publishing message to topic");
-         messageProducer.send(message);
-
-         try
-         {
-            latch.await();
-         }
-         catch (InterruptedException e)
-         {
-         }
+         //Step 9. Create the subscription and the subscriber.
+         TopicSubscriber subscriber = session.createDurableSubscriber(topic, "subscriber-1");
+         
+         //Step 10. Create a text message
+         TextMessage message1 = session.createTextMessage("This is a text message 1");
+         
+         //Step 11. Send the text message to the topic
+         messageProducer.send(message1);
+         
+         System.out.println("Sent message: " + message1.getText());
+         
+         //Step 12. Consume the message from the durable subscription
+                           
+         TextMessage messageReceived = (TextMessage)subscriber.receive();
+         
+         System.out.println("Received message: " + messageReceived.getText());
+         
+         //Step 13. Create and send another message
+         
+         TextMessage message2 = session.createTextMessage("This is a text message 2");
+         
+         messageProducer.send(message2);
+         
+         System.out.println("Sent message: " + message2.getText());
+         
+         //Step 14. Close the subscriber - the server could even be stopped at this point!
+         subscriber.close();
+         
+         //Step 15. Create a new subscriber on the *same* durable subscription.
+         
+         subscriber = session.createDurableSubscriber(topic, "subscriber-1");
+         
+         //Step 16. Consume the message
+         
+         messageReceived = (TextMessage)subscriber.receive();
+         
+         System.out.println("Received message: " + messageReceived.getText());
+         
+         //Step 17. Close the subscriber
+         subscriber.close();
+         
+         //Step 18. Delete the durable subscription
+         session.unsubscribe("subscriber-1");
       }
-
       finally
       {
          if (connection != null)
          {
-            try
-            {
-               connection.close();
-            }
-            catch (JMSException e)
-            {
-               e.printStackTrace();
-            }
+            // Step 19. Be sure to close our JMS resources!
+            connection.close();
          }
       }
    }




More information about the jboss-cvs-commits mailing list