[jboss-cvs] JBoss Messaging SVN: r6273 - in trunk/examples/jms/temp-queue: 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 06:35:42 EDT 2009


Author: gaohoward
Date: 2009-04-02 06:35:38 -0400 (Thu, 02 Apr 2009)
New Revision: 6273

Modified:
   trunk/examples/jms/temp-queue/readme.html
   trunk/examples/jms/temp-queue/src/org/jboss/jms/example/TemporaryQueueExample.java
Log:
improve the temorary queue example


Modified: trunk/examples/jms/temp-queue/readme.html
===================================================================
--- trunk/examples/jms/temp-queue/readme.html	2009-04-02 10:15:54 UTC (rev 6272)
+++ trunk/examples/jms/temp-queue/readme.html	2009-04-02 10:35:38 UTC (rev 6273)
@@ -6,8 +6,8 @@
   <body>
      <h1>JMS Temporary Queue Example</h1>
      <br>
-     <p>This example shows you how to use a TemporaryQueue object with JBoss Messaging.</p>
-     <p>TemporaryQueue is a JMS queue that lives within lifetime of its connection. Please consult the JMS 1.1 specification for full details.</p>
+     <p>This example shows you how to use a TemporaryQueue object with JBoss Messaging. First it creates a temporary queue to send and receive a message, then delete it. Then it creates another temporary queue and tries to use it after its connection is closed -- to illustrate its scope.</p>
+     <p>TemporaryQueue is a JMS queue that lives within lifetime of its connection. It is often used in request-reply type messaging where the reply is sent through a temporary destination. The temporary queue is often created as a server resource, so after using, the user should call delete() method to release the resources. 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>
@@ -68,6 +68,52 @@
            <code>message = (TextMessage) messageConsumer.receive(5000);</code>
         </pre>
 
+        <li>We close the consumer and producer before destroying the temporary queue</li>
+        <pre>
+           <code>messageConsumer.close();</code>
+           <code>messageProducer.close();</code>
+        </pre>
+
+        <li>We delete the temporary queue</li>
+        <pre>
+           <code>tempQueue.delete();</code>
+        </pre>
+
+        <li>We create another temporary queue</li>
+        <pre>
+           <code>TemporaryQueue tempQueue2 = session.createTemporaryQueue();</code>
+        </pre>
+
+        <li>We close the connection</li>
+        <pre>
+           <code>connection.close();</code>
+        </pre>
+
+        <li>We create a new connection</li>
+        <pre>
+           <code>connection = cf.createConnection();</code>
+        </pre>
+
+        <li>We create a new session</li>
+        <pre>
+           <code>session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
+        </pre>
+
+        <li>We try to access the tempQueue2 outside its lifetime, this will cause exception thrown</li>
+        <pre>
+           <code>
+         try
+         {
+            messageConsumer = session.createConsumer(tempQueue2);
+            throw new Exception("Temporary queue cannot be accessed outside its lifecycle!");
+         }
+         catch (InvalidDestinationException e)
+         {
+            System.out.println("Exception got when trying to access a temp queue outside its scope: " + e);
+         }
+           </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>
@@ -88,4 +134,4 @@
          
      </ol>
   </body>
-</html>
\ No newline at end of file
+</html>

Modified: trunk/examples/jms/temp-queue/src/org/jboss/jms/example/TemporaryQueueExample.java
===================================================================
--- trunk/examples/jms/temp-queue/src/org/jboss/jms/example/TemporaryQueueExample.java	2009-04-02 10:15:54 UTC (rev 6272)
+++ trunk/examples/jms/temp-queue/src/org/jboss/jms/example/TemporaryQueueExample.java	2009-04-02 10:35:38 UTC (rev 6273)
@@ -23,21 +23,14 @@
 
 import javax.jms.Connection;
 import javax.jms.ConnectionFactory;
+import javax.jms.InvalidDestinationException;
 import javax.jms.JMSException;
-import javax.jms.Message;
 import javax.jms.MessageConsumer;
-import javax.jms.MessageListener;
 import javax.jms.MessageProducer;
-import javax.jms.Queue;
-import javax.jms.QueueConnection;
-import javax.jms.QueueConnectionFactory;
-import javax.jms.QueueSession;
 import javax.jms.Session;
+import javax.jms.TemporaryQueue;
 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 temporary queues.
@@ -72,7 +65,7 @@
          Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
          
          // Step 6. Create a Temporary Queue
-         Queue tempQueue = session.createTemporaryQueue();
+         TemporaryQueue tempQueue = session.createTemporaryQueue();
          
          System.out.println("Temporary queue is created: " + tempQueue);
          
@@ -94,6 +87,39 @@
          message = (TextMessage) messageConsumer.receive(5000);
 
          System.out.println("Received message: " + message.getText());
+         
+         // Step 13. Close the consumer and producer
+         messageConsumer.close();
+         messageProducer.close();
+         
+         // Step 14. Delete the temporary queue
+         tempQueue.delete();
+         
+         // Step 15. Create another temporary queue.
+         TemporaryQueue tempQueue2 = session.createTemporaryQueue();
+         
+         System.out.println("Another temporary queue is created: " + tempQueue2);
+         
+         // Step 16. Close the connection.
+         connection.close();
+         
+         // Step 17. Create a new connection.
+         connection = cf.createConnection();
+         
+         // Step 18. Create a new session.
+         session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         
+         // Step 19. Try to access the tempQueue2 outside its lifetime
+         try
+         {
+            messageConsumer = session.createConsumer(tempQueue2);
+            throw new Exception("Temporary queue cannot be accessed outside its lifecycle!");
+         }
+         catch (InvalidDestinationException e)
+         {
+            System.out.println("Exception got when trying to access a temp queue outside its scope: " + e);
+         }
+         
       }
       finally
       {
@@ -101,7 +127,7 @@
          {
             try
             {
-               // Step 13. Be sure to close our JMS resources!
+               // Step 20. Be sure to close our JMS resources!
                connection.close();
             }
             catch (JMSException e)




More information about the jboss-cvs-commits mailing list