[jboss-cvs] JBoss Messaging SVN: r6438 - in trunk/examples/jms/bridge: server0 and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 15 14:00:22 EDT 2009


Author: timfox
Date: 2009-04-15 14:00:22 -0400 (Wed, 15 Apr 2009)
New Revision: 6438

Added:
   trunk/examples/jms/bridge/src/org/jboss/jms/example/HatColourChangeTransformer.java
Removed:
   trunk/examples/jms/bridge/src/org/jboss/jms/example/PokeEyeTransformer.java
Modified:
   trunk/examples/jms/bridge/readme.html
   trunk/examples/jms/bridge/server0/jbm-configuration.xml
   trunk/examples/jms/bridge/src/org/jboss/jms/example/BridgeExample.java
Log:
finished off bridge example

Modified: trunk/examples/jms/bridge/readme.html
===================================================================
--- trunk/examples/jms/bridge/readme.html	2009-04-15 17:18:42 UTC (rev 6437)
+++ trunk/examples/jms/bridge/readme.html	2009-04-15 18:00:22 UTC (rev 6438)
@@ -7,12 +7,12 @@
      <h1>JBoss Messaging Core Bridge Example</h1>
      <br>
      
-     <p>This example demonstrates a core bridge deployed on a single server, which consumes messages from a
-     particular local queue and forwards them to an address on a second server.</p>
+     <p>This example demonstrates a core bridge deployed on one server, which consumes messages from a
+     local queue and forwards them to an address on a second server.</p>
      
      <p>Core bridges are used to create message flows between any two JBoss Messaging servers which are remotely separated.
      Core bridges are resilient and will cope with temporary connection failure allowing them to be an ideal
-     choice for forwarding over unreliable connections, e.g. WANS.</p>
+     choice for forwarding over unreliable connections, e.g. a WAN.</p>
      <p>They can also be configured with an optional filter expression, and will only forward messages that
      match that filter.</p>
      <p>Furthermore they can be configured to use an optional Transformer class. A user defined Transformer class
@@ -20,66 +20,72 @@
      the message in some way, e.g. changing it's properties or body</p>
      <p>JBoss Messaging also includes a <b>JMS Bridge</b>. This is similar in some ways to a core bridge, but uses the JMS API
      and can be used to bridge between any two JMS 1.1 compliant messaging systems. The core bridge is limited to bridging
-     between JBoss Messaging instances, but may provide better performance than the JMS bridge.</p>
+     between JBoss Messaging instances, but may provide better performance than the JMS bridge. The JMS bridge is covered in
+     a separate example.</p>
      <p>For more information on bridges, please see the JBoss Messaging user manual.</p>
-     
-     <h1>TODO not finished</h1>
-     
-     <p>This example demonstrates a JMS queue deployed on two different nodes. The two nodes are configured to form a cluster.</p>
-     <p>We then create a consumer on the queue on each node, and we create a producer on only one of the nodes.</p>
-     <p>We then send some messages via the producer, and we verify that <b>both</b> consumers receive the sent messages
-     in a round-robin fashion.</p>
-     <p>In other words, JBoss Messaging <b>load balances</b> the sent messages across all consumers on the cluster</p>
-     <p>This example uses JNDI to lookup the JMS Queue and ConnectionFactory objects. If you prefer not to use
-     JNDI, these could be instantiated directly.</p>     
-     <p>Here's the relevant snippet from the server configuration, which tells the server to form a cluster between the two nodes
-     and to load balance the messages between the nodes.</p>     
+     <br>
+     <p>In this example we will demonstrate a simple sausage factory for aardvarks.</p>
+     <p>We have a JMS queue on server 0 named <code>sausage-factory</code>, and we have a 
+     JMS queue on server 1 named <code>mincing-machine</code></p>
+     <p>We want to forward any messages that are sent to the <code>sausage-factory</code> queue on server 0, to the <code>mincing-machine</code>
+     on server 1.</p>
+     <p>We only want to make aardvark sausages, so we only forward messages where the property "name" is set
+     to "aardvark". It is known that other things, such are Sasquatches are also sent to the <code>sausage-factory</code> and we
+     want to reject those.</p>
+     <p>Moreover it is known that Aardvarks normally wear blue hats, and it's important that we only make sausages using
+     Aardvarks with green hats, so on the way we are going transform the property "hat" from "green" to "blue".</p>  
+     <p>Here's a snippet from <code>jbm-configuration.xml</code> showing the bridge configuration</p>
      <pre>
      <code>
