You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by gt...@apache.org on 2009/05/26 17:55:41 UTC
svn commit: r778777 - in /activemq/trunk/activemq-core/src:
main/java/org/apache/activemq/transport/failover/FailoverTransport.java
test/java/org/apache/activemq/transport/failover/FailoverRandomTest.java
Author: gtully
Date: Tue May 26 15:55:41 2009
New Revision: 778777
URL: http://svn.apache.org/viewvc?rev=778777&view=rev
Log:
have failover transport use shared random generator so random can loadbalance in the same jvm
Added:
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/transport/failover/FailoverRandomTest.java (with props)
Modified:
activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java
Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java?rev=778777&r1=778776&r2=778777&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java (original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java Tue May 26 15:55:41 2009
@@ -573,10 +573,8 @@
}
if (randomize) {
// Randomly, reorder the list by random swapping
- Random r = new Random();
- r.setSeed(System.currentTimeMillis());
for (int i = 0; i < l.size(); i++) {
- int p = r.nextInt(l.size());
+ int p = (int) (Math.random()*100 % l.size());
URI t = l.get(p);
l.set(p, l.get(i));
l.set(i, t);
@@ -585,6 +583,7 @@
if (removed) {
l.add(failedConnectTransportURI);
}
+ LOG.debug("urlList connectionList:" + l);
return l;
}
Added: activemq/trunk/activemq-core/src/test/java/org/apache/activemq/transport/failover/FailoverRandomTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/transport/failover/FailoverRandomTest.java?rev=778777&view=auto
==============================================================================
--- activemq/trunk/activemq-core/src/test/java/org/apache/activemq/transport/failover/FailoverRandomTest.java (added)
+++ activemq/trunk/activemq-core/src/test/java/org/apache/activemq/transport/failover/FailoverRandomTest.java Tue May 26 15:55:41 2009
@@ -0,0 +1,77 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.activemq.transport.failover;
+
+import junit.framework.TestCase;
+
+import org.apache.activemq.ActiveMQConnection;
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.activemq.broker.BrokerService;
+
+public class FailoverRandomTest extends TestCase {
+
+ BrokerService brokerA, brokerB;
+
+ public void setUp() throws Exception {
+ brokerA = createBroker("A");
+ brokerB = createBroker("B");
+ }
+
+ public void tearDown() throws Exception {
+ brokerA.stop();
+ brokerB.stop();
+ }
+
+ private BrokerService createBroker(String name) throws Exception {
+ BrokerService broker = new BrokerService();
+ broker.setBrokerName("Broker"+ name);
+ broker.addConnector("tcp://localhost:0");
+ broker.getManagementContext().setCreateConnector(false);
+ broker.setPersistent(false);
+ broker.setUseJmx(false);
+ broker.start();
+ return broker;
+ }
+
+ public void testRandomConnections() throws Exception {
+ String failoverUrl = "failover:("
+ + brokerA.getTransportConnectors().get(0).getConnectUri()
+ + ","
+ + brokerB.getTransportConnectors().get(0).getConnectUri()
+ + ")";
+ ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(failoverUrl);
+
+
+ ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection();
+ connection.start();
+ String brokerName1 = connection.getBrokerName();
+ assertNotNull(brokerName1);
+ connection.close();
+
+ String brokerName2 = brokerName1;
+ int attempts = 5;
+ while (brokerName1.equals(brokerName2) && attempts-- > 0) {
+ connection = (ActiveMQConnection) cf.createConnection();
+ connection.start();
+ brokerName2 = connection.getBrokerName();
+ assertNotNull(brokerName2);
+ connection.close();
+ }
+ assertTrue(brokerName1 + "!=" + brokerName2, !brokerName1.equals(brokerName2));
+ }
+}
Propchange: activemq/trunk/activemq-core/src/test/java/org/apache/activemq/transport/failover/FailoverRandomTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/trunk/activemq-core/src/test/java/org/apache/activemq/transport/failover/FailoverRandomTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date