[jboss-cvs] JBoss Messaging SVN: r6266 - in trunk/examples/jms: paging and 5 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 1 18:51:47 EDT 2009


Author: clebert.suconic at jboss.com
Date: 2009-04-01 18:51:47 -0400 (Wed, 01 Apr 2009)
New Revision: 6266

Added:
   trunk/examples/jms/paging/
   trunk/examples/jms/paging/build.xml
   trunk/examples/jms/paging/readme.html
   trunk/examples/jms/paging/src/
   trunk/examples/jms/paging/src/org/
   trunk/examples/jms/paging/src/org/jboss/
   trunk/examples/jms/paging/src/org/jboss/jms/
   trunk/examples/jms/paging/src/org/jboss/jms/example/
   trunk/examples/jms/paging/src/org/jboss/jms/example/PagingExample.java
Log:
Adding paging example


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


Added: trunk/examples/jms/paging/build.xml
===================================================================
--- trunk/examples/jms/paging/build.xml	                        (rev 0)
+++ trunk/examples/jms/paging/build.xml	2009-04-01 22:51:47 UTC (rev 6266)
@@ -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 Queue Example">
+
+   <import file="../common/build.xml"/>
+
+   <target name="run">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.PagingExample"/>
+      </antcall>
+   </target>
+
+   <target name="runRemote">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.PagingExample"/>
+         <param name="jbm.example.runServer" value="false"/>
+      </antcall>
+   </target>
+
+</project>

Added: trunk/examples/jms/paging/readme.html
===================================================================
--- trunk/examples/jms/paging/readme.html	                        (rev 0)
+++ trunk/examples/jms/paging/readme.html	2009-04-01 22:51:47 UTC (rev 6266)
@@ -0,0 +1,114 @@
+<html>
+  <head>
+    <title>JBoss Messaging Paging Example</title>
+    <link rel="stylesheet" type="text/css" href="../common/common.css">
+  </head>
+  <body>
+     <h1>Paging Example</h1>
+     <br>
+     <p>This example shows how JBoss Messaging would avoid running out of resources by paging messages.</p>
+     <p>A maxSize could be specified per Destination on the destinations settings, or a globalMaxSize could also be set on the maing config file.</a>
+     <p>When the buffered messages are consuming too much memory, JBossMessaging starts writing messages on the file-system, and as the memory is released by message acknowledgement or transaction commits those messages are recovered from disk and placed in memory</p>
+     <p>Acknowledgement plays an important factor on paging as messages will stay on the file system until the memory is released</p> 
+       
+     <p>A Queue is used to send messages point to point, from a producer to a consumer. The queue guarantees message ordering between these 2 points.</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 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 don't need persistent messages in order to use paging. (This step is optional)</li>
+        <pre><code>
+        messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+        </code></pre>
+
+        <li>Create a Binary Bytes Message with 10K arbitrary bytes</li>
+        <pre><code>
+         BytesMessage message = session.createBytesMessage();
+         message.writeBytes(new byte[10 * 1024]);</code>
+       </pre>
+       
+       <li>Send the same message over 30K times, which should be over the max limit configured and imposed by the server. Probably also over to what would fit on the server's memory</li>
+       
+       <pre><code>
+         for (int i = 0; i < 30000; i++)
+         {
+            messageProducer.send(message);
+         }</code>
+       </pre>
+       
+       <li>Create a JMS Consumer</p>
+       <pre><code>
+       MessageConsumer messageConsumer = session.createConsumer(queue);
+       </code></pre>
+       
+       <li> Start the JMS Connection. This step will activate the subscribers to receive messages.</li>
+       <pre><code>
+       connection.start();
+       </code></pre>
+
+
+        <li>Receive the messages. It's important to ACK for messages as JBM will not read messages from the paging file system until messages are ACKed or that would lead the server to be OutOfMemory</li>
+        <pre><code>
+         for (int i = 0; i < 30000; i++)
+         {
+            message = (BytesMessage)messageConsumer.receive(1000);
+
+            if (i % 1000 == 0)
+            {
+               System.out.println("Received " + i + " messages");
+               
+               message.acknowledge();
+            }
+         }
+        </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 12. Be sure to close our JMS resources!
+            connection.close();
+         }
+      }
+           </code>
+        </pre>
+
+     </ol>
+  </body>
+</html>
\ No newline at end of file

Added: trunk/examples/jms/paging/src/org/jboss/jms/example/PagingExample.java
===================================================================
--- trunk/examples/jms/paging/src/org/jboss/jms/example/PagingExample.java	                        (rev 0)
+++ trunk/examples/jms/paging/src/org/jboss/jms/example/PagingExample.java	2009-04-01 22:51:47 UTC (rev 6266)
@@ -0,0 +1,120 @@
+/*
+   * 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.BytesMessage;
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.DeliveryMode;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.naming.InitialContext;
+
+/**
+ * A simple JMS Queue example that creates a producer and consumer on a queue and sends then receives a message.
+ *
+ * @author <a href="ataylor at redhat.com">Andy Taylor</a>
+ */
+public class PagingExample extends JMSExample
+{
+   public static void main(String[] args)
+   {
+      new PagingExample().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. 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.CLIENT_ACKNOWLEDGE);
+
+         //Step 6. Create a JMS Message Producer
+         MessageProducer messageProducer = session.createProducer(queue);
+         
+         //Step 7. We don't need persistent messages in order to use paging. (This step is optional)
+         messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+
+         //Step 8. Create a Binary Bytes Message with 10K arbitrary bytes
+         BytesMessage message = session.createBytesMessage();
+         message.writeBytes(new byte[10 * 1024]);
+         
+         //Step 9. Send the message for about 30K, which should be over the memory limit imposed by the server
+         for (int i = 0; i < 30000; i++)
+         {
+            messageProducer.send(message);
+         }
+         
+         
+         //Step 10. Create a JMS Message Consumer
+         MessageConsumer messageConsumer = session.createConsumer(queue);
+         
+
+         //Step 11. Start the Connection
+         connection.start();
+         
+         
+         //Step 12. Receive the messages. 
+         //         It's important to ACK for messages as JBM will not read messages from paging until messages are ACKed or that would lead the server to be OutOfMemory
+         
+         for (int i = 0; i < 30000; i++)
+         {
+            message = (BytesMessage)messageConsumer.receive(1000);
+
+            if (i % 1000 == 0)
+            {
+               System.out.println("Received " + i + " messages");
+               
+               message.acknowledge();
+            }
+         }
+         
+         System.out.println("Received 30000 messages");
+
+         initialContext.close();
+      }
+      finally
+      {
+         //Step 12. Be sure to close our JMS resources!
+         if(connection != null)
+         {
+            connection.close();
+         }
+      }
+   }
+
+}




More information about the jboss-cvs-commits mailing list