diff --git a/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnection.java b/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnection.java index d657706514..7268498c62 100755 --- a/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnection.java +++ b/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnection.java @@ -118,7 +118,14 @@ public class PooledConnection implements TopicConnection, QueueConnection, Enhan } 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, diff --git a/activemq-pool/src/test/java/org/apache/activemq/pool/PooledConnectionTest.java b/activemq-pool/src/test/java/org/apache/activemq/pool/PooledConnectionTest.java new file mode 100644 index 0000000000..d98ae055aa --- /dev/null +++ b/activemq-pool/src/test/java/org/apache/activemq/pool/PooledConnectionTest.java @@ -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; + } +}