You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by da...@apache.org on 2012/08/30 10:37:44 UTC

svn commit: r1378853 - in /activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool: ConnectionPool.java PooledConnectionFactory.java

Author: davsclaus
Date: Thu Aug 30 08:37:44 2012
New Revision: 1378853

URL: http://svn.apache.org/viewvc?rev=1378853&view=rev
Log:
AMQ-4003: Add option to PooledConnectionFactory to control if connection should be created on startup or not

Modified:
    activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/ConnectionPool.java
    activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnectionFactory.java

Modified: activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/ConnectionPool.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/ConnectionPool.java?rev=1378853&r1=1378852&r2=1378853&view=diff
==============================================================================
--- activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/ConnectionPool.java (original)
+++ activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/ConnectionPool.java Thu Aug 30 08:37:44 2012
@@ -224,4 +224,9 @@ public class ConnectionPool {
     void onSessionInvalidated(PooledSession session) {
         this.loanedSessions.remove(session);
     }
+
+    @Override
+    public String toString() {
+        return "ConnectionPool[" + connection + "]";
+    }
 }

Modified: activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnectionFactory.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnectionFactory.java?rev=1378853&r1=1378852&r2=1378853&view=diff
==============================================================================
--- activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnectionFactory.java (original)
+++ activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/PooledConnectionFactory.java Thu Aug 30 08:37:44 2012
@@ -61,14 +61,15 @@ import org.apache.commons.pool.impl.Gene
 public class PooledConnectionFactory implements ConnectionFactory, Service {
     private static final transient Logger LOG = LoggerFactory.getLogger(PooledConnectionFactory.class);
     private ConnectionFactory connectionFactory;
-    private Map<ConnectionKey, LinkedList<ConnectionPool>> cache = new HashMap<ConnectionKey, LinkedList<ConnectionPool>>();
+    private final Map<ConnectionKey, LinkedList<ConnectionPool>> cache = new HashMap<ConnectionKey, LinkedList<ConnectionPool>>();
     private ObjectPoolFactory poolFactory;
     private int maximumActive = 500;
     private int maxConnections = 1;
     private int idleTimeout = 30 * 1000;
     private boolean blockIfSessionPoolIsFull = true;
-    private AtomicBoolean stopped = new AtomicBoolean(false);
+    private final AtomicBoolean stopped = new AtomicBoolean(false);
     private long expiryTimeout = 0l;
+    private boolean createConnectionOnStartup = true;
 
     public PooledConnectionFactory() {
         this(new ActiveMQConnectionFactory());
@@ -116,12 +117,19 @@ public class PooledConnectionFactory imp
         // Now.. we might get a connection, but it might be that we need to
         // dump it..
         if (connection != null && connection.expiredCheck()) {
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Connection has expired: {}", connection);
+            }
             connection = null;
         }
 
         if (connection == null) {
             ActiveMQConnection delegate = createConnection(key);
             connection = createConnectionPool(delegate);
+
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Created new connection: {}", connection);
+            }
         }
         pools.add(connection);
         return new PooledConnection(connection);
@@ -142,28 +150,28 @@ public class PooledConnectionFactory imp
         }
     }
 
-    /**
-     * @see org.apache.activemq.service.Service#start()
-     */
     public void start() {
-        try {
-            stopped.set(false);
-            createConnection();
-        } catch (JMSException e) {
-            LOG.warn("Create pooled connection during start failed.", e);
-            IOExceptionSupport.create(e);
+        LOG.debug("Staring the PooledConnectionFactory");
+        stopped.set(false);
+        if (isCreateConnectionOnStartup()) {
+            try {
+                // warm the pool by creating a connection during startup
+                createConnection();
+            } catch (JMSException e) {
+                LOG.warn("Create pooled connection during start failed. This exception will be ignored.", e);
+            }
         }
     }
 
     public void stop() {
-        LOG.debug("Stop the PooledConnectionFactory, number of connections in cache: "+cache.size());
+        LOG.debug("Stopping the PooledConnectionFactory, number of connections in cache: {}", cache.size());
         stopped.set(true);
         for (Iterator<LinkedList<ConnectionPool>> iter = cache.values().iterator(); iter.hasNext();) {
             for (ConnectionPool connection : iter.next()) {
                 try {
                     connection.close();
-                }catch(Exception e) {
-                    LOG.warn("Close connection failed",e);
+                } catch (Exception e) {
+                    LOG.warn("Close connection failed for connection: " + connection + ". This exception will be ignored.",e);
                 }
             }
         }
@@ -264,4 +272,20 @@ public class PooledConnectionFactory imp
     public long getExpiryTimeout() {
         return expiryTimeout;
     }
+
+    public boolean isCreateConnectionOnStartup() {
+        return createConnectionOnStartup;
+    }
+
+    /**
+     * Whether to create a connection on starting this {@link PooledConnectionFactory}.
+     * <p/>
+     * This can be used to warmup the pool on startup. Notice that any kind of exception
+     * happens during startup is logged at WARN level and ignored.
+     *
+     * @param createConnectionOnStartup <tt>true</tt> to create a connection on startup
+     */
+    public void setCreateConnectionOnStartup(boolean createConnectionOnStartup) {
+        this.createConnectionOnStartup = createConnectionOnStartup;
+    }
 }