[jboss-cvs] JBoss Messaging SVN: r6276 - 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 09:48:05 EDT 2009


Author: gaohoward
Date: 2009-04-02 09:48:05 -0400 (Thu, 02 Apr 2009)
New Revision: 6276

Modified:
   trunk/examples/jms/request-reply/readme.html
   trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java
Log:
improve the request-reply example: using a independent class to handle request, to simulate client/server interaction. Also reuse the jms objects.


Modified: trunk/examples/jms/request-reply/readme.html
===================================================================
--- trunk/examples/jms/request-reply/readme.html	2009-04-02 11:31:55 UTC (rev 6275)
+++ trunk/examples/jms/request-reply/readme.html	2009-04-02 13:48:05 UTC (rev 6276)
@@ -6,7 +6,7 @@
   <body>
      <h1>JMS Request-Reply Example</h1>
      <br>
-     <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>This example shows you how to handle a request message and receive a reply. To get a reply message, the requesting client creates a temporary queue. 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 a SimpleRequestServer, who is listening to the request queue for incoming requests. If a request message has arrived, it extracts the reply queue from the request message by JMSReplyTo header, and sends back a reply message. To let the client know to which request message a reply message is related, the server also set the JMSCorrelationID header to the 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>
@@ -15,8 +15,15 @@
      <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>
+
+        <li>We start the request server</li>
         <pre>
+           <code>SimpleRequestServer server = new SimpleRequestServer();</code>
+           <code>server.start();</code>
+        </pre>
+
+        <li>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>
 
@@ -55,21 +62,11 @@
            <code>TemporaryQueue replyQueue = session.createTemporaryQueue();</code>
         </pre>
 
-        <li>We create consumer to receive reply message</li>
+        <li>We create a 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>
@@ -100,10 +97,15 @@
            <code>replyConsumer.close();</code>
         </pre>
 
-        <li>We elete the temporary queue</li>
+        <li>We delete the temporary queue</li>
         <pre>
            <code>replyQueue.delete();</code>
         </pre>
+
+        <li>We shutdown the request server</li>
+        <pre>
+           <code>server.shutdown();</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>
 
@@ -122,29 +124,14 @@
         </pre>
      </ol>
 
-     In SimpleMessageHandler.onMessage(),
+     Request Messages are handled in SimpleRequestServer.onMessage(),
 
-     <ol>
-        <li>Create a connection</li>
-        <pre>
-           <code>connection = cf.createConnection();</code>
-        </pre>
-
-        <li>Create a session</li>
-        <pre>
-           <code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
-        </pre>
-            
+     <ol>            
         <li>Extract the ReplyTo destination</li>
         <pre>
            <code>Destination replyDestination = request.getJMSReplyTo();</code>
         </pre>
 
-        <li>Create a producer to send the reply message</li>
-        <pre>
-           <code>MessageProducer replyProducer = session.createProducer(replyDestination);</code>
-        </pre>
-
         <li>Create the reply message</li>
         <pre>
            <code>TextMessage replyMessage = session.createTextMessage("A reply message");</code>
@@ -159,28 +146,6 @@
         <pre>
            <code>replyProducer.send(replyMessage);</code>
         </pre>
