1 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 ; 29 30 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 { 48 if (sessionPool != null) { 49 sessionPool.close(); 50 } 51 sessionPool = null; 52 } 53 54 public PooledSession borrowSession() throws JMSException { 55 try { 56 Object object = getSessionPool().borrowObject(); 57 return (PooledSession) object; 58 } 59 catch (JMSException e) { 60 throw e; 61 } 62 catch (Exception e) { 63 throw JMSExceptionSupport.create(e); 64 } 65 } 66 67 public void returnSession(PooledSession session) throws JMSException { 68 getConnection(); 70 try { 71 getSessionPool().returnObject(session); 72 } 73 catch (Exception e) { 74 throw JMSExceptionSupport.create("Failed to return session to pool: " + e, e); 75 } 76 } 77 78 public Object makeObject() throws Exception { 81 return new PooledSession(createSession(), this); 82 } 83 84 public void destroyObject(Object o) throws Exception { 85 PooledSession session = (PooledSession) o; 86 session.getSession().close(); 87 } 88 89 public boolean validateObject(Object o) { 90 return true; 91 } 92 93 public void activateObject(Object o) throws Exception { 94 } 95 96 public void passivateObject(Object o) throws Exception { 97 } 98 99 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 { 109 return connectionPool.getConnection(); 110 } 111 112 protected ActiveMQSession createSession() throws JMSException { 113 return (ActiveMQSession) getConnection().createSession(key.isTransacted(), key.getAckMode()); 114 } 115 116 } 117 | Popular Tags |