[hornetq-commits] JBoss hornetq SVN: r10172 - in branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing: server0 and 3 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Wed Feb 2 03:35:03 EST 2011


Author: ataylor
Date: 2011-02-02 03:35:03 -0500 (Wed, 02 Feb 2011)
New Revision: 10172

Modified:
   branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/readme.html
   branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/server0/hornetq-configuration.xml
   branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/server1/hornetq-configuration.xml
   branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/server2/hornetq-configuration.xml
   branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/src/org/hornetq/jms/example/ClientSideLoadBalancingExample.java
Log:
https://issues.jboss.org/browse/JBPAPP-5782 - updated client side load balancing example

Modified: branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/readme.html
===================================================================
--- branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/readme.html	2011-02-02 05:25:08 UTC (rev 10171)
+++ branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/readme.html	2011-02-02 08:35:03 UTC (rev 10172)
@@ -8,7 +8,7 @@
   <body onload="prettyPrint()">
      <h1>JMS Client-Side Load-Balancing Example</h1>
 
-     <p>This example demonstrates how sessions created from a single JMS Connection can be created
+     <p>This example demonstrates how connnections created from a single JMS Connection factory can be created
      to different nodes of the cluster. In other words it demonstrates how HornetQ does <b>client side load balancing</b> of
      connections across the cluster.</p>
      <p>The particular load-balancing policy can be chosen to be random, round-robin or user-defined. Please see the user
@@ -44,20 +44,24 @@
            <code>ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");</code>
         </pre>
 
-        <li>We create a single connection</li>
+        <li> We create 3 JMS connections from the same connection factory. Since we are using round-robin
+         load-balancing this should result in each sessions being connected to a different node of the cluster</li>
         <pre class="prettyprint">
            <code>
-        connection = connectionFactory.createConnection();
+        Connection conn = connectionFactory.createConnection();
+        connectionA = connectionFactory.createConnection();
+        connectionB = connectionFactory.createConnection();
+        connectionC = connectionFactory.createConnection();
+        conn.close();
            </code>
         </pre>
 
-        <li>We create 3 JMS Sessions from the same connection. Since we are using round-robin 
-           load-balancing this should result in each sessions being connected to a different node of the cluster</li>
+        <li>We create JMS Sessions</li>
         <pre class="prettyprint">
            <code>
-        Session sessionA = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-        Session sessionB = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-        Session sessionC = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         Session sessionB = connectionB.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         Session sessionC = connectionC.createSession(false, Session.AUTO_ACKNOWLEDGE);
            </code>
         </pre>
 
@@ -95,7 +99,9 @@
         <li>We start the connection to consume messages</li>
         <pre class="prettyprint">
            <code>
-       connection.start();
+       connectionA.start();
+       connectionB.start();
+       connectionC.start();
            </code>
         </pre>
 
@@ -117,15 +123,23 @@
            <code>
 	finally
 	{
-	   if (connection0 != null)
-	   {
-	      connection0.close();
-	   }
-	      
-	   if (connection1 != null)
-	   {
-	      connection1.close();
-	   }
+	     if (connectionA != null)
+         {
+            connectionA.close();
+         }
+         if (connectionB != null)
+         {
+            connectionB.close();
+         }
+         if (connectionC != null)
+         {
+            connectionC.close();
+         }
+
+         if (initialContext != null)
+         {
+            initialContext.close();
+         }
 	}
            </code>
         </pre>

Modified: branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/server0/hornetq-configuration.xml
===================================================================
--- branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/server0/hornetq-configuration.xml	2011-02-02 05:25:08 UTC (rev 10171)
+++ branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/server0/hornetq-configuration.xml	2011-02-02 08:35:03 UTC (rev 10172)
@@ -38,6 +38,15 @@
          <refresh-timeout>10000</refresh-timeout>
       </discovery-group>
    </discovery-groups>
+
+   <cluster-connections>
+      <cluster-connection name="my-cluster">
+	  <address>jms</address>
+          <connector-ref>netty-connector</connector-ref>
+          <max-hops>0</max-hops>
+	  <discovery-group-ref discovery-group-name="my-discovery-group"/>
+      </cluster-connection>
+   </cluster-connections>
    
    <!-- Other config -->
 

Modified: branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/server1/hornetq-configuration.xml
===================================================================
--- branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/server1/hornetq-configuration.xml	2011-02-02 05:25:08 UTC (rev 10171)
+++ branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/server1/hornetq-configuration.xml	2011-02-02 08:35:03 UTC (rev 10172)
@@ -5,7 +5,6 @@
    <clustered>true</clustered>
 
    <!-- Connectors -->
-
    <connectors>
       <connector name="netty-connector">
          <factory-class>org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</factory-class>
@@ -22,7 +21,6 @@
    </acceptors>
    
    <!-- Clustering configuration -->
