[jboss-cvs] JBoss Messaging SVN: r6251 - 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 02:05:36 EDT 2009


Author: gaohoward
Date: 2009-04-01 02:05:36 -0400 (Wed, 01 Apr 2009)
New Revision: 6251

Added:
   trunk/examples/jms/temp-queue/
   trunk/examples/jms/temp-queue/build.xml
   trunk/examples/jms/temp-queue/readme.html
   trunk/examples/jms/temp-queue/src/
   trunk/examples/jms/temp-queue/src/org/
   trunk/examples/jms/temp-queue/src/org/jboss/
   trunk/examples/jms/temp-queue/src/org/jboss/jms/
   trunk/examples/jms/temp-queue/src/org/jboss/jms/example/
   trunk/examples/jms/temp-queue/src/org/jboss/jms/example/TemporaryQueueExample.java
Modified:
   trunk/.classpath
Log:
added temporary queue example


Modified: trunk/.classpath
===================================================================
--- trunk/.classpath	2009-04-01 02:36:17 UTC (rev 6250)
+++ trunk/.classpath	2009-04-01 06:05:36 UTC (rev 6251)
@@ -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/temp-queue/src"/>
 	<classpathentry kind="src" path="examples/jms/topic-selector/src"/>
 	<classpathentry kind="src" path="examples/jms/transactional/src"/>
 	<classpathentry kind="src" path="examples/jms/topic/src"/>

Added: trunk/examples/jms/temp-queue/build.xml
===================================================================
--- trunk/examples/jms/temp-queue/build.xml	                        (rev 0)
+++ trunk/examples/jms/temp-queue/build.xml	2009-04-01 06:05:36 UTC (rev 6251)
@@ -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 Temporary Queue Example">
+
+   <import file="../common/build.xml"/>
+
+   <target name="run">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.TemporaryQueueExample"/>
+      </antcall>
+   </target>
+
+   <target name="runRemote">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.TemporaryQueueExample"/>
+         <param name="jbm.example.runServer" value="false"/>
+      </antcall>
+   </target>
+
+</project>

Added: trunk/examples/jms/temp-queue/readme.html
===================================================================
--- trunk/examples/jms/temp-queue/readme.html	                        (rev 0)
+++ trunk/examples/jms/temp-queue/readme.html	2009-04-01 06:05:36 UTC (rev 6251)
@@ -0,0 +1,91 @@
+<html>
+  <head>
+    <title>JBoss Messaging JMS Temporary Queue Example</title>
+    <link rel="stylesheet" type="text/css" href="../common/common.css">
+  </head>
+  <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>
+     <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 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 look-up JMS connection factory 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 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 Temporary Queue</li>
+        <pre>
+           <code>Queue tempQueue = session.createTemporaryQueue();</code>
+        </pre>
+
+        <li>We create a JMS message producer to the temporary queue. This will be used to send the messages.</li>
+        <pre>
+	        <code>MessageProducer messageProducer = session.createProducer(tempQueue);</code>
+        </pre>
+         
+        <li>We create a JMS text message to send </li>
+        <pre>
+           <code>TextMessage message = session.createTextMessage("This is a text message");</code>
+        </pre>
+   
+        <li>We send the message to the temporary queue</li>
+        <pre>
+           <code>messageProducer.send(message);</code>
+        </pre>
+
+        <li>We create a message consumer of the temporary queue</li>
+        <pre>
+           <code>MessageConsumer messageConsumer = session.createConsumer(tempQueue);</code>
+        </pre>
+
+        <li>We receive the message from the temporary queue</li>
+        <pre>
+           <code>message = (TextMessage) messageConsumer.receive(5000);</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>
+           <code>
+
+      finally
+      {
+         if (connection != null)
+         {
+            // Be sure to close our JMS resources!
+            connection.close();
+         }
+      }
+           </code>
+        </pre>
+
+
+         
+     </ol>
+  </body>
+</html>
\ No newline at end of file

Added: trunk/examples/jms/temp-queue/src/org/jboss/jms/example/TemporaryQueueExample.java
===================================================================
--- trunk/examples/jms/temp-queue/src/org/jboss/jms/example/TemporaryQueueExample.java	                        (rev 0)
+++ trunk/examples/jms/temp-queue/src/org/jboss/jms/example/TemporaryQueueExample.java	2009-04-01 06:05:36 UTC (rev 6251)
@@ -0,0 +1,114 @@
+/*
+   * 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.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.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.
+ *
+ * @author <a href="hgao at redhat.com">Howard Gao</a>
+ */
+public class TemporaryQueueExample extends JMSExample
+{
+   public static void main(String[] args)
+   {
+      new TemporaryQueueExample().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. Look-up the JMS connection factory
+         ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
+         
+         // Step 3. Create a JMS Connection
+         connection = cf.createConnection();
+
+         // Step 4. Start the connection
+         connection.start();
+
+         // Step 5. Create a JMS session with AUTO_ACKNOWLEDGE mode
+         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         
+         // Step 6. Create a Temporary Queue
+         Queue tempQueue = session.createTemporaryQueue();
+         
+         System.out.println("Temporary queue is created: " + tempQueue);
+         
+         // Step 7. Create a JMS message producer
+         MessageProducer messageProducer = session.createProducer(tempQueue);
+
+         // Step 8. Create a text message
+         TextMessage message = session.createTextMessage("This is a text message");
+
+         // Step 9. Send the text message to the queue
+         messageProducer.send(message);
+
+         System.out.println("Sent message: " + message.getText());
+         
+         // Step 11. Create a message consumer
+         MessageConsumer messageConsumer = session.createConsumer(tempQueue);
+
+         // Step 12. Receive the message from the queue
+         message = (TextMessage) messageConsumer.receive(5000);
+
+         System.out.println("Received message: " + message.getText());
+      }
+      finally
+      {
+         if(connection != null)
+         {
+            try
+            {
+               // Step 13. Be sure to close our JMS resources!
+               connection.close();
+            }
+            catch (JMSException e)
+            {
+               e.printStackTrace();
+            }
+         }
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list