[jboss-cvs] JBoss Messaging SVN: r6275 - in trunk/examples/jms/request-reply: 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 07:31:55 EDT 2009


Author: gaohoward
Date: 2009-04-02 07:31:55 -0400 (Thu, 02 Apr 2009)
New Revision: 6275

Modified:
   trunk/examples/jms/request-reply/readme.html
   trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java
Log:
request-reply example: Using a message listener to handle request message


Modified: trunk/examples/jms/request-reply/readme.html
===================================================================
--- trunk/examples/jms/request-reply/readme.html	2009-04-02 10:44:36 UTC (rev 6274)
+++ trunk/examples/jms/request-reply/readme.html	2009-04-02 11:31:55 UTC (rev 6275)
@@ -6,8 +6,8 @@
   <body>
      <h1>JMS Request-Reply Example</h1>
      <br>
-     <p>This example shows you how to send a request message and receive a reply. It first sends a request message with a JMSReplyTo header set to get a reply, then after 
-     receiving the request message, forges a reply message and send out to the reply queue. Then it receives the reply message.</p>
+     <p>This example shows you how to send a request message and receive a reply. It creates a temporary queue and register a message listener to handle the request. Then it sends out the request message with JMSReplyTo set to the temporary queue and a JMSCorrelationID to identify the request. The request message is handled by the message listener, who on receiving the request message, extracts the reply queue from the request message by JMSReplyTo header, and sends back a reply message. </p>
+
      <p>Request/Reply style messaging is supported through standard JMS message headers JMSReplyTo and JMSCorrelationID. This is often used in request-reply style communications between applications.
      Whenever a client sends a message that expects a response, it can use this mechanism to implement. please consult the JMS 1.1 specification for full details.</p>
      <br>
@@ -55,11 +55,21 @@
            <code>TemporaryQueue replyQueue = session.createTemporaryQueue();</code>
         </pre>
 
+        <li>We create consumer to receive reply message</li>
+        <pre>
+           <code>MessageConsumer replyConsumer = session.createConsumer(replyQueue);</code>
+        </pre>
+
         <li>We create a JMS Message Consumer to receive request</li>
         <pre>
            <code>MessageConsumer messageConsumer = session.createConsumer(requestQueue);</code>
         </pre>
 
+        <li>We register a message listener as request handler, the request handler will send the reply message on receiving the request message</li>
+        <pre>
+           <code>messageConsumer.setMessageListener(new SimpleRequestHandler(cf));</code>
+        </pre>
+
         <li>We create a request Text Message</li>
         <pre>
            <code>TextMessage requestMsg = session.createTextMessage("A request message");</code>
@@ -80,75 +90,98 @@
            <code>producer.send(requestMsg);</code>
         </pre>
 
-        <li>We receive the request message</li>
+        <li>We receive the reply message</li>
         <pre>
-           <code>TextMessage requestMsgReceived = (TextMessage) messageConsumer.receive(5000);</code>
+           <code>TextMessage replyMessageReceived = (TextMessage)replyConsumer.receive();</code>
         </pre>
 
-        <li>We extract the ReplyTo destination from the request message</li>
+        <li>We close the consumer and producer on the replyQueue</li>
         <pre>
-           <code>Destination replyDestination = requestMsgReceived.getJMSReplyTo();</code>
+           <code>replyConsumer.close();</code>
         </pre>
 
-        <li>We create a reply message</li>
+        <li>We elete the temporary queue</li>
         <pre>
-           <code>TextMessage replyMessage = session.createTextMessage("A reply message");</code>
+           <code>replyQueue.delete();</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>
 
-        <li>We set the CorrelationID</li>
         <pre>
-           <code>replyMessage.setJMSCorrelationID(requestMsgReceived.getJMSCorrelationID());</code>
+           <code>
+
+      finally
+      {
+         if (connection != null)
+         {
+            // Step 19. Be sure to close our JMS resources!
+            connection.close();
+         }
+      }
+           </code>
         </pre>
+     </ol>
 
