This commit is contained in:
Rob Davies 2013-11-12 14:04:06 +00:00
parent 4f1754bdc2
commit 497fbfc04b
2 changed files with 100 additions and 10 deletions

View File

@ -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);
}

View File

@ -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());
}
}