-   &lt;cluster-connection name="my-cluster"&gt;
-      &lt;address&gt;jms&lt;/address&gt;
-	   &lt;retry-interval&gt;500&lt;/retry-interval&gt;
-	   &lt;use-duplicate-detection&gt;true&lt;/use-duplicate-detection&gt;
-	   &lt;forward-when-no-consumers&gt;true&lt;/forward-when-no-consumers&gt;
-	   &lt;max-hops&gt;1&lt;/max-hops&gt;
-	   &lt;discovery-group-ref discovery-group-name="my-discovery-group"/&gt;
-   &lt;/cluster-connection&gt;
-   </code>
-     </pre>    
-     <p>For more information on JBoss Messaging load balancing, and clustering in general, please see the clustering
-     section of the user manual.</p>      
+     &lt;bridge name="my-bridge"&gt;
+          &lt;queue-name&gt;jms.queue.sausage-factory&lt;/queue-name&gt;
+          &lt;forwarding-address&gt;jms.queue.mincing-machine&lt;/forwarding-address&gt;
+          &lt;filter string="name='aardvark'"/&gt;
+          &lt;transformer-class-name&gt;org.jboss.jms.example.HatColourChangeTransformer&lt;/transformer-class-name&gt;
+          &lt;reconnect-attempts&gt;-1&lt;/reconnect-attempts&gt;
+          &lt;connector-ref connector-name="remote-connector"/&gt;          
+     &lt;/bridge&gt;
+     </code>
+     </pre>              
      <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> Get an initial context for looking up JNDI from server 0.</li>
+        <li>We create an initial context for looking up JNDI on node 0</li>
         <pre>
            <code>
    ic0 = getContext(0);
    </code>
         </pre>
 
-        <li>Look-up the JMS Queue object from JNDI</li>
+        <li>We look up the sausage-factory queue from node 0</li>
         <pre>
-           <code>Queue queue = (Queue)ic0.lookup("/queue/exampleQueue");</code>
+           <code>Queue sausageFactory = (Queue)ic0.lookup("/queue/sausage-factory");</code>
         </pre>
 
-        <li>Look-up a JMS Connection Factory object from JNDI on server 0</li>
+        <li>We look up a JMS ConnectionFactory object from node 0</li>
         <pre>
            <code>ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");</code>
         </pre>
 
-        <li>Get an initial context for looking up JNDI from server 1.</li>
+        <li>We create an initial context for looking up JNDI on node 1</li>
         <pre>
            <code>ic1 = getContext(1);</code>
         </pre>
 
-        <li>Look-up a JMS Connection Factory object from JNDI on server 1</li>
+        <li>We look up the mincing-machine queue on node 1</li>
         <pre>
-           <code>ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
+           <code>Queue mincingMachine = (Queue)ic1.lookup("/queue/mincing-machine");
            </code>
         </pre>
 
+        <li>We look up a JMS ConnectionFactory object from node 1</li>
+        <pre>
+          <code>
+   ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
+          </code>
+        </pre>
+        
         <li>We create a JMS Connection connection0 which is a connection to server 0</li>
         <pre>
           <code>
@@ -108,83 +114,95 @@
             </code>
         </pre>
 
-        <li>We start the connections to ensure delivery occurs on them</li>
+        <li>We start the connection to ensure delivery occurs on them</li>
         <pre>
            <code>
-   connection0.start();
-
    connection1.start();
            </code>
         </pre>
 