-        <li>We create a producer to send the reply message</li>
+     In SimpleMessageHandler.onMessage(),
+
+     <ol>
+        <li>Create a connection</li>
         <pre>
-           <code>MessageProducer replyProducer = session.createProducer(replyDestination);</code>
+           <code>connection = cf.createConnection();</code>
         </pre>
 
-        <li>We send out the reply message</li>
+        <li>Create a session</li>
         <pre>
-           <code>replyProducer.send(replyMessage);</code>
+           <code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
         </pre>
-
-        <li>We create consumer to receive reply message</li>
+            
+        <li>Extract the ReplyTo destination</li>
         <pre>
-           <code>MessageConsumer replyConsumer = session.createConsumer(replyDestination);</code>
+           <code>Destination replyDestination = request.getJMSReplyTo();</code>
         </pre>
 
-        <li>We receive the reply message</li>
+        <li>Create a producer to send the reply message</li>
         <pre>
-           <code>TextMessage replyMessageReceived = (TextMessage)replyConsumer.receive(5000);</code>
+           <code>MessageProducer replyProducer = session.createProducer(replyDestination);</code>
         </pre>
 
-        <li>We close the consumer and producer on the replyQueue</li>
+        <li>Create the reply message</li>
         <pre>
-           <code>replyConsumer.close();</code>
-           <code>replyProducer.close();</code>
+           <code>TextMessage replyMessage = session.createTextMessage("A reply message");</code>
         </pre>
 
-        <li>We elete the temporary queue</li>
+        <li>Set the CorrelationID</li>
         <pre>
-           <code>replyQueue.delete();</code>
+           <code>replyMessage.setJMSCorrelationID(request.getJMSCorrelationID());</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>
 
+        <li>Send out the reply message</li>
         <pre>
+           <code>replyProducer.send(replyMessage);</code>
+        </pre>
+            
+        <li>Close the connection in the end</li>
+        <pre>
            <code>
-
-      finally
-      {
-         if (connection != null)
+         finally
          {
-            // Step 22. Be sure to close our JMS resources!
-            connection.close();
+            if (connection != null)
+            {
+               try 
+               {
+                  //Close the connection
+                  connection.close();
+               }
+               catch (JMSException e)
+               {
+                  e.printStackTrace();
+               }
+            }
          }
-      }
            </code>
         </pre>
 
-
-
      </ol>
+     
   </body>
 </html>

Modified: trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java
===================================================================
--- trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java	2009-04-02 10:44:36 UTC (rev 6274)
+++ trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java	2009-04-02 11:31:55 UTC (rev 6275)
@@ -24,7 +24,10 @@
 import javax.jms.Connection;
 import javax.jms.ConnectionFactory;
 import javax.jms.Destination;
+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.Session;
@@ -53,7 +56,7 @@
          InitialContext initialContext = getContext();
 
          //Step 2. Lookup the queue for sending the request message
-         Queue requestQueue = (Queue) initialContext.lookup("/queue/exampleQueue");
+         Queue requestQueue = (Queue) initialContext.lookup("/queue/exampleQueue");         
 
          //Step 3. Lookup for the Connection Factory
          ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
@@ -73,71 +76,116 @@
          //Step 8. Create a temporary queue used to send reply message
          TemporaryQueue replyQueue = session.createTemporaryQueue();
          
-         //Step 9. Create a JMS Message Consumer
+         
+         //Step 9. Create consumer to receive reply message
+         MessageConsumer replyConsumer = session.createConsumer(replyQueue);
+
+         //Step 10. Create a JMS Message Consumer
          MessageConsumer messageConsumer = session.createConsumer(requestQueue);
+         
+         //Step 11. Register a message listener as request handler
+         messageConsumer.setMessageListener(new SimpleRequestHandler(cf));
 
-         //Step 10. Create a request Text Message
+         //Step 12. Create a request Text Message
          TextMessage requestMsg = session.createTextMessage("A request message");
          
-         //Step 11. Set the ReplyTo header so that the request receiver knows where to send the reply.
+         //Step 13. Set the ReplyTo header so that the request receiver knows where to send the reply.
          requestMsg.setJMSReplyTo(replyQueue);
          
