1 package com.protomatter.pool; 2 3 52 53 import java.util.*; 54 import com.protomatter.syslog.Syslog; 55 56 64 public abstract class GrowingObjectPool 65 extends SimpleObjectPool 66 { 67 private int maxPoolSize = -1; 68 private int initialPoolSize = 0; 69 private int createWaitTime = 0; 70 private int growBlock = 1; 71 private List pool = new ArrayList(); 72 73 77 public GrowingObjectPool() 78 { 79 super(); 80 } 81 82 87 public GrowingObjectPool(boolean monitorCheckedOutObjects) 88 { 89 super(monitorCheckedOutObjects); 90 } 91 92 95 protected List getPool() 96 { 97 return this.pool; 98 } 99 100 103 public int getObjectPoolSize() 104 { 105 return this.pool.size(); 106 } 107 108 112 public int getCurrentPoolSize() 113 { 114 int size = 0; 115 synchronized (sync) 116 { 117 size = getObjectPoolSize() + getObjectsInUse(); 118 } 119 return size; 120 } 121 122 131 protected ObjectPoolObject getNextPoolObject() 132 throws Exception 133 { 134 synchronized (sync) 135 { 136 if (pool.size() > 0) 138 { 139 return (ObjectPoolObject)pool.remove(0); 140 } 141 else if (getCurrentPoolSize() < maxPoolSize) 143 { 144 int currentPoolSize = getCurrentPoolSize(); 145 for (int i=1; i<growBlock && (maxPoolSize + growBlock) >= currentPoolSize; i++) 146 { 147 pool.add(createObjectPoolObject()); 148 doCreateWait(); 149 } 150 return createObjectPoolObject(); 151 } 152 else if (maxPoolSize == -1) 154 { 155 for (int i=1; i<growBlock; i++) 156 { 157 pool.add(createObjectPoolObject()); 158 doCreateWait(); 159 } 160 return createObjectPoolObject(); 161 } 162 } 163 return null; 165 } 166 167 178 public void setMaxObjectPoolSize(int size) 179 throws PoolException 180 { 181 synchronized (sync) 182 { 183 if (size < getObjectsInUse()) 184 { 185 throw new PoolException(PoolResources.getResourceString(MessageConstants.CANNOT_SHRINK_POOL_1)); 186 } 187 if (size < initialPoolSize) 188 { 189 throw new PoolException(PoolResources.getResourceString(MessageConstants.CANNOT_SHRINK_POOL_2)); 190 } 191 192 int evict = getCurrentPoolSize() - size; 194 if (evict > 0) { 196 for (int i=0; i<evict; i++) 197 { 198 ObjectPoolObject o = (ObjectPoolObject)pool.remove(0); 199 o.deleteObjectPoolObject(); } 201 } 202 maxPoolSize = size; 203 } 204 } 205 206 private void doCreateWait() 207 { 208 if (createWaitTime > 0) 209 { 210 try 211 { 212 Thread.sleep(createWaitTime); 213 } 214 catch (InterruptedException x) 215 { 216 ; 217 } 218 } 219 } 220 221 247 public void init(Map ht) 248 throws Exception 249 { 250 Integer ips = (Integer )ht.get("pool.initialSize"); 251 if (ips != null) 252 initialPoolSize = ips.intValue(); 253 254 Integer mps = (Integer )ht.get("pool.maxSize"); 255 if (mps != null) 256 maxPoolSize = mps.intValue(); 257 258 Integer pgb = (Integer )ht.get("pool.growBlock"); 259 if (pgb != null) 260 growBlock = pgb.intValue(); 261 262 Integer cwt = (Integer )ht.get("pool.createWaitTime"); 263 if (cwt != null) 264 createWaitTime = cwt.intValue(); 265 266 for (int i=0; i<initialPoolSize; i++) 268 { 269 pool.add(createObjectPoolObject()); 270 } 271 } 272 273 280 public void reInitializeObjectPool() 281 throws Exception 282 { 283 synchronized (sync) 284 { 285 Iterator it = pool.iterator(); 286 while (it.hasNext()) 287 { 288 ObjectPoolObject obj = (ObjectPoolObject)it.next(); 289 obj.deleteObjectPoolObject(); 290 } 291 pool.clear(); 292 293 for (int i=0; i<initialPoolSize; i++) 294 { 295 pool.add(createObjectPoolObject()); 296 doCreateWait(); 297 } 298 } 299 } 300 301 304 protected void checkinPoolObject(ObjectPoolObject o) 305 { 306 pool.add(o); 307 } 308 309 312 public int getInitialObjectPoolSize() 313 { 314 return this.initialPoolSize; 315 } 316 317 320 public int getMaxObjectPoolSize() 321 { 322 return this.maxPoolSize; 323 } 324 325 329 public int getObjectPoolGrowSize() 330 { 331 return this.growBlock; 332 } 333 334 338 public int getCreateWaitTime() 339 { 340 return this.createWaitTime; 341 } 342 } 343 | Popular Tags |