1 16 17 package org.springframework.jms.listener.serversession; 18 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.jms.JMSException ; 25 import javax.jms.ServerSession ; 26 27 import org.apache.commons.pool.ObjectPool; 28 import org.apache.commons.pool.PoolableObjectFactory; 29 import org.apache.commons.pool.impl.GenericObjectPool; 30 31 import org.springframework.jms.listener.serversession.ListenerSessionManager; 32 33 56 public class CommonsPoolServerSessionFactory extends AbstractPoolingServerSessionFactory { 57 58 private int maxIdle = GenericObjectPool.DEFAULT_MAX_IDLE; 59 60 private int minIdle = GenericObjectPool.DEFAULT_MIN_IDLE; 61 62 private long maxWait = GenericObjectPool.DEFAULT_MAX_WAIT; 63 64 private long timeBetweenEvictionRunsMillis = GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; 65 66 private long minEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS; 67 68 private final Map serverSessionPools = Collections.synchronizedMap(new HashMap (1)); 69 70 71 77 public CommonsPoolServerSessionFactory() { 78 setMaxSize(GenericObjectPool.DEFAULT_MAX_ACTIVE); 79 } 80 81 86 public void setMaxIdle(int maxIdle) { 87 this.maxIdle = maxIdle; 88 } 89 90 93 public int getMaxIdle() { 94 return maxIdle; 95 } 96 97 102 public void setMinIdle(int minIdle) { 103 this.minIdle = minIdle; 104 } 105 106 109 public int getMinIdle() { 110 return minIdle; 111 } 112 113 118 public void setMaxWait(long maxWait) { 119 this.maxWait = maxWait; 120 } 121 122 125 public long getMaxWait() { 126 return maxWait; 127 } 128 129 135 public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) { 136 this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; 137 } 138 139 142 public long getTimeBetweenEvictionRunsMillis() { 143 return timeBetweenEvictionRunsMillis; 144 } 145 146 154 public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) { 155 this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; 156 } 157 158 161 public long getMinEvictableIdleTimeMillis() { 162 return minEvictableIdleTimeMillis; 163 } 164 165 166 171 public ServerSession getServerSession(ListenerSessionManager sessionManager) throws JMSException { 172 ObjectPool pool = null; 173 synchronized (this.serverSessionPools) { 174 pool = (ObjectPool) this.serverSessionPools.get(sessionManager); 175 if (pool == null) { 176 if (logger.isInfoEnabled()) { 177 logger.info("Creating Commons ServerSession pool for: " + sessionManager); 178 } 179 pool = createObjectPool(sessionManager); 180 this.serverSessionPools.put(sessionManager, pool); 181 } 182 } 183 try { 184 return (ServerSession ) pool.borrowObject(); 185 } 186 catch (Exception ex) { 187 JMSException jmsEx = new JMSException ("Failed to borrow ServerSession from pool"); 188 jmsEx.setLinkedException(ex); 189 throw jmsEx; 190 } 191 } 192 193 203 protected ObjectPool createObjectPool(ListenerSessionManager sessionManager) { 204 GenericObjectPool pool = new GenericObjectPool(createPoolableObjectFactory(sessionManager)); 205 pool.setMaxActive(getMaxSize()); 206 pool.setMaxIdle(getMaxIdle()); 207 pool.setMinIdle(getMinIdle()); 208 pool.setMaxWait(getMaxWait()); 209 pool.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis()); 210 pool.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis()); 211 return pool; 212 } 213 214 224 protected PoolableObjectFactory createPoolableObjectFactory(final ListenerSessionManager sessionManager) { 225 return new PoolableObjectFactory() { 226 public Object makeObject() throws JMSException { 227 return createServerSession(sessionManager); 228 } 229 public void destroyObject(Object obj) { 230 destroyServerSession((ServerSession ) obj); 231 } 232 public boolean validateObject(Object obj) { 233 return true; 234 } 235 public void activateObject(Object obj) { 236 } 237 public void passivateObject(Object obj) { 238 } 239 }; 240 } 241 242 246 protected void serverSessionFinished(ServerSession serverSession, ListenerSessionManager sessionManager) { 247 ObjectPool pool = (ObjectPool) this.serverSessionPools.get(sessionManager); 248 try { 249 pool.returnObject(serverSession); 250 } 251 catch (Exception ex) { 252 logger.error("Failed to return ServerSession to pool", ex); 253 } 254 } 255 256 259 public void close(ListenerSessionManager sessionManager) { 260 synchronized (this.serverSessionPools) { 261 for (Iterator it = this.serverSessionPools.values().iterator(); it.hasNext();) { 262 ObjectPool pool = (ObjectPool) it.next(); 263 try { 264 pool.close(); 265 } 266 catch (Exception ex) { 267 logger.error("Failed to close ServerSession pool", ex); 268 } 269 } 270 } 271 } 272 273 } 274 | Popular Tags |