-        <li>We create JMS MessageConsumer objects on server 0 and server 1</li>
+        <li>We create a JMS MessageConsumer object on server 1</li>
         <pre>
            <code>
-   MessageConsumer consumer0 = session0.createConsumer(queue);
-
-   MessageConsumer consumer1 = session1.createConsumer(queue);
+   MessageConsumer consumer = session1.createConsumer(mincingMachine);
            </code>
         </pre>
 
         <li>We create a JMS MessageProducer object on server 0.</li>
         <pre>
            <code>
-   MessageProducer producer = session0.createProducer(queue);</code>
+   MessageProducer producer = session0.createProducer(sausageFactory);</code>
         </pre>
 
-        <li>We send some messages to server 0.</li>
+        <li>We create and send a message representing an aardvark with a green hat to the sausage-factory
+         on node 0</li>
         <pre>
            <code>
-	final int numMessages = 10;
+         Message message = session0.createMessage();
 
-	for (int i = 0; i < numMessages; i++)
-	{
-	   TextMessage message = session0.createTextMessage("This is text message " + i);
-	      
-	   producer.send(message);
-	
-	   System.out.println("Sent message: " + message.getText());
-	}
+         message.setStringProperty("name", "aardvark");
+
+         message.setStringProperty("hat", "green");
+
+         producer.send(message);
            </code>
         </pre>
         
-        <li>We now consume those messages on *both* server 0 and server 1.
-         We note the messages have been distributed between servers in a round robin fashion.
-         JBoss Messaging has <b>load balanced</b> the messages between the available consumers on the different nodes.
-         JBoss Messaging can be configured to always load balance messages to all nodes, or to only balance messages
-         to nodes which have consumers with no or matching selectors. See the user manual for more details.</li>
-         JMS Queues implement point-to-point message where each message is only ever consumed by a
-         maximum of one consumer.
+        <li>We successfully receive the aardvark message from the mincing-machine one node 1. The aardvark's
+         hat is now blue since it has been transformed!</li>
         <pre>
            <code>
-	for (int i = 0; i < numMessages; i += 2)
-	{
-	   TextMessage message0 = (TextMessage)consumer0.receive(5000);
-	
-	   System.out.println("Got message: " + message0.getText() + " from node 0");
-	
-	   TextMessage message1 = (TextMessage)consumer1.receive(5000);
-	
-	   System.out.println("Got message: " + message1.getText() + " from node 1");
-	}
+        Message receivedMessage = consumer.receive(5000);
            </code>
-        </pre> 
+        </pre>
+        
+        <li>We create and send another message, this time representing a sasquatch with a mauve hat to the
+          sausage-factory on node 0. This won't be bridged to the mincing-machine since we only want aardvarks, not sasquatches.</li>
+        <pre>
+           <code>
+         message = session0.createMessage();
 
+         message.setStringProperty("name", "sasquatch");
+
+         message.setStringProperty("hat", "mauve");
+
+         producer.send(message);
+           </code>
+        </pre>
+        
+        <li>We don't receive the sasquatch message since it's not an aardvark!</li>
+        <pre>
+           <code>
+         receivedMessage = (TextMessage)consumer.receive(1000);
+           </code>
+        </pre>
+        
+
         <li>And finally (no pun intended), <b>always</b> remember to close your JMS resources 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 (connection0 != null)
-	   {
-	      connection0.close();
-	   }
-	      
-	   if (connection1 != null)
-	   {
-	      connection1.close();
-	   }
+	      if (connection0 != null)
+         {
+            connection0.close();
+         }
+
+         if (connection1 != null)
+         {
+            connection1.close();
+         }
+
+         if (ic0 != null)
+         {
+            ic0.close();
+         }
+
+         if (ic1 != null)
+         {
+            ic1.close();
+         }
 	}
            </code>
         </pre>