-         //Step 12. Set the CorrelationID so that it can be linked with corresponding reply message
+         //Step 14. Set the CorrelationID so that it can be linked with corresponding reply message
          requestMsg.setJMSCorrelationID("jbm-id: 0000001");
          
-         //Step 13. Sent the request message
+         //Step 15. Sent the request message
          producer.send(requestMsg);
          
          System.out.println("Request message sent.");
          
-         //Step 14. Receive the request message
-         TextMessage requestMsgReceived = (TextMessage) messageConsumer.receive(5000);
-
-         System.out.println("Received message: " + requestMsgReceived.getText());
-         System.out.println("The request CorrelationID: " + requestMsgReceived.getJMSCorrelationID());
-
-         //Step 15. Extract the ReplyTo destination
-         Destination replyDestination = requestMsgReceived.getJMSReplyTo();
+         //Step 16. Receive the reply message.
+         TextMessage replyMessageReceived = (TextMessage)replyConsumer.receive();
          
-         System.out.println("Got reply queue: " + replyDestination);
-         
-         //Step 16. Create a reply message
-         TextMessage replyMessage = session.createTextMessage("A reply message");
-         
-         //Step 17. Set the CorrelationID
-         replyMessage.setJMSCorrelationID(requestMsgReceived.getJMSCorrelationID());
-         
-         //Step 18. Create a producer to send the reply message
-         MessageProducer replyProducer = session.createProducer(replyDestination);
-         
-         //Step 19. Send out the reply message
-         replyProducer.send(replyMessage);
-         
-         //Step 20. Create consumer to receive reply message
-         MessageConsumer replyConsumer = session.createConsumer(replyDestination);
-         
-         //Step 21. Receive the reply message.
-         TextMessage replyMessageReceived = (TextMessage)replyConsumer.receive(5000);
-         
          System.out.println("Received reply: " + replyMessageReceived.getText());
          System.out.println("CorrelatedId: " + replyMessageReceived.getJMSCorrelationID());
-
-         //Step 22 closing the consumer and producer on the replyQueue
+         
+         //Step 17. close the consumer.
          replyConsumer.close();
-         replyProducer.close();
          
-         //Step 23. Delete the temporary queue
+         //Step 18. Delete the temporary queue
          replyQueue.delete();
          
       }
       finally
       {
-         //Step 24. Be sure to close our JMS resources!
+         //Step 19. Be sure to close our JMS resources!
          if(connection != null)
          {
             connection.close();
          }
       }
    }
+   
+   private class SimpleRequestHandler implements MessageListener
+   {
+      private ConnectionFactory cf;
+      
+      public SimpleRequestHandler(ConnectionFactory cfact)
+      {
+         cf = cfact;
+      }
+      
+      public void onMessage(Message request)
+      {
+         Connection connection = null;
+         try
+         {
+            System.out.println("Received request message: " + ((TextMessage)request).getText());
 
+            // Create a connection
+            connection = cf.createConnection();
+
+            // Create a session
+            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+            // Extract the ReplyTo destination
+            Destination replyDestination = request.getJMSReplyTo();
+
+            // Create a producer to send the reply message
+            MessageProducer replyProducer = session.createProducer(replyDestination);
+
+            System.out.println("Got reply queue: " + replyDestination);
+
+            // Create the reply message
+            TextMessage replyMessage = session.createTextMessage("A reply message");
+
+            // Set the CorrelationID
+            replyMessage.setJMSCorrelationID(request.getJMSCorrelationID());
+
+            // Send out the reply message
+            replyProducer.send(replyMessage);
+
+            // Close the producer on the replyQueue
+            replyProducer.close();
+         }
+         catch (JMSException e)
+         {
+            e.printStackTrace();
+         }
+         finally
+         {
+            if (connection != null)
+            {
+               try 
+               {
+                  //Close the connection
+                  connection.close();
+               }
+               catch (JMSException e)
+               {
+                  e.printStackTrace();
+               }
+            }
+         }
+      }
+      
+   }
+
 }




More information about the jboss-cvs-commits mailing list