[jboss-cvs] JBoss Messaging SVN: r6260 - 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 10:21:23 EDT 2009


Author: jmesnil
Date: 2009-04-01 10:21:23 -0400 (Wed, 01 Apr 2009)
New Revision: 6260

Added:
   trunk/examples/jms/browser/
   trunk/examples/jms/browser/build.xml
   trunk/examples/jms/browser/readme.html
   trunk/examples/jms/browser/src/
   trunk/examples/jms/browser/src/org/
   trunk/examples/jms/browser/src/org/jboss/
   trunk/examples/jms/browser/src/org/jboss/jms/
   trunk/examples/jms/browser/src/org/jboss/jms/example/
   trunk/examples/jms/browser/src/org/jboss/jms/example/QueueBrowserExample.java
Modified:
   trunk/.classpath
Log:
JMS QueueBrowser example

+ reordered eclipse's classpath to have example source folders appear after src and tests

Modified: trunk/.classpath
===================================================================
--- trunk/.classpath	2009-04-01 13:24:39 UTC (rev 6259)
+++ trunk/.classpath	2009-04-01 14:21:23 UTC (rev 6260)
@@ -1,15 +1,6 @@
 <?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"/>
-	<classpathentry kind="src" path="examples/jms/topic/src"/>
-	<classpathentry kind="src" path="examples/jms/queue/src"/>
-	<classpathentry kind="src" path="examples/jms/queue-requestor/src"/>
-	<classpathentry kind="src" path="examples/jms/durable/src"/>
-	<classpathentry kind="src" path="examples/jms/common/src"/>
 	<classpathentry kind="src" path="build/src"/>
 	<classpathentry kind="src" path="tests/config"/>
 	<classpathentry kind="src" path="src/config"/>
@@ -22,6 +13,16 @@
 	<classpathentry kind="src" path="src/schemas"/>
 	<classpathentry kind="src" path="tests/joram-tests/src"/>
 	<classpathentry kind="src" path="tests/joram-tests/config"/>
+    <classpathentry kind="src" path="examples/jms/browser/src"/>
+	<classpathentry kind="src" path="examples/jms/common/src"/>
+	<classpathentry kind="src" path="examples/jms/durable/src"/>
+	<classpathentry kind="src" path="examples/jms/queue/src"/>
+	<classpathentry kind="src" path="examples/jms/queue-requestor/src"/>
+	<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/src"/>
+	<classpathentry kind="src" path="examples/jms/topic-selector/src"/>
+	<classpathentry kind="src" path="examples/jms/transactional/src"/>
 	<classpathentry kind="lib" path="thirdparty/oswego-concurrent/lib/concurrent.jar"/>
 	<classpathentry kind="lib" path="thirdparty/apache-log4j/lib/log4j.jar"/>
 	<classpathentry kind="lib" path="thirdparty/junit/lib/junit.jar"/>


Property changes on: trunk/examples/jms/browser
___________________________________________________________________
Name: svn:ignore
   + build


Added: trunk/examples/jms/browser/build.xml
===================================================================
--- trunk/examples/jms/browser/build.xml	                        (rev 0)
+++ trunk/examples/jms/browser/build.xml	2009-04-01 14:21:23 UTC (rev 6260)
@@ -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 QueueBrowser Example">
+
+   <import file="../common/build.xml"/>
+
+   <target name="run">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.QueueBrowserExample"/>
+      </antcall>
+   </target>
+
+   <target name="runRemote">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.QueueBrowserExample"/>
+         <param name="jbm.example.runServer" value="false"/>
+      </antcall>
+   </target>
+
+</project>
\ No newline at end of file

