Author: tmielke
Date: Fri Mar  2 09:03:23 2012
New Revision: 1296094

URL: http://svn.apache.org/viewvc?rev=1296094&view=rev
Log:
AMQ-3752: repeated call to PooledConnection.setClientID() with the same client 
id are ignored and don't raise an exception.

Added:
    
activemq/trunk/activemq-pool/src/test/java/org/apache/activemq/pool/PooledConnectionTest.java
Modified:
    
activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnection.java

Modified: 
activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnection.java
URL: 
http://svn.apache.org/viewvc/activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnection.java?rev=1296094&r1=1296093&r2=1296094&view=diff
==============================================================================
--- 
activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnection.java
 (original)
+++ 
activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnection.java
 Fri Mar  2 09:03:23 2012
@@ -118,7 +118,14 @@ public class PooledConnection implements
     }
 
     public void setClientID(String clientID) throws JMSException {
-        getConnection().setClientID(clientID);
+       
+       // ignore repeated calls to setClientID() with the same client id
+       // this could happen when a JMS component such as Spring that uses a 
+       // PooledConnectionFactory shuts down and reinitializes.
+       //
+        if (this.getConnection().getClientID() == null || 
!this.getClientID().equals(clientID)) {
+               getConnection().setClientID(clientID);
+        }
     }
 
     public ConnectionConsumer createConnectionConsumer(Queue queue, String 
selector,

Added: 
activemq/trunk/activemq-pool/src/test/java/org/apache/activemq/pool/PooledConnectionTest.java
URL: 
http://svn.apache.org/viewvc/activemq/trunk/activemq-pool/src/test/java/org/apache/activemq/pool/PooledConnectionTest.java?rev=1296094&view=auto
==============================================================================
--- 
activemq/trunk/activemq-pool/src/test/java/org/apache/activemq/pool/PooledConnectionTest.java
 (added)
+++ 
activemq/trunk/activemq-pool/src/test/java/org/apache/activemq/pool/PooledConnectionTest.java
 Fri Mar  2 09:03:23 2012
@@ -0,0 +1,99 @@
+/**
+ * 
+ */
+package org.apache.activemq.pool;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.IllegalStateException;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A couple of tests against the PooledConnection class.
+ *
+ */
+public class PooledConnectionTest extends TestCase {
+       
+       private Logger log = 
LoggerFactory.getLogger(PooledConnectionTest.class);
+       
+       
+       @Override
+       public void setUp() throws Exception {
+               log.debug("setUp() called.");
+       }
+       
+       
+       @Override
+       public void tearDown() throws Exception {
+               log.debug("tearDown() called.");
+       }
+               
+       
+       /**
+        * AMQ-3752:
+        * Tests how the ActiveMQConnection reacts to repeated calls to
+        * setClientID(). 
+        * 
+        * @throws Exception
+        */
+       public void testRepeatedSetClientIDCalls() throws Exception {
+               log.debug("running testRepeatedSetClientIDCalls()");
+               
+               // 1st test: call setClientID("newID") twice 
+               // this should be tolerated and not result in an exception
+               //
+               ConnectionFactory cf = createPooledConnectionFactory();
+               Connection conn = cf.createConnection();
+               conn.setClientID("newID");
+               
+               try {
+                       conn.setClientID("newID");
+                       conn.start();
+                       conn.close();
+                       cf = null;
+               } catch (IllegalStateException ise) {
+                       log.error("Repeated calls to 
ActiveMQConnection.setClientID(\"newID\") caused " + ise.getMessage());
+                       Assert.fail("Repeated calls to 
ActiveMQConnection.setClientID(\"newID\") caused " + ise.getMessage());
+               }
+               
+               // 2nd test: call setClientID() twice with different IDs
+               // this should result in an IllegalStateException
+               //
+               cf = createPooledConnectionFactory();
+               conn = cf.createConnection();
+               conn.setClientID("newID1");
+               try {
+                       conn.setClientID("newID2");
+                       Assert.fail("calling ActiveMQConnection.setClientID() 
twice with different clientID must raise an IllegalStateException"); 
+               } catch (IllegalStateException ise) {
+                       log.debug("Correctly received " + ise);
+               }
+               
+               // 3rd test: try to call setClientID() after start()
+               // should result in an exception
+               cf = createPooledConnectionFactory();
+               conn = cf.createConnection();
+               try {
+               conn.start();
+               conn.setClientID("newID3");
+               Assert.fail("Calling setClientID() after start() mut raise a 
JMSException.");
+               } catch (IllegalStateException ise) {
+                       log.debug("Correctly received " + ise);
+               }
+               
+               log.debug("Test finished.");
+       }
+       
+       
+       protected ConnectionFactory createPooledConnectionFactory() {
+               ConnectionFactory cf = new 
PooledConnectionFactory("vm://localhost?broker.persistent=false");
+               ((PooledConnectionFactory)cf).setMaxConnections(1);     
+               log.debug("ConnectionFactory initialized.");
+               return cf;
+       }
+}


Reply via email to