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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Apr 2 05:53:20 EDT 2009


Author: jmesnil
Date: 2009-04-02 05:53:20 -0400 (Thu, 02 Apr 2009)
New Revision: 6270

Added:
   trunk/examples/jms/expiry/
   trunk/examples/jms/expiry/build.xml
   trunk/examples/jms/expiry/readme.html
   trunk/examples/jms/expiry/src/
   trunk/examples/jms/expiry/src/org/
   trunk/examples/jms/expiry/src/org/jboss/
   trunk/examples/jms/expiry/src/org/jboss/jms/
   trunk/examples/jms/expiry/src/org/jboss/jms/example/
   trunk/examples/jms/expiry/src/org/jboss/jms/example/ExpiryExample.java
Modified:
   trunk/.classpath
   trunk/examples/jms/common/config/jbm-jms.xml
   trunk/examples/jms/common/config/jbm-queues.xml
Log:
JMS Expiration example

Modified: trunk/.classpath
===================================================================
--- trunk/.classpath	2009-04-02 07:48:17 UTC (rev 6269)
+++ trunk/.classpath	2009-04-02 09:53:20 UTC (rev 6270)
@@ -13,9 +13,10 @@
 	<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/browser/src"/>
 	<classpathentry kind="src" path="examples/jms/common/src"/>
 	<classpathentry kind="src" path="examples/jms/durable/src"/>
+	<classpathentry kind="src" path="examples/jms/expiry/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"/>

Modified: trunk/examples/jms/common/config/jbm-jms.xml
===================================================================
--- trunk/examples/jms/common/config/jbm-jms.xml	2009-04-02 07:48:17 UTC (rev 6269)
+++ trunk/examples/jms/common/config/jbm-jms.xml	2009-04-02 09:53:20 UTC (rev 6270)
@@ -14,6 +14,10 @@
       <entry name="/queue/exampleQueue"/>
    </queue>
 
+   <queue name="expiryQueue">
+      <entry name="/queue/expiryQueue"/>
+   </queue>
+
    <topic name="topic">
       <entry name="/topic/exampleTopic"/>
    </topic>

Modified: trunk/examples/jms/common/config/jbm-queues.xml
===================================================================
--- trunk/examples/jms/common/config/jbm-queues.xml	2009-04-02 07:48:17 UTC (rev 6269)
+++ trunk/examples/jms/common/config/jbm-queues.xml	2009-04-02 09:53:20 UTC (rev 6270)
@@ -50,12 +50,11 @@
       <permission type="send" roles="guest,def"/>
    </security>
 
-
    <!--default for catch all-->
    <address-settings match="#">
       <clustered>false</clustered>
       <dead-letter-address>queuejms.DLQ</dead-letter-address>
-      <expiry-address>queuejms.ExpiryQueue</expiry-address>
+      <expiry-address>queuejms.expiryQueue</expiry-address>
       <redelivery-delay>0</redelivery-delay>
       <max-size-bytes>-1</max-size-bytes>
       <page-size-bytes>10485760</page-size-bytes>

Added: trunk/examples/jms/expiry/build.xml
===================================================================
--- trunk/examples/jms/expiry/build.xml	                        (rev 0)
+++ trunk/examples/jms/expiry/build.xml	2009-04-02 09:53:20 UTC (rev 6270)
@@ -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 Expiry Example">
+
+   <import file="../common/build.xml"/>
+
+   <target name="run">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.ExpiryExample"/>
+      </antcall>
+   </target>
+
+   <target name="runRemote">
+      <antcall target="runExample">
+         <param name="example.classname" value="org.jboss.jms.example.ExpiryExample"/>
+         <param name="jbm.example.runServer" value="false"/>
+      </antcall>
+   </target>
+
+</project>
\ No newline at end of file