Modified: trunk/examples/jms/bridge/server0/jbm-configuration.xml
===================================================================
--- trunk/examples/jms/bridge/server0/jbm-configuration.xml	2009-04-15 17:18:42 UTC (rev 6437)
+++ trunk/examples/jms/bridge/server0/jbm-configuration.xml	2009-04-15 18:00:22 UTC (rev 6438)
@@ -33,8 +33,8 @@
 	   <bridge name="my-bridge">
           <queue-name>jms.queue.sausage-factory</queue-name>
           <forwarding-address>jms.queue.mincing-machine</forwarding-address>
-          <filter string="president='bush'"/>
-          <transformer-class-name>org.jboss.jms.example.PokeEyeTransformer</transformer-class-name>
+          <filter string="name='aardvark'"/>
+          <transformer-class-name>org.jboss.jms.example.HatColourChangeTransformer</transformer-class-name>
           <reconnect-attempts>-1</reconnect-attempts>
           <connector-ref connector-name="remote-connector"/>          
       </bridge>

Modified: trunk/examples/jms/bridge/src/org/jboss/jms/example/BridgeExample.java
===================================================================
--- trunk/examples/jms/bridge/src/org/jboss/jms/example/BridgeExample.java	2009-04-15 17:18:42 UTC (rev 6437)
+++ trunk/examples/jms/bridge/src/org/jboss/jms/example/BridgeExample.java	2009-04-15 18:00:22 UTC (rev 6438)
@@ -56,66 +56,109 @@
 
       try
       {
+         // Step 1 - we create an initial context for looking up JNDI on node 0
+
          ic0 = getContext(0);
-        
+
+         // Step 2 - we look up the sausage-factory queue from node 0
+
          Queue sausageFactory = (Queue)ic0.lookup("/queue/sausage-factory");
-         
+
+         // Step 3 - we look up a JMS ConnectionFactory object from node 0
+
          ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");
-         
-         
+
+         // Step 4 - we create an initial context for looking up JNDI on node 1
+
          ic1 = getContext(1);
-         
+
+         // Step 5 - we look up the mincing-machine queue on node 1
+
          Queue mincingMachine = (Queue)ic1.lookup("/queue/mincing-machine");
-         
+
+         // Step 6 - we look up a JMS ConnectionFactory object from node 1
+
          ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
-         
 
-         // Step 6. We create a JMS Connection connection0 which is a connection to server 0
+         // Step 7. We create a JMS Connection connection0 which is a connection to server 0
+
          connection0 = cf0.createConnection();
 
-         // Step 7. We create a JMS Connection connection1 which is a connection to server 1
+         // Step 8. We create a JMS Connection connection1 which is a connection to server 1
          connection1 = cf1.createConnection();
 
-         // Step 8. We create a JMS Session on server 0
+         // Step 9. We create a JMS Session on server 0
+
          Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
 
-         // Step 9. We create a JMS Session on server 1
+         // Step 10. We create a JMS Session on server 1
+
          Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
 
          // Step 10. We start the connection to ensure delivery occurs on them
 
          connection1.start();
 
-         // Step 11. We create JMS MessageConsumer objects 
+         // Step 11. We create JMS MessageConsumer object
          MessageConsumer consumer = session1.createConsumer(mincingMachine);
 
          // Step 12. We create a JMS MessageProducer object on server 0
          MessageProducer producer = session0.createProducer(sausageFactory);
 
+         // Step 13. We create and send a message representing an aardvark with a green hat to the sausage-factory
+         // on node 0
          Message message = session0.createMessage();
-         
-         message.setStringProperty("president", "bush");
-         
-         message.setStringProperty("eye", "not poked yet");
 
+         message.setStringProperty("name", "aardvark");
+
+         message.setStringProperty("hat", "green");
+
          producer.send(message);
 
+         System.out.println("Sent " + message.getStringProperty("name") +
+                            " message with " +
+                            message.getStringProperty("hat") +
+                            " hat to sausage-factory on node 0");
+
+         // Step 14 - we successfully receive the aardvark message from the mincing-machine one node 1. The aardvark's
+         // hat is now blue since it has been transformed!
+
          Message receivedMessage = consumer.receive(5000);
-         
-         System.out.println("Received message " + receivedMessage.getStringProperty("eye"));
-         
+
+         System.out.println("Received " + receivedMessage.getStringProperty("name") +
+                            " message with " +
+                            receivedMessage.getStringProperty("hat") +
+                            " hat from mincing-machine on node 1");
+
+         // Step 13. We create and send another message, this time representing a sasquatch with a mauve hat to the
+         // sausage-factory on node 0. This won't be bridged to the mincing-machine since we only want aardvarks, not sasquatches
+
          message = session0.createMessage();
-         
-         message.setStringProperty("president", "obama");
-         
-         message.setStringProperty("eye", "not poked yet");
 
+         message.setStringProperty("name", "sasquatch");
+
+         message.setStringProperty("hat", "mauve");        
+
          producer.send(message);
          
+         System.out.println("Sent " + message.getStringProperty("name") +
+                            " message with " +
+                            message.getStringProperty("hat") +
+                            " hat to sausage-factory on node 0");
+
+         // Step 14. We don't receive the message since it has not been bridged.
+
          receivedMessage = (TextMessage)consumer.receive(1000);
-         
-         System.out.println("Received message " + receivedMessage);
-        
+
+         if (receivedMessage == null)
+         {
+            System.out.println("Didn't receive that message from mincing-machine on node 1");
+         }
+         else
+         {
+            return false;
+         }
+
          return true;
       }
       finally

