1 6 7 package org.jfox.pool; 8 9 import java.util.ArrayList ; 10 import java.util.Collections ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 17 18 public class SimpleObjectPool extends AbstractObjectPool { 19 private volatile int numWorking = 0; 21 private List pool = Collections.synchronizedList(new ArrayList ()); 22 23 26 private int maxRest = 10; 27 28 private int initNum = 0; 29 30 34 private long timeout = 5 * 60 * 1000; 36 public SimpleObjectPool(ObjectFactory factory) { 37 this(factory, 0, 10); 38 } 39 40 public SimpleObjectPool(ObjectFactory factory, int initNum, int maxRest) { 41 this(factory, initNum, maxRest, 5 * 60 * 1000); 42 } 43 44 public SimpleObjectPool(ObjectFactory factory, int initNum, int maxRest, long timeout) { 45 super(factory); 46 this.factory = factory; 47 this.initNum = initNum; 48 this.maxRest = maxRest; 49 this.timeout = timeout; 50 } 51 52 public SimpleObjectPool(Class factoryClass, Class poolableClass) { 53 this(factoryClass, poolableClass, 0, 10); 54 } 55 56 public SimpleObjectPool(Class factoryClass, Class poolableClass, int initNum, int maxRest) { 57 this(factoryClass, poolableClass, initNum, maxRest, 5 * 60 * 1000); 58 } 59 60 public SimpleObjectPool(Class factoryClass, Class poolableClass, int initNum, int maxRest, long timeout) { 61 super(factoryClass, poolableClass); 62 this.initNum = initNum; 63 this.maxRest = maxRest; 64 this.timeout = timeout; 65 } 66 67 72 public synchronized PoolableObject retrieveObject() throws Exception { 73 logger.debug("retrieve object, pool size is " + pool.size() + ", max rest is " + maxRest); 74 PoolableObject obj = null; 75 synchronized(pool) { 76 while(pool.size() > 0) { 77 obj = (PoolableObject) pool.remove(0); 78 if(!pool.isEmpty()) { 79 if(obj.getSleepTime() > timeout) { 81 factory.destroyObject(obj); 82 obj = null; 83 } 84 else { 85 break; 87 } 88 } 89 } 90 95 } 96 if(obj == null) { obj = factory.makeObject(); 98 } 99 if(obj != null) { 100 obj.activate(); 101 } 102 numWorking++; 103 return obj; 104 } 105 106 111 public boolean restoreObject(PoolableObject obj) { 112 logger.debug("restore object " + obj + ", pool size " + pool.size()); 113 numWorking--; 114 PoolableObject realObject = obj; 115 116 121 if(factory.validateObject(realObject)) { 122 try { 123 obj.passivate(); 124 synchronized(pool) { 125 if(pool.size() < maxRest) { 128 pool.add(realObject); 129 return true; 130 } 131 else { 132 factory.destroyObject(realObject); 133 return false; 134 } 135 } 136 } 137 catch(Exception e) { 138 try { 139 factory.destroyObject(realObject); 140 } 141 catch(Exception ex) { 142 logger.warn(e); 143 } 144 return false; 145 } 147 } 148 else { 149 try { 150 factory.destroyObject(realObject); 151 } 152 catch(Exception e) { 153 logger.warn(e.getMessage(), e); 154 } 155 return false; 156 } 157 } 158 159 public boolean removeObject(PoolableObject obj) { 160 logger.debug("remove object " + obj); 161 return pool.remove(obj); 162 } 163 164 public synchronized void clear() { 166 logger.debug("clear object pool"); 167 Iterator iter = pool.iterator(); 168 while(iter.hasNext()) { 169 try { 170 factory.destroyObject((PoolableObject) iter.next()); 171 } 172 catch(Exception e) { 173 logger.warn(e); 174 } 175 } 176 synchronized(pool) { 177 pool.clear(); 178 } 179 } 180 181 public int getWorking() { 182 return numWorking; 183 } 184 185 public int getRest() { 186 return pool.size(); 187 } 188 189 protected void doInit() throws Exception { 190 logger.debug("make " + initNum + " poolable object"); 191 int i = initNum; 192 while(i-- > 0) { 193 try { 194 pool.add(factory.makeObject()); 195 } 196 catch(Exception e) { 197 throw new RuntimeException (e); 198 } 199 } 200 } 201 202 protected void doDestroy() throws Exception { 204 this.clear(); 205 } 206 207 public int getInitNum() { 208 return initNum; 209 } 210 211 public int getMaxRest() { 212 return maxRest; 213 } 214 215 public static void main(String [] args) { 216 217 } 218 } | Popular Tags |