1 16 17 package org.springframework.aop.target; 18 19 import org.apache.commons.pool.ObjectPool; 20 import org.apache.commons.pool.PoolableObjectFactory; 21 import org.apache.commons.pool.impl.GenericObjectPool; 22 23 import org.springframework.beans.BeansException; 24 import org.springframework.core.Constants; 25 26 58 public class CommonsPoolTargetSource extends AbstractPoolingTargetSource 59 implements PoolableObjectFactory { 60 61 private static final Constants constants = new Constants(GenericObjectPool.class); 62 63 64 private int maxIdle = GenericObjectPool.DEFAULT_MAX_IDLE; 65 66 private int minIdle = GenericObjectPool.DEFAULT_MIN_IDLE; 67 68 private long maxWait = GenericObjectPool.DEFAULT_MAX_WAIT; 69 70 private long timeBetweenEvictionRunsMillis = GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; 71 72 private long minEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS; 73 74 private byte whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION; 75 76 79 private ObjectPool pool; 80 81 82 88 public CommonsPoolTargetSource() { 89 setMaxSize(GenericObjectPool.DEFAULT_MAX_ACTIVE); 90 } 91 92 97 public void setMaxIdle(int maxIdle) { 98 this.maxIdle = maxIdle; 99 } 100 101 104 public int getMaxIdle() { 105 return this.maxIdle; 106 } 107 108 113 public void setMinIdle(int minIdle) { 114 this.minIdle = minIdle; 115 } 116 117 120 public int getMinIdle() { 121 return this.minIdle; 122 } 123 124 129 public void setMaxWait(long maxWait) { 130 this.maxWait = maxWait; 131 } 132 133 136 public long getMaxWait() { 137 return this.maxWait; 138 } 139 140 146 public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) { 147 this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; 148 } 149 150 153 public long getTimeBetweenEvictionRunsMillis() { 154 return this.timeBetweenEvictionRunsMillis; 155 } 156 157 165 public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) { 166 this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; 167 } 168 169 172 public long getMinEvictableIdleTimeMillis() { 173 return this.minEvictableIdleTimeMillis; 174 } 175 176 182 public void setWhenExhaustedActionName(String whenExhaustedActionName) { 183 setWhenExhaustedAction(constants.asNumber(whenExhaustedActionName).byteValue()); 184 } 185 186 194 public void setWhenExhaustedAction(byte whenExhaustedAction) { 195 this.whenExhaustedAction = whenExhaustedAction; 196 } 197 198 201 public byte getWhenExhaustedAction() { 202 return whenExhaustedAction; 203 } 204 205 206 210 protected final void createPool() { 211 logger.debug("Creating Commons object pool"); 212 this.pool = createObjectPool(); 213 } 214 215 223 protected ObjectPool createObjectPool() { 224 GenericObjectPool gop = new GenericObjectPool(this); 225 gop.setMaxActive(getMaxSize()); 226 gop.setMaxIdle(getMaxIdle()); 227 gop.setMinIdle(getMinIdle()); 228 gop.setMaxWait(getMaxWait()); 229 gop.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis()); 230 gop.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis()); 231 gop.setWhenExhaustedAction(getWhenExhaustedAction()); 232 return gop; 233 } 234 235 236 239 public Object getTarget() throws Exception { 240 return this.pool.borrowObject(); 241 } 242 243 246 public void releaseTarget(Object target) throws Exception { 247 this.pool.returnObject(target); 248 } 249 250 public int getActiveCount() throws UnsupportedOperationException { 251 return this.pool.getNumActive(); 252 } 253 254 public int getIdleCount() throws UnsupportedOperationException { 255 return this.pool.getNumIdle(); 256 } 257 258 259 262 public void destroy() throws Exception { 263 logger.debug("Closing Commons ObjectPool"); 264 this.pool.close(); 265 } 266 267 268 272 public Object makeObject() throws BeansException { 273 return newPrototypeInstance(); 274 } 275 276 public void destroyObject(Object obj) throws Exception { 277 destroyPrototypeInstance(obj); 278 } 279 280 public boolean validateObject(Object obj) { 281 return true; 282 } 283 284 public void activateObject(Object obj) { 285 } 286 287 public void passivateObject(Object obj) { 288 } 289 290 } 291 | Popular Tags |