[jboss-cvs] JBoss Messaging SVN: r6258 - in trunk: examples/jms and 6 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 1 07:13:04 EDT 2009


Author: gaohoward
Date: 2009-04-01 07:13:04 -0400 (Wed, 01 Apr 2009)
New Revision: 6258

Added:
   trunk/examples/jms/request-reply/
   trunk/examples/jms/request-reply/build.xml
   trunk/examples/jms/request-reply/readme.html
   trunk/examples/jms/request-reply/src/
   trunk/examples/jms/request-reply/src/org/
   trunk/examples/jms/request-reply/src/org/jboss/
   trunk/examples/jms/request-reply/src/org/jboss/jms/
   trunk/examples/jms/request-reply/src/org/jboss/jms/example/
   trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java
Modified:
   trunk/.classpath
Log:
add simple request-reply example


Modified: trunk/.classpath
===================================================================
--- trunk/.classpath	2009-04-01 10:55:48 UTC (rev 6257)
+++ trunk/.classpath	2009-04-01 11:13:04 UTC (rev 6258)
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
 	<classpathentry excluding="**/.svn/**/*" kind="src" path="src/main"/>
+	<classpathentry kind="src" path="examples/jms/request-reply/src"/>
 	<classpathentry kind="src" path="examples/jms/temp-queue/src"/>
 	<classpathentry kind="src" path="examples/jms/topic-selector/src"/>
 	<classpathentry kind="src" path="examples/jms/transactional/src"/>

Added: trunk/examples/jms/request-reply/build.xml
===================================================================
--- trunk/examples/jms/request-reply/build.xml	                        (rev 0)
+++ trunk/examples/jms/request-reply/build.xml	2009-04-01 11:13:04 UTC (rev 6258)
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE project [
+      <!ENTITY libraries SYSTEM "../../../thirdparty/libraries.ent">
+      ]>
+
+<!-- =========================================================================================== -->
+<!--                                                                                             -->
+<!-- JBoss, Home of Professional Open Source                                                     -->
+<!-- Copyright 2005, JBoss Inc., and individual contributors as indicated                        -->
+<!-- by the @authors tag. See the copyright.txt in the distribution for a                        -->
+<!-- full listing of individual contributors.                                                    -->
+<!--                                                                                             -->
+<!-- This is free software; you can redistribute it and/or modify it                             -->
+<!-- under the terms of the GNU Lesser General Public License as                                 -->
+<!-- published by the Free Software Foundation; either version 2.1 of                            -->
+<!-- the License, or (at your option) any later version.                                         -->
+<!--                                                                                             -->
+<!-- This software is distributed in the hope that it will be useful,                            -->
+<!-- but WITHOUT ANY WARRANTY; without even the implied warranty of                              -->
+<!-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU                            -->
+<!-- Lesser General Public License for more details.                                             -->
+<!--                                                                                             -->
+<!-- You should have received a copy of the GNU Lesser General Public                            -->
+<!-- License along with this software; if not, write to the Free                                 -->
+<!-- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA                          -->
+<!-- 02110-1301 USA, or see the FSF site: http://www.fsf.org.                                    -->
+<!--                                                                                             -->
+<!-- =========================================================================================== -->
+
+
+<project default="run" name="JBoss Messaging JMS Request-Reply Example">
+
+   <import file="../common/build.xml"/>
+
+   <target name="run">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.RequestReplyExample"/>
+      </antcall>
+   </target>
+
+   <target name="runRemote">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.RequestReplyExample"/>
+         <param name="jbm.example.runServer" value="false"/>
+      </antcall>
+   </target>
+
+</project>