-
    <broadcast-groups>
       <broadcast-group name="my-broadcast-group">
          <group-address>231.7.7.7</group-address>
@@ -39,7 +37,16 @@
          <refresh-timeout>10000</refresh-timeout>
       </discovery-group>
    </discovery-groups>
-   
+
+   <cluster-connections>
+      <cluster-connection name="my-cluster">
+	  <address>jms</address>
+          <connector-ref>netty-connector</connector-ref>
+          <max-hops>0</max-hops>
+	  <discovery-group-ref discovery-group-name="my-discovery-group"/>
+      </cluster-connection>
+   </cluster-connections>
+      
    <!-- Other config -->
 
    <security-settings>
@@ -54,4 +61,4 @@
       </security-setting>
    </security-settings>
    
-</configuration>
\ No newline at end of file
+</configuration>

Modified: branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/server2/hornetq-configuration.xml
===================================================================
--- branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/server2/hornetq-configuration.xml	2011-02-02 05:25:08 UTC (rev 10171)
+++ branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/server2/hornetq-configuration.xml	2011-02-02 08:35:03 UTC (rev 10172)
@@ -38,6 +38,15 @@
       </discovery-group>
    </discovery-groups>
 
+   <cluster-connections>
+      <cluster-connection name="my-cluster">
+	  <address>jms</address>
+          <connector-ref>netty-connector</connector-ref>
+          <max-hops>0</max-hops>
+	  <discovery-group-ref discovery-group-name="my-discovery-group"/>
+      </cluster-connection>
+   </cluster-connections>
+   
    <!-- Other config -->
 
    <security-settings>

Modified: branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/src/org/hornetq/jms/example/ClientSideLoadBalancingExample.java
===================================================================
--- branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/src/org/hornetq/jms/example/ClientSideLoadBalancingExample.java	2011-02-02 05:25:08 UTC (rev 10171)
+++ branches/Branch_2_2_EAP/examples/jms/client-side-load-balancing/src/org/hornetq/jms/example/ClientSideLoadBalancingExample.java	2011-02-02 08:35:03 UTC (rev 10172)
@@ -45,7 +45,9 @@
    {
       InitialContext initialContext = null;
 
-      Connection connection = null;
+      Connection connectionA = null;
+      Connection connectionB = null;
+      Connection connectionC = null;
 
       try
       {
@@ -61,15 +63,23 @@
          // Wait a little while to make sure broadcasts from all nodes have reached the client
          Thread.sleep(5000);
 
-         // Step 4. We create a single connection
-         connection = connectionFactory.createConnection();
-         
-         // Step 5. We create 3 JMS Sessions from the same connection. Since we are using round-robin 
+         // Step 4. We create 3 JMS connections from the same connection factory. Since we are using round-robin
          // load-balancing this should result in each sessions being connected to a different node of the cluster
-         Session sessionA = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-         Session sessionB = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-         Session sessionC = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         Connection conn = connectionFactory.createConnection();         	 
+         connectionA = connectionFactory.createConnection();
+         connectionB = connectionFactory.createConnection();
+         connectionC = connectionFactory.createConnection();
+         conn.close();         
 
+         // Step 5. We create JMS Sessions
+         Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         Session sessionB = connectionB.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         Session sessionC = connectionC.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+         System.out.println("Session A - " + ((org.hornetq.core.client.impl.DelegatingSession) ((org.hornetq.jms.client.HornetQSession) sessionA).getCoreSession()).getConnection().getRemoteAddress() );
+         System.out.println("Session B - " + ((org.hornetq.core.client.impl.DelegatingSession) ((org.hornetq.jms.client.HornetQSession) sessionB).getCoreSession()).getConnection().getRemoteAddress() );
+         System.out.println("Session C - " + ((org.hornetq.core.client.impl.DelegatingSession) ((org.hornetq.jms.client.HornetQSession) sessionC).getCoreSession()).getConnection().getRemoteAddress() );
+
          // Step 6. We create JMS MessageProducer objects on the sessions
          MessageProducer producerA = sessionA.createProducer(queue);
          MessageProducer producerB = sessionB.createProducer(queue);
@@ -94,7 +104,9 @@
          }
          
          // Step 8. We start the connection to consume messages
-         connection.start();
+         connectionA.start();
+         connectionB.start();
+         connectionC.start();
          
          // Step 9. We consume messages from the 3 session, one at a time.
          // We try to consume one more message than expected from each session. If
@@ -110,10 +122,18 @@
       {
          // Step 10. Be sure to close our resources!
 
-         if (connection != null)
+         if (connectionA != null)
          {
-            connection.close();
+            connectionA.close();
          }
+         if (connectionB != null)
+         {
+            connectionB.close();
+         }
+         if (connectionC != null)
+         {
+            connectionC.close();
+         }
 
          if (initialContext != null)
          {



More information about the hornetq-commits mailing list