1 18 package org.apache.activemq.pool; 19 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.LinkedList ; 23 import java.util.Map ; 24 25 import javax.jms.Connection ; 26 import javax.jms.ConnectionFactory ; 27 import javax.jms.JMSException ; 28 import javax.transaction.TransactionManager ; 29 30 import org.apache.activemq.ActiveMQConnection; 31 import org.apache.activemq.ActiveMQConnectionFactory; 32 import org.apache.activemq.Service; 33 import org.apache.activemq.util.IOExceptionSupport; 34 import org.apache.commons.pool.ObjectPoolFactory; 35 import org.apache.commons.pool.impl.GenericObjectPoolFactory; 36 37 49 public class PooledConnectionFactory implements ConnectionFactory , Service { 50 private ConnectionFactory connectionFactory; 51 private Map cache = new HashMap (); 52 private ObjectPoolFactory poolFactory; 53 private int maximumActive = 500; 54 private int maxConnections = 1; 55 private TransactionManager transactionManager; 56 57 public PooledConnectionFactory() { 58 this(new ActiveMQConnectionFactory()); 59 } 60 61 public PooledConnectionFactory(String brokerURL) { 62 this(new ActiveMQConnectionFactory(brokerURL)); 63 } 64 65 public PooledConnectionFactory(ActiveMQConnectionFactory connectionFactory) { 66 this.connectionFactory = connectionFactory; 67 } 68 69 public ConnectionFactory getConnectionFactory() { 70 return connectionFactory; 71 } 72 73 public void setConnectionFactory(ConnectionFactory connectionFactory) { 74 this.connectionFactory = connectionFactory; 75 } 76 77 public TransactionManager getTransactionManager() { 78 return transactionManager; 79 } 80 81 public void setTransactionManager(TransactionManager transactionManager) { 82 this.transactionManager = transactionManager; 83 } 84 85 public Connection createConnection() throws JMSException { 86 return createConnection(null, null); 87 } 88 89 public synchronized Connection createConnection(String userName, String password) throws JMSException { 90 ConnectionKey key = new ConnectionKey(userName, password); 91 LinkedList pools = (LinkedList ) cache.get(key); 92 93 if (pools == null) { 94 pools = new LinkedList (); 95 cache.put(key, pools); 96 } 97 98 ConnectionPool connection = null; 99 if (pools.size() == maxConnections) { 100 connection = (ConnectionPool) pools.removeFirst(); 101 } 102 103 if( connection!=null && connection.expiredCheck() ) { 106 connection=null; 107 } 108 109 if (connection == null) { 110 ActiveMQConnection delegate = createConnection(key); 111 connection = createConnectionPool(delegate); 112 } 113 pools.add(connection); 114 return new PooledConnection(connection); 115 } 116 117 protected ConnectionPool createConnectionPool(ActiveMQConnection connection) { 118 return new ConnectionPool(connection, getPoolFactory(), transactionManager); 119 } 120 121 protected ActiveMQConnection createConnection(ConnectionKey key) throws JMSException { 122 if (key.getUserName() == null && key.getPassword() == null) { 123 return (ActiveMQConnection) connectionFactory.createConnection(); 124 } 125 else { 126 return (ActiveMQConnection) connectionFactory.createConnection(key.getUserName(), key.getPassword()); 127 } 128 } 129 130 133 public void start() { 134 try { 135 createConnection(); 136 } 137 catch (JMSException e) { 138 IOExceptionSupport.create(e); 139 } 140 } 141 142 public void stop() throws Exception { 143 for(Iterator iter=cache.values().iterator();iter.hasNext();){ 144 LinkedList list=(LinkedList )iter.next(); 145 for(Iterator i=list.iterator();i.hasNext();){ 146 ConnectionPool connection=(ConnectionPool)i.next(); 147 connection.close(); 148 } 149 } 150 cache.clear(); 151 } 152 153 public ObjectPoolFactory getPoolFactory() { 154 if (poolFactory == null) { 155 poolFactory = createPoolFactory(); 156 } 157 return poolFactory; 158 } 159 160 164 public void setPoolFactory(ObjectPoolFactory poolFactory) { 165 this.poolFactory = poolFactory; 166 } 167 168 public int getMaximumActive() { 169 return maximumActive; 170 } 171 172 175 public void setMaximumActive(int maximumActive) { 176 this.maximumActive = maximumActive; 177 } 178 179 182 public int getMaxConnections() { 183 return maxConnections; 184 } 185 186 189 public void setMaxConnections(int maxConnections) { 190 this.maxConnections = maxConnections; 191 } 192 193 protected ObjectPoolFactory createPoolFactory() { 194 return new GenericObjectPoolFactory(null, maximumActive); 195 } 196 } 197 | Popular Tags |