mirror of https://github.com/apache/activemq.git
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@669510 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
73edcbc2e9
commit
6dc8e1d6fa
|
@ -20,6 +20,7 @@ import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import javax.jms.Connection;
|
import javax.jms.Connection;
|
||||||
import javax.jms.ConnectionFactory;
|
import javax.jms.ConnectionFactory;
|
||||||
|
@ -30,6 +31,8 @@ import org.apache.activemq.ActiveMQConnection;
|
||||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||||
import org.apache.activemq.Service;
|
import org.apache.activemq.Service;
|
||||||
import org.apache.activemq.util.IOExceptionSupport;
|
import org.apache.activemq.util.IOExceptionSupport;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.apache.commons.pool.ObjectPoolFactory;
|
import org.apache.commons.pool.ObjectPoolFactory;
|
||||||
import org.apache.commons.pool.impl.GenericObjectPoolFactory;
|
import org.apache.commons.pool.impl.GenericObjectPoolFactory;
|
||||||
|
|
||||||
|
@ -46,12 +49,14 @@ import org.apache.commons.pool.impl.GenericObjectPoolFactory;
|
||||||
* @version $Revision: 1.1 $
|
* @version $Revision: 1.1 $
|
||||||
*/
|
*/
|
||||||
public class PooledConnectionFactory implements ConnectionFactory, Service {
|
public class PooledConnectionFactory implements ConnectionFactory, Service {
|
||||||
|
private static final transient Log LOG = LogFactory.getLog(PooledConnectionFactory.class);
|
||||||
private ConnectionFactory connectionFactory;
|
private ConnectionFactory connectionFactory;
|
||||||
private Map<ConnectionKey, LinkedList<ConnectionPool>> cache = new HashMap<ConnectionKey, LinkedList<ConnectionPool>>();
|
private Map<ConnectionKey, LinkedList<ConnectionPool>> cache = new HashMap<ConnectionKey, LinkedList<ConnectionPool>>();
|
||||||
private ObjectPoolFactory poolFactory;
|
private ObjectPoolFactory poolFactory;
|
||||||
private int maximumActive = 500;
|
private int maximumActive = 500;
|
||||||
private int maxConnections = 1;
|
private int maxConnections = 1;
|
||||||
private int idleTimeout = 30 * 1000;
|
private int idleTimeout = 30 * 1000;
|
||||||
|
private AtomicBoolean stopped = new AtomicBoolean(false);
|
||||||
|
|
||||||
public PooledConnectionFactory() {
|
public PooledConnectionFactory() {
|
||||||
this(new ActiveMQConnectionFactory());
|
this(new ActiveMQConnectionFactory());
|
||||||
|
@ -78,6 +83,11 @@ public class PooledConnectionFactory implements ConnectionFactory, Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized Connection createConnection(String userName, String password) throws JMSException {
|
public synchronized Connection createConnection(String userName, String password) throws JMSException {
|
||||||
|
if (stopped.get()) {
|
||||||
|
LOG.debug("PooledConnectionFactory is stopped, skip create new connection.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
ConnectionKey key = new ConnectionKey(userName, password);
|
ConnectionKey key = new ConnectionKey(userName, password);
|
||||||
LinkedList<ConnectionPool> pools = cache.get(key);
|
LinkedList<ConnectionPool> pools = cache.get(key);
|
||||||
|
|
||||||
|
@ -124,18 +134,26 @@ public class PooledConnectionFactory implements ConnectionFactory, Service {
|
||||||
*/
|
*/
|
||||||
public void start() {
|
public void start() {
|
||||||
try {
|
try {
|
||||||
|
stopped.set(false);
|
||||||
createConnection();
|
createConnection();
|
||||||
} catch (JMSException e) {
|
} catch (JMSException e) {
|
||||||
|
LOG.warn("Create pooled connection during start failed.", e);
|
||||||
IOExceptionSupport.create(e);
|
IOExceptionSupport.create(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() throws Exception {
|
public void stop() {
|
||||||
|
LOG.debug("Stop the PooledConnectionFactory, number of connections in cache: "+cache.size());
|
||||||
|
stopped.set(true);
|
||||||
for (Iterator<LinkedList<ConnectionPool>> iter = cache.values().iterator(); iter.hasNext();) {
|
for (Iterator<LinkedList<ConnectionPool>> iter = cache.values().iterator(); iter.hasNext();) {
|
||||||
LinkedList list = iter.next();
|
LinkedList list = iter.next();
|
||||||
for (Iterator i = list.iterator(); i.hasNext();) {
|
for (Iterator i = list.iterator(); i.hasNext();) {
|
||||||
ConnectionPool connection = (ConnectionPool) i.next();
|
ConnectionPool connection = (ConnectionPool) i.next();
|
||||||
connection.close();
|
try {
|
||||||
|
connection.close();
|
||||||
|
}catch(Exception e) {
|
||||||
|
LOG.warn("Close connection failed",e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cache.clear();
|
cache.clear();
|
||||||
|
|
Loading…
Reference in New Issue