Added: trunk/examples/jms/request-reply/readme.html
===================================================================
--- trunk/examples/jms/request-reply/readme.html	                        (rev 0)
+++ trunk/examples/jms/request-reply/readme.html	2009-04-01 11:13:04 UTC (rev 6258)
@@ -0,0 +1,142 @@
+<html>
+  <head>
+    <title>JBoss Messaging JMS Request-Reply Example</title>
+    <link rel="stylesheet" type="text/css" href="../common/common.css">
+  </head>
+  <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 and receives the reply message.</p>
+     <p>Request/Reply style messaging are supported through standard JMS message headers JMSReplyTo and JMSCorrelationID, 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>
+     <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 lookup the queue for sending the request message</li>
+        <pre>
+           <code>Queue requestQueue = (Queue) initialContext.lookup("/queue/exampleQueue");</code>
+        </pre>
+
+        <li>We lookup for the Connection Factory</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 start the connection</li>
+        <pre>
+           <code>connection.start();</code>
+        </pre>
+
+        <li>We create a JMS Session</li>
+        <pre>
+           <code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
+        </pre>
+
+        <li>We create a JMS Message Producer to send request message</li>
+        <pre>
+           <code>MessageProducer producer = session.createProducer(requestQueue);</code>
+        </pre>
+
+        <li>We create a temporary queue used to send reply message to and receive reply from</li>
+        <pre>
+           <code>TemporaryQueue replyQueue = session.createTemporaryQueue();</code>
+        </pre>
+
+        <li>We create a request Text Message</li>
+        <pre>
+           <code>TextMessage requestMsg = session.createTextMessage("A request message");</code>
+        </pre>
+
+        <li>We set the ReplyTo header so that the request receiver knows where to send the reply.</li>
+        <pre>
+           <code>requestMsg.setJMSReplyTo(replyQueue);</code>
+        </pre>
+
+        <li>We set the CorrelationID so that it can be linked with corresponding reply message</li>
+        <pre>
+           <code>requestMsg.setJMSCorrelationID("jbm-id: 0000001");</code>
+        </pre>
+
+        <li>We sent the request message</li>
+        <pre>
+           <code>producer.send(requestMsg);</code>
+        </pre>
+
+        <li>We create a JMS Message Consumer to receive request</li>
+        <pre>
+           <code>MessageConsumer messageConsumer = session.createConsumer(requestQueue);</code>
+        </pre>
+
+        <li>We receive the request message</li>
+        <pre>
+           <code>TextMessage requestMsgReceived = (TextMessage) messageConsumer.receive(5000);</code>
+        </pre>
+
+        <li>We extract the ReplyTo destination from the request message</li>
+        <pre>
+           <code>Destination replyDestination = requestMsgReceived.getJMSReplyTo();</code>
+        </pre>
+
+        <li>We create a reply message</li>
+        <pre>
+           <code>TextMessage replyMessage = session.createTextMessage("A reply message");</code>
+        </pre>
+
+        <li>We set the CorrelationID</li>
+        <pre>
+           <code>replyMessage.setJMSCorrelationID(requestMsgReceived.getJMSCorrelationID());</code>
+        </pre>
+
+        <li>We create a producer to send the reply message</li>
+        <pre>
+           <code>MessageProducer replyProducer = session.createProducer(replyDestination);</code>
+        </pre>
+
+        <li>We send out the reply message</li>
+        <pre>
+           <code>replyProducer.send(replyMessage);</code>
+        </pre>
+
+        <li>We create consumer to receive reply message</li>
+        <pre>
+           <code>MessageConsumer replyConsumer = session.createConsumer(replyDestination);</code>
+        </pre>
+
+        <li>We receive the reply message</li>
+        <pre>
+           <code>TextMessage replyMessageReceived = (TextMessage)replyConsumer.receive(5000);</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 22. Be sure to close our JMS resources!
+            connection.close();
+         }
+      }
+           </code>
+        </pre>
+
+
+
+     </ol>
+  </body>
+</html>
\ No newline at end of file

Added: trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java
===================================================================
--- trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java	                        (rev 0)
+++ trunk/examples/jms/request-reply/src/org/jboss/jms/example/RequestReplyExample.java	2009-04-01 11:13:04 UTC (rev 6258)
@@ -0,0 +1,135 @@
+/*
+   * JBoss, Home of Professional Open Source
+   * Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
+   * by the @authors tag. See the copyright.txt in the distribution for a
+   * full listing of individual contributors.
+   *
+   * This is free software; you can redistribute it and/or modify it
+   * under the terms of the GNU Lesser General Public License as
+   * published by the Free Software Foundation; either version 2.1 of
+   * the License, or (at your option) any later version.
+   *
+   * This software is distributed in the hope that it will be useful,
+   * but WITHOUT ANY WARRANTY; without even the implied warranty of
+   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+   * Lesser General Public License for more details.
+   *
+   * You should have received a copy of the GNU Lesser General Public
+   * License along with this software; if not, write to the Free
+   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+   */
+package org.jboss.jms.example;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.Destination;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TemporaryQueue;
+import javax.jms.TextMessage;
+import javax.naming.InitialContext;
+
+/**
+ * A simple JMS example that shows how to use Request/Replay style messaging.
+ *
+ * @author <a href="hgao at redhat.com">Howard Gao</a>
+ */
+public class RequestReplyExample extends JMSExample
+{
+   public static void main(String[] args)
+   {
+      new RequestReplyExample().run(args);
+   }
+
+   public void runExample() throws Exception
+   {
+      Connection connection = null;
+      try
+      {
+         //Step 1. Create an initial context to perform the JNDI lookup.
+         InitialContext initialContext = getContext();
+
+         //Step 2. Lookup the queue for sending the request message
+         Queue requestQueue = (Queue) initialContext.lookup("/queue/exampleQueue");
+
+         //Step 3. Lookup for the Connection Factory
+         ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
+
+         //Step 4. Create a JMS Connection
+         connection = cf.createConnection();
+         
+         //Step 5. Start the connection.
+         connection.start();
+
+         //Step 6. Create a JMS Session
+         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+         //Step 7. 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
+         TemporaryQueue replyQueue = session.createTemporaryQueue();
+         
+         //Step 9. Create a request Text Message
+         TextMessage requestMsg = session.createTextMessage("A request message");
+         
+         //Step 10. Set the ReplyTo header so that the request receiver knows where to send the reply.
+         requestMsg.setJMSReplyTo(replyQueue);
+         
+         //Step 11. Set the CorrelationID so that it can be linked with corresponding reply message
+         requestMsg.setJMSCorrelationID("jbm-id: 0000001");
+         
+         //Step 12. Sent the request message
+         producer.send(requestMsg);
+         
+         System.out.println("Request message sent.");
+         
+         //Step 13. Create a JMS Message Consumer
+         MessageConsumer messageConsumer = session.createConsumer(requestQueue);
+         
+         //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();
+         
+         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());
+      }
+      finally
+      {
+         //Step 22. Be sure to close our JMS resources!
+         if(connection != null)
+         {
+            connection.close();
+         }
+      }
+   }
+
+}




More information about the jboss-cvs-commits mailing list