Fix for https://issues.apache.org/jira/browse/AMQ-4719
Project: http://git-wip-us.apache.org/repos/asf/activemq/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/747ea16a Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/747ea16a Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/747ea16a Branch: refs/heads/activemq-5.9 Commit: 747ea16ad322d60d247bf2f3e132abf028f58666 Parents: ad3ea19 Author: Rob Davies <[email protected]> Authored: Tue Nov 12 14:04:06 2013 +0000 Committer: Hadrian Zbarcea <[email protected]> Committed: Wed Mar 12 10:25:54 2014 -0400 ---------------------------------------------------------------------- .../activemq/broker/region/RegionBroker.java | 19 ++-- .../activemq/broker/LinkStealingTest.java | 91 ++++++++++++++++++++ 2 files changed, 100 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq/blob/747ea16a/activemq-broker/src/main/java/org/apache/activemq/broker/region/RegionBroker.java ---------------------------------------------------------------------- diff --git a/activemq-broker/src/main/java/org/apache/activemq/broker/region/RegionBroker.java b/activemq-broker/src/main/java/org/apache/activemq/broker/region/RegionBroker.java index 108670a..40f599b 100755 --- a/activemq-broker/src/main/java/org/apache/activemq/broker/region/RegionBroker.java +++ b/activemq-broker/src/main/java/org/apache/activemq/broker/region/RegionBroker.java @@ -33,14 +33,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; import javax.jms.InvalidClientIDException; import javax.jms.JMSException; -import org.apache.activemq.broker.Broker; -import org.apache.activemq.broker.BrokerService; -import org.apache.activemq.broker.Connection; -import org.apache.activemq.broker.ConnectionContext; -import org.apache.activemq.broker.ConsumerBrokerExchange; -import org.apache.activemq.broker.EmptyBroker; -import org.apache.activemq.broker.ProducerBrokerExchange; -import org.apache.activemq.broker.TransportConnector; +import org.apache.activemq.broker.*; import org.apache.activemq.broker.region.policy.DeadLetterStrategy; import org.apache.activemq.broker.region.policy.PolicyMap; import org.apache.activemq.command.ActiveMQDestination; @@ -234,8 +227,14 @@ public class RegionBroker extends EmptyBroker { if (context.isAllowLinkStealing()){ clientIdSet.remove(clientId); if (oldContext.getConnection() != null) { - LOG.warn("Stealing link for clientId {} From Connection {}", clientId, oldContext.getConnection()); - oldContext.getConnection().stop(); + Connection connection = oldContext.getConnection(); + LOG.warn("Stealing link for clientId {} From Connection {}", clientId, oldContext.getConnection()); + if (connection instanceof TransportConnection){ + TransportConnection transportConnection = (TransportConnection) connection; + transportConnection.stopAsync(); + }else{ + connection.stop(); + } }else{ LOG.error("Not Connection for {}", oldContext); } http://git-wip-us.apache.org/repos/asf/activemq/blob/747ea16a/activemq-broker/src/test/java/org/apache/activemq/broker/LinkStealingTest.java ---------------------------------------------------------------------- diff --git a/activemq-broker/src/test/java/org/apache/activemq/broker/LinkStealingTest.java b/activemq-broker/src/test/java/org/apache/activemq/broker/LinkStealingTest.java new file mode 100644 index 0000000..ec944fb --- /dev/null +++ b/activemq-broker/src/test/java/org/apache/activemq/broker/LinkStealingTest.java @@ -0,0 +1,91 @@ +/** + * 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.broker; + +import junit.framework.TestCase; +import org.apache.activemq.ActiveMQConnectionFactory; + +import javax.jms.Connection; +import javax.jms.InvalidClientIDException; +import java.util.concurrent.atomic.AtomicBoolean; + +public class LinkStealingTest extends TestCase { + protected BrokerService brokerService; + protected int timeOutInSeconds = 10; + + + @Override + protected void setUp() throws Exception { + brokerService = new BrokerService(); + brokerService.setPersistent(false); + } + + @Override + protected void tearDown() throws Exception { + if (brokerService != null) { + brokerService.stop(); + } + } + + + public void testStealLinkFails() throws Exception { + + brokerService.addConnector(ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL); + brokerService.start(); + + final String clientID = "ThisIsAClientId"; + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL); + Connection connection1 = factory.createConnection(); + connection1.setClientID(clientID); + connection1.start(); + + AtomicBoolean exceptionFlag = new AtomicBoolean(); + try { + Connection connection2 = factory.createConnection(); + connection2.setClientID(clientID); + connection2.start(); + } catch (InvalidClientIDException e) { + exceptionFlag.set(true); + } + assertTrue(exceptionFlag.get()); + + } + + public void testStealLinkSuccess() throws Exception { + + brokerService.addConnector(ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL+"?allowLinkStealing=true"); + brokerService.start(); + + final String clientID = "ThisIsAClientId"; + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL); + Connection connection1 = factory.createConnection(); + connection1.setClientID(clientID); + connection1.start(); + + AtomicBoolean exceptionFlag = new AtomicBoolean(); + try { + Connection connection2 = factory.createConnection(); + connection2.setClientID(clientID); + connection2.start(); + } catch (InvalidClientIDException e) { + e.printStackTrace(); + exceptionFlag.set(true); + } + assertFalse(exceptionFlag.get()); + + } +}
