1 22 package org.jboss.ejb3; 23 24 import java.util.LinkedList ; 25 import javax.ejb.EJBException ; 26 import org.jboss.logging.Logger; 27 import EDU.oswego.cs.dl.util.concurrent.FIFOSemaphore; 28 29 33 public class StrictMaxPool 34 extends AbstractPool 35 { 36 public static final int DEFAULT_MAX_SIZE = 30; 38 public static final long DEFAULT_TIMEOUT = Long.MAX_VALUE; 39 40 46 private FIFOSemaphore strictMaxSize; 47 50 private long strictTimeout; 51 52 55 protected LinkedList pool = new LinkedList (); 56 59 protected int maxSize = 30; 60 61 Logger log = Logger.getLogger(StrictMaxPool.class); 62 63 64 66 68 70 73 public void initialize(Container container, Class contextClass, Class beanClass, int maxSize, long timeout) 74 { 75 super.initialize(container, contextClass, beanClass, maxSize, timeout); 76 this.maxSize = maxSize; 77 this.strictMaxSize = new FIFOSemaphore(maxSize); 78 this.strictTimeout = timeout; 79 } 80 81 public void setMaxSize(int maxSize) 82 { 83 this.maxSize = maxSize; 84 this.strictMaxSize = new FIFOSemaphore(maxSize); 85 } 86 87 93 public BeanContext get() 94 { 95 boolean trace = log.isTraceEnabled(); 96 if (trace) 97 log.trace("Get instance " + this + "#" + pool.size() + "#" + container.getBeanClass()); 98 99 try 101 { 102 boolean acquired = strictMaxSize.attempt(strictTimeout); 103 if (trace) 104 log.trace("Acquired(" + acquired + ") strictMaxSize semaphore, remaining=" + strictMaxSize.permits()); 105 if (acquired == false) 106 throw new EJBException ("Failed to acquire the pool semaphore, strictTimeout=" + strictTimeout); 107 } 108 catch (InterruptedException e) 109 { 110 throw new EJBException ("Pool strictMaxSize semaphore was interrupted"); 111 } 112 113 synchronized (pool) 114 { 115 if (!pool.isEmpty()) 116 { 117 return (BeanContext) pool.removeFirst(); 118 } 119 } 120 121 return create(); 123 } 124 125 public BeanContext get(Class [] initTypes, Object [] initValues) 126 { 127 boolean trace = log.isTraceEnabled(); 128 if (trace) 129 log.trace("Get instance " + this + "#" + pool.size() + "#" + container.getBeanClass()); 130 131 try 133 { 134 boolean acquired = strictMaxSize.attempt(strictTimeout); 135 if (trace) 136 log.trace("Acquired(" + acquired + ") strictMaxSize semaphore, remaining=" + strictMaxSize.permits()); 137 if (acquired == false) 138 throw new EJBException ("Failed to acquire the pool semaphore, strictTimeout=" + strictTimeout); 139 } 140 catch (InterruptedException e) 141 { 142 throw new EJBException ("Pool strictMaxSize semaphore was interrupted"); 143 } 144 145 synchronized (pool) 146 { 147 if (!pool.isEmpty()) 148 { 149 return (BeanContext) pool.removeFirst(); 150 } 151 } 152 153 return create(initTypes, initValues); 155 } 156 157 166 public void release(BeanContext ctx) 167 { 168 if (log.isTraceEnabled()) 169 { 170 String msg = pool.size() + "/" + maxSize + " Free instance:" + this 171 + "#" + container.getBeanClass(); 172 log.trace(msg); 173 } 174 175 try 176 { 177 boolean removeIt = false; 179 synchronized (pool) 180 { 181 if (pool.size() < maxSize) 182 { 183 pool.addFirst(ctx); 184 } 185 else 186 { 187 removeIt = true; 188 } 189 } 190 if (removeIt) remove(ctx); 191 strictMaxSize.release(); 193 } 194 catch (Exception ignored) 195 { 196 } 197 } 198 199 public void discard(BeanContext ctx) 200 { 201 if (log.isTraceEnabled()) 202 { 203 String msg = "Discard instance:" + this + "#" + ctx 204 + "#" + container.getBeanClass(); 205 log.trace(msg); 206 } 207 208 strictMaxSize.release(); 210 211 super.discard(ctx); 213 } 214 215 217 protected void destroy() throws Exception 219 { 220 freeAll(); 221 this.container = null; 222 } 223 224 226 229 private void freeAll() 230 { 231 LinkedList clone = (LinkedList ) pool.clone(); 232 for (int i = 0; i < clone.size(); i++) 233 { 234 BeanContext bc = (BeanContext) clone.get(i); 235 discard(bc); 237 } 238 pool.clear(); 239 } 240 241 243 } 244 | Popular Tags |