Copied: trunk/examples/jms/bridge/src/org/jboss/jms/example/HatColourChangeTransformer.java (from rev 6437, trunk/examples/jms/bridge/src/org/jboss/jms/example/PokeEyeTransformer.java)
===================================================================
--- trunk/examples/jms/bridge/src/org/jboss/jms/example/HatColourChangeTransformer.java	                        (rev 0)
+++ trunk/examples/jms/bridge/src/org/jboss/jms/example/HatColourChangeTransformer.java	2009-04-15 18:00:22 UTC (rev 6438)
@@ -0,0 +1,53 @@
+/*
+ * 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 org.jboss.messaging.core.server.ServerMessage;
+import org.jboss.messaging.core.server.cluster.Transformer;
+import org.jboss.messaging.utils.SimpleString;
+
+/**
+ * A HatColourChangeTransformer
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ *
+ */
+public class HatColourChangeTransformer implements Transformer
+{
+   public ServerMessage transform(final ServerMessage message)     
+   {
+      SimpleString propName = new SimpleString("hat");
+      
+      SimpleString oldProp = (SimpleString)message.getProperty(propName);
+      
+      System.out.println("Old hat colour is " + oldProp);
+      
+      //Change the colour
+      message.putStringProperty(propName, new SimpleString("blue"));
+      
+      return message;
+   }
+
+}

Deleted: trunk/examples/jms/bridge/src/org/jboss/jms/example/PokeEyeTransformer.java
===================================================================
--- trunk/examples/jms/bridge/src/org/jboss/jms/example/PokeEyeTransformer.java	2009-04-15 17:18:42 UTC (rev 6437)
+++ trunk/examples/jms/bridge/src/org/jboss/jms/example/PokeEyeTransformer.java	2009-04-15 18:00:22 UTC (rev 6438)
@@ -1,53 +0,0 @@
-/*
- * 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 org.jboss.messaging.core.server.ServerMessage;
-import org.jboss.messaging.core.server.cluster.Transformer;
-import org.jboss.messaging.utils.SimpleString;
-
-/**
- * A PokeEyeTransformer
- *
- * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
- *
- *
- */
-public class PokeEyeTransformer implements Transformer
-{
-   public ServerMessage transform(final ServerMessage message)     
-   {
-      SimpleString propName = new SimpleString("eye");
-      
-      SimpleString oldProp = (SimpleString)message.getProperty(propName);
-      
-      System.out.println("Old property is " + oldProp);
-      
-      //Change the property
-      message.putStringProperty(propName, new SimpleString("Poked!"));
-      
-      return message;
-   }
-
-}




More information about the jboss-cvs-commits mailing list