Added: trunk/examples/jms/browser/readme.html
===================================================================
--- trunk/examples/jms/browser/readme.html	                        (rev 0)
+++ trunk/examples/jms/browser/readme.html	2009-04-01 14:21:23 UTC (rev 6260)
@@ -0,0 +1,122 @@
+<html>
+  <head>
+    <title>JBoss Messaging JMS QueueBrowser Example</title>
+    <link rel="stylesheet" type="text/css" href="../common/common.css">
+  </head>
+  <body>
+     <h1>JMS Queue Example</h1>
+     <br>
+     <p>This example shows you how to use a JMS <a href="http://java.sun.com/javaee/5/docs/api/javax/jms/QueueBrowser.html">QueueBrowser</a> with JBoss Messaging.<br />
+     Queues are a standard part of JMS, please consult the JMS 1.1 specification for full details.<br />
+     A QueueBrowser is used to look at messages on the queue without removing them. 
+     It can scan the entire content of a queue or only messages matching a message selector.</p>
+     <p>
+         The example will send 2 messages on a queue, use a QueueBrowser to browse 
+         the queue (looking at the message without removing them) and finally consume the 2 messages
+     </p>
+     <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 look up the JMS queue object from JNDI</li>
+        <pre>
+           <code>Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");</code>
+        </pre>
+
+        <li>We look up the JMS connection factory object 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 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 JMS message producer on the session. This will be used to send the messages.</li>
+        <pre>
+          <code>MessageProducer messageProducer = session.createProducer(topic);</code>
+       </pre>
+
+        <li>We create 2 JMS text messages that we are going to send.</li>
+        <pre>
+           <code> TextMessage message_1 = session.createTextMessage("this is the 1st message");
+            TextMessage message_2 = session.createTextMessage("this is the 2nd message");</code>
+        </pre>
+
+        <li>We send messages to the queue</li>
+        <pre>
+           <code>messageProducer.send(message_1);
+           messageProducer.send(message_2);</code>
+        </pre>
+        
+        <li>We create a JMS QueueBrowser.<br />
+            We have not specified a message selector so the browser will enumerate the entire content of the queue.
+            </li>
+        <pre>
+           <code>QueueBrowser browser = session.createBrowser(queue);</code>
+        </pre>
+            
+        <li>We browse the queue and display all the messages' text
+        </li>
+        <pre>
+            <code>Enumeration messageEnum = browser.getEnumeration();
+            while (messageEnum.hasMoreElements())
+            {
+               TextMessage message = (TextMessage)messageEnum.nextElement();
+               System.out.println("Browsing: " + message.getText());
+            }</code>
+        </pre>
+        
+        <li>We close the browser once we have finished to use it</li>
+        <pre>
+            <code>browser.close();</code>
+        </pre>
+
+        <p>The messages were browsed but they were not removed from the queue. We will now consume them.</p>
+        
+        <li>We create a JMS Message Consumer to receive the messages.</li>
+          <pre>
+           <code>MessageConsumer messageConsumer = session.createConsumer(queue);</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>The 2 messages arrive at the consumer</li>
+        <pre>
+           <code>TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
+           System.out.println("Received message: " + messageReceived.getText());
+           messageReceived = (TextMessage)messageConsumer.receive(5000);
+           System.out.println("Received message: " + messageReceived.getText());</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)
+              {
+                 connection.close();
+              }
+           }</code>
+        </pre>
+
+
+
+     </ol>
+  </body>
+</html>
\ No newline at end of file

Added: trunk/examples/jms/browser/src/org/jboss/jms/example/QueueBrowserExample.java
===================================================================
--- trunk/examples/jms/browser/src/org/jboss/jms/example/QueueBrowserExample.java	                        (rev 0)
+++ trunk/examples/jms/browser/src/org/jboss/jms/example/QueueBrowserExample.java	2009-04-01 14:21:23 UTC (rev 6260)
@@ -0,0 +1,120 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005-2009, 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 java.util.Enumeration;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.QueueBrowser;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.naming.InitialContext;
+
+/**
+ * A simple example which shows how to use a QueueBrowser to look at messages of a queue without removing them from the queue
+ *
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ *
+ *
+ */
+public class QueueBrowserExample extends JMSExample
+{
+   public static void main(String[] args)
+   {
+      new QueueBrowserExample().run(args);
+   }
+
+   @Override
+   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. Perfom a lookup on the queue
+         Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
+
+         // Step 3. Perform a lookup on the Connection Factory
+         ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
+
+         // Step 4.Create a JMS Connection
+         connection = cf.createConnection();
+
+         // Step 5. Create a JMS Session
+         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+         // Step 6. Create a JMS Message Producer
+         MessageProducer producer = session.createProducer(queue);
+
+         // Step 7. Create 2 Text Messages
+         TextMessage message_1 = session.createTextMessage("this is the 1st message");
+         TextMessage message_2 = session.createTextMessage("this is the 2nd message");
+
+         // Step 8. Send the Message
+         producer.send(message_1);
+         producer.send(message_2);
+
+         // Step 9. Create the JMS QueueBrowser
+         QueueBrowser browser = session.createBrowser(queue);
+         
+         // Step 10. Browse the messages on the queue
+         // Browsing a queue does not remove the messages from the queue
+         Enumeration messageEnum = browser.getEnumeration();
+         while (messageEnum.hasMoreElements())
+         {
+            TextMessage message = (TextMessage)messageEnum.nextElement();
+            System.out.println("Browsing: " + message.getText());
+         }
+
+         // Step 11. Close the browser
+         browser.close();
+         
+         // Step 12. Create a JMS Message Consumer
+         MessageConsumer messageConsumer = session.createConsumer(queue);
+
+         // Step 13. Start the Connection
+         connection.start();
+
+         // Step 14. Receive the 2 messages
+         TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
+         System.out.println("Received message: " + messageReceived.getText());
+         messageReceived = (TextMessage)messageConsumer.receive(5000);
+         System.out.println("Received message: " + messageReceived.getText());
+         
+      }
+      finally
+      {
+         // Step 15. Be sure to close our JMS resources!
+         if (connection != null)
+         {
+            connection.close();
+         }
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list