[jboss-cvs] JBoss Messaging SVN: r2282 - in trunk: tests/src/org/jboss/test/messaging/jms/clustering and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Feb 12 20:05:45 EST 2007
Author: clebert.suconic at jboss.com
Date: 2007-02-12 20:05:45 -0500 (Mon, 12 Feb 2007)
New Revision: 2282
Added:
trunk/src/main/org/jboss/jms/client/plugin/RandomLoadBalancingFactory.java
trunk/src/main/org/jboss/jms/client/plugin/RandomLoadBalancingPolicy.java
Modified:
trunk/tests/src/org/jboss/test/messaging/jms/clustering/LoadBalancingTest.java
Log:
hhttp://jira.jboss.org/jira/browse/JBMESSAGING-770 & http://jira.jboss.org/jira/browse/JBMESSAGING-830
Added: trunk/src/main/org/jboss/jms/client/plugin/RandomLoadBalancingFactory.java
===================================================================
--- trunk/src/main/org/jboss/jms/client/plugin/RandomLoadBalancingFactory.java (rev 0)
+++ trunk/src/main/org/jboss/jms/client/plugin/RandomLoadBalancingFactory.java 2007-02-13 01:05:45 UTC (rev 2282)
@@ -0,0 +1,59 @@
+/*
+ * 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.
+ */
+
+package org.jboss.jms.client.plugin;
+
+import org.jboss.jms.delegate.ConnectionFactoryDelegate;
+
+/**
+ * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
+ * @version <tt>$Revision$</tt>
+ * $Id$
+ */
+public class RandomLoadBalancingFactory extends LoadBalancingFactory
+{
+
+ // Constants ------------------------------------------------------------------------------------
+
+ // Attributes -----------------------------------------------------------------------------------
+
+ // Static ---------------------------------------------------------------------------------------
+
+ // Constructors ---------------------------------------------------------------------------------
+
+ // Public ---------------------------------------------------------------------------------------
+
+ // Implementation of LoadBalancingFactory -------------------------------------------------------
+ public LoadBalancingPolicy createLoadBalancingPolicy(ConnectionFactoryDelegate[] view)
+ {
+ return new RandomLoadBalancingPolicy(view);
+ }
+
+ // Package protected ----------------------------------------------------------------------------
+
+ // Protected ------------------------------------------------------------------------------------
+
+ // Private --------------------------------------------------------------------------------------
+
+ // Inner classes --------------------------------------------------------------------------------
+
+}
Property changes on: trunk/src/main/org/jboss/jms/client/plugin/RandomLoadBalancingFactory.java
___________________________________________________________________
Name: svn:keywords
+ Id LastChangedDate Author Revision
Added: trunk/src/main/org/jboss/jms/client/plugin/RandomLoadBalancingPolicy.java
===================================================================
--- trunk/src/main/org/jboss/jms/client/plugin/RandomLoadBalancingPolicy.java (rev 0)
+++ trunk/src/main/org/jboss/jms/client/plugin/RandomLoadBalancingPolicy.java 2007-02-13 01:05:45 UTC (rev 2282)
@@ -0,0 +1,87 @@
+/*
+ * 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.
+ */
+
+package org.jboss.jms.client.plugin;
+
+import org.jboss.jms.delegate.ConnectionFactoryDelegate;
+import java.util.Random;
+
+/**
+ * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
+ * @version <tt>$Revision$</tt>
+ * $Id$
+ */
+public class RandomLoadBalancingPolicy implements LoadBalancingPolicy
+{
+
+ // Constants ------------------------------------------------------------------------------------
+
+ // Attributes -----------------------------------------------------------------------------------
+
+ private ConnectionFactoryDelegate[] delegates;
+ private transient Random random = null;
+
+ // Static ---------------------------------------------------------------------------------------
+
+ // Constructors ---------------------------------------------------------------------------------
+
+ public RandomLoadBalancingPolicy(ConnectionFactoryDelegate[] delegates)
+ {
+ this.delegates = delegates;
+ }
+
+ // Public ---------------------------------------------------------------------------------------
+
+ public synchronized ConnectionFactoryDelegate getNext()
+ {
+ return delegates[randomSelect()];
+ }
+
+ public synchronized void updateView(ConnectionFactoryDelegate[] delegates)
+ {
+ this.delegates = delegates;
+ }
+
+ // Package protected ----------------------------------------------------------------------------
+
+ // Protected ------------------------------------------------------------------------------------
+
+ protected int randomSelect()
+ {
+ if (random == null)
+ {
+ random = new Random();
+ }
+ int nextInt = random.nextInt() % delegates.length;
+ if (nextInt<0)
+ {
+ nextInt *= -1;
+ }
+
+ return nextInt;
+ }
+
+ // Private --------------------------------------------------------------------------------------
+
+ // Inner classes --------------------------------------------------------------------------------
+
+}
Property changes on: trunk/src/main/org/jboss/jms/client/plugin/RandomLoadBalancingPolicy.java
___________________________________________________________________
Name: svn:keywords
+ Id LastChangedDate Author Revision
Modified: trunk/tests/src/org/jboss/test/messaging/jms/clustering/LoadBalancingTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/jms/clustering/LoadBalancingTest.java 2007-02-12 23:01:27 UTC (rev 2281)
+++ trunk/tests/src/org/jboss/test/messaging/jms/clustering/LoadBalancingTest.java 2007-02-13 01:05:45 UTC (rev 2282)
@@ -7,12 +7,20 @@
package org.jboss.test.messaging.jms.clustering;
import org.jboss.test.messaging.tools.ServerManagement;
+import org.jboss.test.messaging.tools.jmx.ServiceAttributeOverrides;
import org.jboss.test.messaging.MessagingTestCase;
import org.jboss.jms.client.JBossConnection;
+import org.jboss.jms.client.JBossConnectionFactory;
+import org.jboss.jms.client.state.ConnectionState;
+import org.jboss.jms.client.plugin.RandomLoadBalancingPolicy;
+import org.jboss.jms.client.plugin.RoundRobinLoadBalancingPolicy;
+import org.jboss.jms.client.delegate.ClientClusteredConnectionFactoryDelegate;
+import org.jboss.jms.client.delegate.DelegateSupport;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.naming.InitialContext;
+import javax.management.ObjectName;
/**
* This test DOESN'T extend ClusteringTestBase because I want to have control over first invocations
@@ -53,6 +61,13 @@
ConnectionFactory cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
+ JBossConnectionFactory jbcf = (JBossConnectionFactory)cf;
+ ClientClusteredConnectionFactoryDelegate clusteredDelegate =
+ (ClientClusteredConnectionFactoryDelegate )jbcf.getDelegate();
+
+ assertSame(RoundRobinLoadBalancingPolicy.class,
+ clusteredDelegate.getLoadBalancingPolicy().getClass());
+
Connection conn0 = cf.createConnection();
assertEquals(0, ((JBossConnection)conn0).getServerID());
@@ -94,6 +109,13 @@
ConnectionFactory cf = (ConnectionFactory)ic0.lookup("/ConnectionFactory");
+ JBossConnectionFactory jbcf = (JBossConnectionFactory)cf;
+ ClientClusteredConnectionFactoryDelegate clusteredDelegate =
+ (ClientClusteredConnectionFactoryDelegate )jbcf.getDelegate();
+
+ assertSame(RoundRobinLoadBalancingPolicy.class,
+ clusteredDelegate.getLoadBalancingPolicy().getClass());
+
Connection conn0 = cf.createConnection();
assertEquals(0, ((JBossConnection)conn0).getServerID());
@@ -129,10 +151,124 @@
}
}
+ public void testRandomRobinLoadBalancingSingleNode() throws Exception
+ {
+ // Make sure all servers are created and started; make sure that database is zapped ONLY for
+ // the first server, the others rely on values they expect to find in shared tables; don't
+ // clear the database for those.
+
+ ServiceAttributeOverrides override = new ServiceAttributeOverrides();
+ override.put(new ObjectName("jboss.messaging.connectionfactory:service=ConnectionFactory"),
+ "LoadBalancingFactory", "org.jboss.jms.client.plugin.RandomLoadBalancingFactory");
+ ServerManagement.start(0, "all", override, true);
+
+ try
+ {
+ InitialContext ic0 = new InitialContext(ServerManagement.getJNDIEnvironment(0));
+
+ ConnectionFactory cf = (ConnectionFactory)ic0.lookup("/ConnectionFactory");
+
+ JBossConnectionFactory jbcf = (JBossConnectionFactory)cf;
+ ClientClusteredConnectionFactoryDelegate clusteredDelegate =
+ (ClientClusteredConnectionFactoryDelegate )jbcf.getDelegate();
+
+ assertSame(RandomLoadBalancingPolicy.class,
+ clusteredDelegate.getLoadBalancingPolicy().getClass());
+
+ Connection conn0 = cf.createConnection();
+ assertEquals(0, getConnectionState(conn0).getServerID());
+
+ Connection conn1 = cf.createConnection();
+ assertEquals(0, getConnectionState(conn1).getServerID());
+
+ Connection conn2 = cf.createConnection();
+ assertEquals(0, getConnectionState(conn2).getServerID());
+
+ Connection conn3 = cf.createConnection();
+ assertEquals(0, getConnectionState(conn3).getServerID());
+
+ Connection conn4 = cf.createConnection();
+ assertEquals(0, getConnectionState(conn4).getServerID());
+
+ conn0.close();
+ conn1.close();
+ conn2.close();
+ conn3.close();
+ conn4.close();
+
+ ic0.close();
+ }
+ finally
+ {
+ ServerManagement.stop(1);
+ ServerManagement.stop(0);
+ }
+ }
+
+
+
+ public void testRandomRobinLoadBalancingTwoNodes() throws Exception
+ {
+ // Make sure all servers are created and started; make sure that database is zapped ONLY for
+ // the first server, the others rely on values they expect to find in shared tables; don't
+ // clear the database for those.
+
+ ServiceAttributeOverrides override = new ServiceAttributeOverrides();
+ override.put(new ObjectName("jboss.messaging.connectionfactory:service=ConnectionFactory"),
+ "LoadBalancingFactory", "org.jboss.jms.client.plugin.RandomLoadBalancingFactory");
+ ServerManagement.start(0, "all", override, true);
+ ServerManagement.start(1, "all", override, false);
+
+ try
+ {
+ InitialContext ic0 = new InitialContext(ServerManagement.getJNDIEnvironment(0));
+
+ ConnectionFactory cf = (ConnectionFactory)ic0.lookup("/ConnectionFactory");
+
+ JBossConnectionFactory jbcf = (JBossConnectionFactory)cf;
+ ClientClusteredConnectionFactoryDelegate clusteredDelegate =
+ (ClientClusteredConnectionFactoryDelegate )jbcf.getDelegate();
+
+ assertSame(RandomLoadBalancingPolicy.class,
+ clusteredDelegate.getLoadBalancingPolicy().getClass());
+
+ Connection conn0 = cf.createConnection();
+
+ Connection conn1 = cf.createConnection();
+
+ Connection conn2 = cf.createConnection();
+
+ Connection conn3 = cf.createConnection();
+
+ Connection conn4 = cf.createConnection();
+
+ conn0.close();
+ conn1.close();
+ conn2.close();
+ conn3.close();
+ conn4.close();
+
+ ic0.close();
+ }
+ finally
+ {
+ ServerManagement.stop(1);
+ ServerManagement.stop(0);
+ }
+ }
+
+
+
// Package protected ----------------------------------------------------------------------------
// Protected ------------------------------------------------------------------------------------
+ protected ConnectionState getConnectionState(Connection conn)
+ {
+ return (ConnectionState) (((DelegateSupport) ((JBossConnection) conn).
+ getDelegate()).getState());
+ }
+
protected void setUp() throws Exception
{
super.setUp();
More information about the jboss-cvs-commits
mailing list