KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > pool > PooledConnectionFactory


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.pool;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.jms.Connection JavaDoc;
26 import javax.jms.ConnectionFactory JavaDoc;
27 import javax.jms.JMSException JavaDoc;
28 import javax.transaction.TransactionManager JavaDoc;
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 /**
38  * A JMS provider which pools Connection, Session and MessageProducer instances
39  * so it can be used with tools like Spring's <a
40  * HREF="http://activemq.org/Spring+Support">JmsTemplate</a>.
41  *
42  * <b>NOTE</b> this implementation is only intended for use when sending
43  * messages. It does not deal with pooling of consumers; for that look at a
44  * library like <a HREF="http://jencks.org/">Jencks</a> such as in <a
45  * HREF="http://jencks.org/Message+Driven+POJOs">this example</a>
46  *
47  * @version $Revision: 1.1 $
48  */

49 public class PooledConnectionFactory implements ConnectionFactory JavaDoc, Service {
50     private ConnectionFactory JavaDoc connectionFactory;
51     private Map JavaDoc cache = new HashMap JavaDoc();
52     private ObjectPoolFactory poolFactory;
53     private int maximumActive = 500;
54     private int maxConnections = 1;
55     private TransactionManager JavaDoc transactionManager;
56
57     public PooledConnectionFactory() {
58         this(new ActiveMQConnectionFactory());
59     }
60
61     public PooledConnectionFactory(String JavaDoc brokerURL) {
62         this(new ActiveMQConnectionFactory(brokerURL));
63     }
64
65     public PooledConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
66         this.connectionFactory = connectionFactory;
67     }
68
69     public ConnectionFactory JavaDoc getConnectionFactory() {
70         return connectionFactory;
71     }
72
73     public void setConnectionFactory(ConnectionFactory JavaDoc connectionFactory) {
74         this.connectionFactory = connectionFactory;
75     }
76
77     public TransactionManager JavaDoc getTransactionManager() {
78         return transactionManager;
79     }
80
81     public void setTransactionManager(TransactionManager JavaDoc transactionManager) {
82         this.transactionManager = transactionManager;
83     }
84
85     public Connection JavaDoc createConnection() throws JMSException JavaDoc {
86         return createConnection(null, null);
87     }
88
89     public synchronized Connection JavaDoc createConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc {
90         ConnectionKey key = new ConnectionKey(userName, password);
91         LinkedList JavaDoc pools = (LinkedList JavaDoc) cache.get(key);
92         
93         if (pools == null) {
94             pools = new LinkedList JavaDoc();
95             cache.put(key, pools);
96         }
97
98         ConnectionPool connection = null;
99         if (pools.size() == maxConnections) {
100             connection = (ConnectionPool) pools.removeFirst();
101         }
102         
103         // Now.. we might get a connection, but it might be that we need to
104
// dump it..
105
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 JavaDoc {
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     /**
131      * @see org.apache.activemq.service.Service#start()
132      */

133     public void start() {
134         try {
135             createConnection();
136         }
137         catch (JMSException JavaDoc e) {
138             IOExceptionSupport.create(e);
139         }
140     }
141
142     public void stop() throws Exception JavaDoc{
143         for(Iterator JavaDoc iter=cache.values().iterator();iter.hasNext();){
144             LinkedList JavaDoc list=(LinkedList JavaDoc)iter.next();
145             for(Iterator JavaDoc 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     /**
161      * Sets the object pool factory used to create individual session pools for
162      * each connection
163      */

164     public void setPoolFactory(ObjectPoolFactory poolFactory) {
165         this.poolFactory = poolFactory;
166     }
167
168     public int getMaximumActive() {
169         return maximumActive;
170     }
171
172     /**
173      * Sets the maximum number of active sessions per connection
174      */

175     public void setMaximumActive(int maximumActive) {
176         this.maximumActive = maximumActive;
177     }
178
179     /**
180      * @return the maxConnections
181      */

182     public int getMaxConnections() {
183         return maxConnections;
184     }
185
186     /**
187      * @param maxConnections the maxConnections to set
188      */

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