activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r365917 - in /incubator/activemq/trunk/activemq-core/src: main/java/org/apache/activemq/broker/ main/java/org/apache/activemq/broker/region/ test/java/org/apache/activemq/
Date Wed, 04 Jan 2006 14:42:30 GMT
Author: jstrachan
Date: Wed Jan  4 06:42:24 2006
New Revision: 365917

URL: http://svn.apache.org/viewcvs?rev=365917&view=rev
Log:
added test case and fix for AMQ-463 to test for duplicate clientIDs connecting

Added:
    incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/ReconnectWithSameClientIDTest.java
  (with props)
Modified:
    incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/AbstractConnection.java
    incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/region/RegionBroker.java

Modified: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/AbstractConnection.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/AbstractConnection.java?rev=365917&r1=365916&r2=365917&view=diff
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/AbstractConnection.java
(original)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/AbstractConnection.java
Wed Jan  4 06:42:24 2006
@@ -20,7 +20,7 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
-import java.util.LinkedList;
+import java.util.*;
 import java.util.List;
 
 import org.apache.activemq.Service;
@@ -61,6 +61,8 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import javax.jms.InvalidClientIDException;
+
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
 
 
@@ -447,12 +449,12 @@
     
     public Response processAddConnection(ConnectionInfo info) throws Throwable {
         // Setup the context.
+        String clientId = info.getClientId();
         ConnectionContext context = new ConnectionContext();
         context.setConnection(this);
         context.setBroker(broker);
         context.setConnector(connector);
         context.setTransactions(new ConcurrentHashMap());
-        String clientId = info.getClientId();
         context.setClientId(clientId);
         context.setUserName(info.getUserName());
         context.setConnectionId(info.getConnectionId());

Modified: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/region/RegionBroker.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/region/RegionBroker.java?rev=365917&r1=365916&r2=365917&view=diff
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/region/RegionBroker.java
(original)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/region/RegionBroker.java
Wed Jan  4 06:42:24 2006
@@ -40,10 +40,13 @@
 import org.apache.activemq.util.IdGenerator;
 import org.apache.activemq.util.LongSequenceGenerator;
 
+import javax.jms.InvalidClientIDException;
 import javax.jms.JMSException;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.*;
+import java.util.Set;
 
 /**
  * Routes Broker operations to the correct messaging regions for processing.
@@ -67,6 +70,7 @@
     private final LongSequenceGenerator sequenceGenerator = new LongSequenceGenerator();
   
     private BrokerId brokerId;
     private String brokerName;
+    private Map clientIdSet = new HashMap(); // we will synchronize access
 
     public RegionBroker(TaskRunnerFactory taskRunnerFactory, UsageManager memoryManager,
PersistenceAdapter adapter) throws IOException {
         this(taskRunnerFactory, memoryManager, createDefaultPersistenceAdapter(memoryManager),
null);
@@ -110,10 +114,35 @@
     }
 
     public void addConnection(ConnectionContext context, ConnectionInfo info) throws Throwable
{
+        String clientId = info.getClientId();
+        if (clientId == null) {
+            throw new InvalidClientIDException("No clientID specified for connection request");
+        }
+        synchronized (clientIdSet ) {
+            if (clientIdSet.containsKey(clientId)) {
+                throw new InvalidClientIDException("Client: " + clientId + " already connected");
+            }
+            else {
+                clientIdSet.put(clientId, info);
+            }
+        }
+
         connections.add(context.getConnection());
     }
 
     public void removeConnection(ConnectionContext context, ConnectionInfo info, Throwable
error) throws Throwable {
+        String clientId = info.getClientId();
+        if (clientId == null) {
+            throw new InvalidClientIDException("No clientID specified for connection disconnect
request");
+        }
+        synchronized (clientIdSet) {
+            ConnectionInfo oldValue = (ConnectionInfo) clientIdSet.get(clientId);
+            // we may be removing the duplicate connection, not the first connection to be
created
+            if (oldValue == info) {
+                clientIdSet.remove(clientId);
+            }
+        }
+
         connections.remove(context.getConnection());
     }
 

Added: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/ReconnectWithSameClientIDTest.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/ReconnectWithSameClientIDTest.java?rev=365917&view=auto
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/ReconnectWithSameClientIDTest.java
(added)
+++ incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/ReconnectWithSameClientIDTest.java
Wed Jan  4 06:42:24 2006
@@ -0,0 +1,79 @@
+/**
+ * 
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ * 
+ * Licensed 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;
+
+import javax.jms.Connection;
+import javax.jms.InvalidClientIDException;
+import javax.jms.JMSException;
+import javax.jms.Session;
+
+/**
+ * 
+ * @version $Revision$
+ */
+public class ReconnectWithSameClientIDTest extends EmbeddedBrokerTestSupport {
+
+    protected Connection connection;
+    protected boolean transacted;
+    protected int authMode = Session.AUTO_ACKNOWLEDGE;
+
+    public void testReconnectMultipleTimesWithSameClientID() throws Exception {
+        connection = connectionFactory.createConnection();
+        useConnection(connection);
+
+        // now lets create another which should fail
+        for (int i = 1; i < 11; i++) {
+        Connection connection2 = connectionFactory.createConnection();
+        try {
+            useConnection(connection2);
+            fail("Should have thrown InvalidClientIDException on attempt" + i);
+        }
+        catch (InvalidClientIDException e) {
+            connection2.close();
+            System.out.println("Caught expected: " + e);
+        }
+        }
+
+        // now lets try closing the original connection and creating a new connection with
the same ID
+        connection.close();
+        connection = connectionFactory.createConnection();
+        useConnection(connection);
+    }
+
+    protected void setUp() throws Exception {
+        bindAddress = "tcp://localhost:61616";
+        super.setUp();
+    }
+
+    protected void tearDown() throws Exception {
+        if (connection != null) {
+            connection.close();
+            connection = null;
+        }
+        super.tearDown();
+    }
+
+    protected void useConnection(Connection connection) throws JMSException {
+        connection.setClientID("foo");
+        connection.start();
+        /**
+         * Session session = connection.createSession(transacted, authMode);
+         * return session;
+         */
+    }
+}

Propchange: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/ReconnectWithSameClientIDTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/ReconnectWithSameClientIDTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/ReconnectWithSameClientIDTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message