-            
-        <li>Close the connection in the end</li>
-        <pre>
-           <code>
-         finally
-         {
-            if (connection != null)
-            {
-               try 
-               {
-                  //Close the connection
-                  connection.close();
-               }
-               catch (JMSException e)
-               {
-                  e.printStackTrace();
-               }
-            }
-         }
-           </code>
-        </pre>
-
      </ol>
      
   </body>

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 11:31:55 UTC (rev 6275)
+++ trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java	2009-04-02 13:48:05 UTC (rev 6276)
@@ -52,55 +52,53 @@
       Connection connection = null;
       try
       {
-         //Step 1. Create an initial context to perform the JNDI lookup.
+         
+         //Step 1. Start the request server
+         SimpleRequestServer server = new SimpleRequestServer();
+         server.start();
+
+         //Step 2. Create an initial context to perform the JNDI lookup.
          InitialContext initialContext = getContext();
 
-         //Step 2. Lookup the queue for sending the request message
+         //Step 3. Lookup the queue for sending the request message
          Queue requestQueue = (Queue) initialContext.lookup("/queue/exampleQueue");         
 
-         //Step 3. Lookup for the Connection Factory
+         //Step 4. Lookup for the Connection Factory
          ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
 
-         //Step 4. Create a JMS Connection
+         //Step 5. Create a JMS Connection
          connection = cf.createConnection();
          
-         //Step 5. Start the connection.
+         //Step 6. Start the connection.
          connection.start();
 
-         //Step 6. Create a JMS Session
+         //Step 7. Create a JMS Session
          Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
 
-         //Step 7. Create a JMS Message Producer to send request message
+         //Step 8. Create a JMS Message Producer to send request message
          MessageProducer producer = session.createProducer(requestQueue);
          
-         //Step 8. Create a temporary queue used to send reply message
+         //Step 9. Create a temporary queue used to send reply message
          TemporaryQueue replyQueue = session.createTemporaryQueue();
          
-         
-         //Step 9. Create consumer to receive reply message
+         //Step 10. 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 12. Create a request Text Message
+         //Step 11. Create a request Text Message
          TextMessage requestMsg = session.createTextMessage("A request message");
          
-         //Step 13. Set the ReplyTo header so that the request receiver knows where to send the reply.
+         //Step 12. Set the ReplyTo header so that the request receiver knows where to send the reply.
          requestMsg.setJMSReplyTo(replyQueue);
          
-         //Step 14. Set the CorrelationID so that it can be linked with corresponding reply message
+         //Step 13. Set the CorrelationID so that it can be linked with corresponding reply message
          requestMsg.setJMSCorrelationID("jbm-id: 0000001");
          
-         //Step 15. Sent the request message
+         //Step 14. Sent the request message
          producer.send(requestMsg);
          
          System.out.println("Request message sent.");
          
-         //Step 16. Receive the reply message.
+         //Step 15. Receive the reply message.
          TextMessage replyMessageReceived = (TextMessage)replyConsumer.receive();
          
          System.out.println("Received reply: " + replyMessageReceived.getText());
@@ -112,6 +110,9 @@
          //Step 18. Delete the temporary queue
          replyQueue.delete();
          
+         //Step 19. Shutdown the request server
+         server.shutdown();
+         
       }
       finally
       {
@@ -123,36 +124,54 @@
       }
    }
    
-   private class SimpleRequestHandler implements MessageListener
+   private class SimpleRequestServer implements MessageListener
    {
-      private ConnectionFactory cf;
+      private Connection connection;
+      private Session session;
+      MessageProducer replyProducer;
+      MessageConsumer requestConsumer;
       
-      public SimpleRequestHandler(ConnectionFactory cfact)
+      public void start() throws Exception
       {
-         cf = cfact;
+         //Get an initial context to perform the JNDI lookup.
+         InitialContext initialContext = getContext();
+
+         //Lookup the queue to receive the request message
+         Queue requestQueue = (Queue) initialContext.lookup("/queue/exampleQueue");         
+
+         //Lookup for the Connection Factory
+         ConnectionFactory cfact = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
+         
+         // Create a connection
+         connection = cfact.createConnection();
+         
+         // Start the connection;
+         connection.start();
+         
+         // Create a session
+         session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         
+         // Create a producer to send the reply message
+         replyProducer = session.createProducer(null);
+         
+         //Create the request comsumer
+         requestConsumer = session.createConsumer(requestQueue);
+         
+         //register the listener
+         requestConsumer.setMessageListener(this);
       }
-      
+
       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("Reply to queue: " + replyDestination);
 
-            System.out.println("Got reply queue: " + replyDestination);
-
             // Create the reply message
             TextMessage replyMessage = session.createTextMessage("A reply message");
 
@@ -160,32 +179,20 @@
             replyMessage.setJMSCorrelationID(request.getJMSCorrelationID());
 
             // Send out the reply message
-            replyProducer.send(replyMessage);
-
-            // Close the producer on the replyQueue
-            replyProducer.close();
+            replyProducer.send(replyDestination, replyMessage);
+            
+            System.out.println("Reply sent");
          }
          catch (JMSException e)
          {
             e.printStackTrace();
          }
-         finally
-         {
-            if (connection != null)
-            {
-               try 
-               {
-                  //Close the connection
-                  connection.close();
-               }
-               catch (JMSException e)
-               {
-                  e.printStackTrace();
-               }
-            }
-         }
       }
       
+      public void shutdown() throws JMSException
+      {
+         connection.close();
+      }
    }
 
 }




More information about the jboss-cvs-commits mailing list