From 684c901016378fa9196481828834880bfaabce79 Mon Sep 17 00:00:00 2001 From: Gary Tully Date: Tue, 26 May 2009 15:55:49 +0000 Subject: [PATCH] have failover transport use shared random generator so random can loadbalance in the same jvm git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@778777 13f79535-47bb-0310-9956-ffa450edef68 --- .../transport/failover/FailoverTransport.java | 5 +- .../failover/FailoverRandomTest.java | 77 +++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 activemq-core/src/test/java/org/apache/activemq/transport/failover/FailoverRandomTest.java diff --git a/activemq-core/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java b/activemq-core/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java index dd6010b56a..c484f470c6 100755 --- a/activemq-core/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java +++ b/activemq-core/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java @@ -573,10 +573,8 @@ public class FailoverTransport implements CompositeTransport { } 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 @@ public class FailoverTransport implements CompositeTransport { if (removed) { l.add(failedConnectTransportURI); } + LOG.debug("urlList connectionList:" + l); return l; } diff --git a/activemq-core/src/test/java/org/apache/activemq/transport/failover/FailoverRandomTest.java b/activemq-core/src/test/java/org/apache/activemq/transport/failover/FailoverRandomTest.java new file mode 100644 index 0000000000..fbfbe3a720 --- /dev/null +++ b/activemq-core/src/test/java/org/apache/activemq/transport/failover/FailoverRandomTest.java @@ -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)); + } +}