Added: trunk/examples/jms/expiry/readme.html
===================================================================
--- trunk/examples/jms/expiry/readme.html	                        (rev 0)
+++ trunk/examples/jms/expiry/readme.html	2009-04-02 09:53:20 UTC (rev 6270)
@@ -0,0 +1,147 @@
+<html>
+  <head>
+    <title>JBoss Messaging JMS Expiration Example</title>
+    <link rel="stylesheet" type="text/css" href="../common/common.css">
+  </head>
+  <body>
+     <h1>JMS Expiration Example</h1>
+
+     <p>This example shows you how to define and deal with message expiration.</p>
+     <p>Messages can be retained in the messaging system for a limited period of time before being removed.
+         JMS specification states that clients should not receive messages that have been expired (but it does not guarantee this will not happen).</p>
+     <p>JBoss Messaging can assign a <em>expiry queue</em> to a given queue so that when messages are expired, they are removed from the queue and put
+         in the expiry queue. These "expired" messages can later be consumed from the expiry queue for further inspection.
+     <p>
+         The example will send 1 message with a short <em>time-to-live</em> to a queue. We will wait for the message to expire and checks that the message
+         is no longer in the queue it was sent to.
+         We will instead consume it from the <em>expiry queue</em> where it was moved when it expired.
+     </p>
+     <h2>Example setup</h2>
+     <p>Expiry queues are defined as in the queue settings configuration file <a href="../common/config/jbm-queues.xml">jbm-queues.xml</a>:</p>
+     <pre>
+         <code>&lt;!--default for all queues--&gt;
+             &lt;address-settings match="#"&gt;
+                ...
+                &lt;expiry-address&gt;queuejms.expiryQueue&lt;/expiry-address&gt;
+             &lt;/address-settings&gt;</code>
+     </pre>          
+     <p>This configuration will moved expired messages from any given queue (the wildcard <code>#</code>) to the <code>expiryQueue</code></p>
+     <p>Since we want to consume messages from this expiryQueue, we also need to add a JNDI binding to perform a lookup.
+         This is configured in <a href="../common/config/jbm-jms.xml">jbm-jms.xml</a></p>
+     <pre>
+         <code>&lt;queue name="expiryQueue"&gt;
+            &lt;entry name="/queue/expiryQueue"/&gt;
+         &lt;/queue&gt;</code>
+     </pre>
+     </p>
+     <h2>Example step-by-step</h2>
+     <p><i>To run the example, simply type <code>ant</code> from this directory</i></p>
+     <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>Messages sent by this producer will be retained for 1s (1000ms) before expiration</li>
+       <pre>
+           <code>producer.setTimeToLive(1000);</code>
+       </pre>
+
+        <li>We create a text messages</li>
+        <pre>
+            <code>TextMessage message = session.createTextMessage("this is a text message");</code>
+        </pre>
+
+        <li>We send the message to the queue</li>
+        <pre>
+            <code>producer.send(message);</code>
+        </pre>
+        
+       <li>We sleep a little bit to let the message expire</li>
+        <pre>
+            <code>Thread.sleep(5000);</code>
+        </pre>
+
+        <p>We will now try to consume the message from the queue but it won't be there since it has expired</p>
+
+        <li>We create a JMS message consumer on the queue</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>We try to receive a message from the queue. Since there is none, the call will timeout after 5000ms and <code>messageReceived</code> will be <code>null</code>
+        <pre>
+           <code>TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
+           System.out.println("Received message from " + queue.getQueueName() + ": " + messageReceived);</code>
+        </pre>
+        
+        <p>However, we have configured JBoss Messaging to send any expired messages to the <code>expiryQueue</code>.
+            We will now consume messages from this expiry queue and receives the <em>expired</em> message.</p>
+            
+        <li>We look up the JMS <em>expiry queue</em> object from JNDI</li>
+        <pre>
+           <code>Queue expiryQueue = (Queue)initialContext.lookup("/queue/expiryQueue");</code>
+        </pre>
+                  
+        <li>We create a JMS message consumer on the expiry queue</li>
+        <pre>
+            <code>MessageConsumer expiryConsumer = session.createConsumer(expiryQueue);</code>
+        </pre>
+        
+        <li>We consume a message from the expiry queue:<li>
+        <pre>
+            <code>messageReceived = (TextMessage)expiryConsumer.receive(5000);</code>
+        </pre>
+        
+        <li>The message consumed from the <em>expiry queue</em> is the same message which was sent to the <em>queue</em>
+        <pre>
+            <code>System.out.println("Received message from " + expiryQueue.getQueueName() + ": " + 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/expiry/src/org/jboss/jms/example/ExpiryExample.java
===================================================================
--- trunk/examples/jms/expiry/src/org/jboss/jms/example/ExpiryExample.java	                        (rev 0)
+++ trunk/examples/jms/expiry/src/org/jboss/jms/example/ExpiryExample.java	2009-04-02 09:53:20 UTC (rev 6270)
@@ -0,0 +1,130 @@
+/*
+   * 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 java.util.HashSet;
+import java.util.Set;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.naming.InitialContext;
+
+/**
+ * An example showing how messages are moved to an expiry queue when they expire.
+ *
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ *
+ */
+public class ExpiryExample extends JMSExample
+{
+   public static void main(String[] args)
+   {
+      new ExpiryExample().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.AUTO_ACKNOWLEDGE);
+
+         // Step 6. Create a JMS Message Producer
+         MessageProducer producer = session.createProducer(queue);
+
+         // Step 7. Messages sent by this producer will be retained for 1s (1000ms) before expiration
+         producer.setTimeToLive(1000);
+
+         // Step 8. Create a Text Message
+         TextMessage message = session.createTextMessage("this is a text message");
+
+         // Step 9. Send the Message
+         producer.send(message);
+         System.out.println("Sent message to " + queue.getQueueName() + ": " + message.getText());
+
+         // Step 10. Sleep for 5s. Once we wake up, the message will have been expired
+         System.out.println("Sleep a little bit to let the message expire...");
+         Thread.sleep(5000);
+
+         // Step 11. Create a JMS Message Consumer for the queue
+         MessageConsumer messageConsumer = session.createConsumer(queue);
+
+         // Step 12. Start the Connection
+         connection.start();
+
+         // Step 13. Trying to receive a message. Since there is none on the queue, the call will timeout after 5000ms
+         // and messageReceived will be null
+         TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
+         System.out.println("Received message from " + queue.getQueueName() + ": " + messageReceived);
+
+         // Step 15. Perfom a lookup on the expiry queue
+         Queue expiryQueue = (Queue)initialContext.lookup("/queue/expiryQueue");
+
+         // Step 16. Create a JMS Message Consumer for the expiry queue
+         MessageConsumer expiryConsumer = session.createConsumer(expiryQueue);
+
+         // Step 17. Receive the message from the expiry queue
+         messageReceived = (TextMessage)expiryConsumer.receive(5000);
+
+         // Step 18. The message sent to the queue was moved to the expiry queue when it expired.
+         System.out.println("Received message from " + expiryQueue.getQueueName() + ": " + messageReceived.getText());
+
+         initialContext.close();
+      }
+      finally
+      {
+         // Step 19. Be sure to close our JMS resources!
+         if (connection != null)
+         {
+            connection.close();
+         }
+      }
+   }
+
+   @Override
+   public Set<String> getQueues()
+   {
+      HashSet<String> queues = new HashSet<String>();
+      queues.add("exampleQueue");
+      queues.add("expiryQueue");
+      return queues;
+   }
+
+}




More information about the jboss-cvs-commits mailing list