KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.activemq.ActiveMQConnection;
21 import org.apache.activemq.ActiveMQSession;
22 import org.apache.activemq.AlreadyClosedException;
23 import org.apache.activemq.util.JMSExceptionSupport;
24 import org.apache.commons.pool.ObjectPool;
25 import org.apache.commons.pool.PoolableObjectFactory;
26 import org.apache.commons.pool.impl.GenericObjectPool;
27
28 import javax.jms.JMSException JavaDoc;
29
30 /**
31  * Represents the session pool for a given JMS connection.
32  *
33  * @version $Revision: 1.1 $
34  */

35 public class SessionPool implements PoolableObjectFactory {
36     private ConnectionPool connectionPool;
37     private SessionKey key;
38     private ObjectPool sessionPool;
39
40     public SessionPool(ConnectionPool connectionPool, SessionKey key, ObjectPool sessionPool) {
41         this.connectionPool = connectionPool;
42         this.key = key;
43         this.sessionPool = sessionPool;
44         sessionPool.setFactory(this);
45     }
46
47     public void close() throws Exception JavaDoc {
48         if (sessionPool != null) {
49             sessionPool.close();
50         }
51         sessionPool = null;
52     }
53
54     public PooledSession borrowSession() throws JMSException JavaDoc {
55         try {
56             Object JavaDoc object = getSessionPool().borrowObject();
57             return (PooledSession) object;
58         }
59         catch (JMSException JavaDoc e) {
60             throw e;
61         }
62         catch (Exception JavaDoc e) {
63             throw JMSExceptionSupport.create(e);
64         }
65     }
66
67     public void returnSession(PooledSession session) throws JMSException JavaDoc {
68         // lets check if we are already closed
69
getConnection();
70         try {
71             getSessionPool().returnObject(session);
72         }
73         catch (Exception JavaDoc e) {
74             throw JMSExceptionSupport.create("Failed to return session to pool: " + e, e);
75         }
76     }
77
78     // PoolableObjectFactory methods
79
// -------------------------------------------------------------------------
80
public Object JavaDoc makeObject() throws Exception JavaDoc {
81         return new PooledSession(createSession(), this);
82     }
83
84     public void destroyObject(Object JavaDoc o) throws Exception JavaDoc {
85         PooledSession session = (PooledSession) o;
86         session.getSession().close();
87     }
88
89     public boolean validateObject(Object JavaDoc o) {
90         return true;
91     }
92
93     public void activateObject(Object JavaDoc o) throws Exception JavaDoc {
94     }
95
96     public void passivateObject(Object JavaDoc o) throws Exception JavaDoc {
97     }
98
99     // Implemention methods
100
// -------------------------------------------------------------------------
101
protected ObjectPool getSessionPool() throws AlreadyClosedException {
102         if (sessionPool == null) {
103             throw new AlreadyClosedException();
104         }
105         return sessionPool;
106     }
107
108     protected ActiveMQConnection getConnection() throws JMSException JavaDoc {
109         return connectionPool.getConnection();
110     }
111
112     protected ActiveMQSession createSession() throws JMSException JavaDoc {
113         return (ActiveMQSession) getConnection().createSession(key.isTransacted(), key.getAckMode());
114     }
115
116 }
117
Popular Tags