FileDocCategorySizeDatePackage
StrictMaxPool.javaAPI DocJBoss 4.2.17841Fri Jul 13 20:53:58 BST 2007org.jboss.ejb3

StrictMaxPool

public class StrictMaxPool extends AbstractPool
author
Kabir Khan
version
$Revision: 61280 $

Fields Summary
public static final int
DEFAULT_MAX_SIZE
public static final long
DEFAULT_TIMEOUT
private EDU.oswego.cs.dl.util.concurrent.FIFOSemaphore
strictMaxSize
A FIFO semaphore that is set when the strict max size behavior is in effect. When set, only maxSize instances may be active and any attempt to get an instance will block until an instance is freed.
private int
inUse
private long
strictTimeout
The time in milliseconds to wait for the strictMaxSize semaphore.
protected LinkedList
pool
The pool data structure
protected int
maxSize
The maximum number of instances allowed in the pool
Logger
log
Constructors Summary
Methods Summary
public voiddestroy()

      freeAll();
   
public voiddiscard(BeanContext ctx)

      if (log.isTraceEnabled())
      {
         String msg = "Discard instance:" + this + "#" + ctx
                      + "#" + container.getBeanClass();
         log.trace(msg);
      }

      // If we block when maxSize instances are in use, invoke release on strictMaxSize
      strictMaxSize.release();
      --inUse;

      // Throw away, unsetContext()
      super.discard(ctx);
   
private voidfreeAll()
At undeployment we want to free completely the pool.

      LinkedList clone = (LinkedList) pool.clone();
      for (int i = 0; i < clone.size(); i++)
      {
         BeanContext bc = (BeanContext) clone.get(i);
         // Clear TX so that still TX entity pools get killed as well
         discard(bc);
      }
      pool.clear();
      inUse = 0;
      
   
public BeanContextget()
Get an instance without identity. Can be used by finders,create-methods, and activation

return
Context /w instance

      boolean trace = log.isTraceEnabled();
      if (trace)
         log.trace("Get instance " + this + "#" + pool.size() + "#" + container.getBeanClass());

      // Block until an instance is available
      try
      {
         boolean acquired = strictMaxSize.attempt(strictTimeout);
         if (trace)
            log.trace("Acquired(" + acquired + ") strictMaxSize semaphore, remaining=" + strictMaxSize.permits());
         if (acquired == false)
            throw new EJBException("Failed to acquire the pool semaphore, strictTimeout=" + strictTimeout);
      }
      catch (InterruptedException e)
      {
         throw new EJBException("Pool strictMaxSize semaphore was interrupted");
      }

      synchronized (pool)
      {
         if (!pool.isEmpty())
         {
            BeanContext bean = (BeanContext) pool.removeFirst();
            ++inUse;
            return bean;
         }
      }

      // Pool is empty, create an instance
      ++inUse;
      return create();
      
   
public BeanContextget(java.lang.Class[] initTypes, java.lang.Object[] initValues)

      boolean trace = log.isTraceEnabled();
      if (trace)
         log.trace("Get instance " + this + "#" + pool.size() + "#" + container.getBeanClass());
  
      // Block until an instance is available
      try
      {
         boolean acquired = strictMaxSize.attempt(strictTimeout);
         if (trace)
            log.trace("Acquired(" + acquired + ") strictMaxSize semaphore, remaining=" + strictMaxSize.permits());
         if (acquired == false)
            throw new EJBException("Failed to acquire the pool semaphore, strictTimeout=" + strictTimeout);
      }
      catch (InterruptedException e)
      {
         throw new EJBException("Pool strictMaxSize semaphore was interrupted");
      }

      synchronized (pool)
      {
         if (!pool.isEmpty())
         {
            BeanContext bean = (BeanContext) pool.removeFirst();
            ++inUse;
            return bean;
         }
      }

      // Pool is empty, create an instance
      ++inUse;
      return create(initTypes, initValues);
   
public intgetAvailableCount()

	   return maxSize - inUse;
   
public intgetCurrentSize()

	   return pool.size();
   
public intgetMaxSize()

	   return maxSize;
   
public voidinitialize(Container container, java.lang.Class contextClass, java.lang.Class beanClass, int maxSize, long timeout)
super.initialize() must have been called in advance



   // Static --------------------------------------------------------

   // Constructors --------------------------------------------------

   // Public --------------------------------------------------------

              
              
   
      super.initialize(container, contextClass, beanClass, maxSize, timeout);
      this.maxSize = maxSize;
      this.strictMaxSize = new FIFOSemaphore(maxSize);
      this.strictTimeout = timeout;
   
public voidrelease(BeanContext ctx)
Return an instance after invocation.

Called in 2 cases: a) Done with finder method b) Just removed

param
ctx

      if (log.isTraceEnabled())
      {
         String msg = pool.size() + "/" + maxSize + " Free instance:" + this
                      + "#" + container.getBeanClass();
         log.trace(msg);
      }

      try
      {
         // Add the unused context back into the pool
         boolean removeIt = false;
         synchronized (pool)
         {
            if (pool.size() < maxSize)
            {
               pool.addFirst(ctx);
            }
            else
            {
               removeIt = true;
            }
         }
         if (removeIt) remove(ctx);
         // If we block when maxSize instances are in use, invoke release on strictMaxSize
         strictMaxSize.release();
         --inUse;
      }
      catch (Exception ignored)
      {
      }
      
   
public voidsetMaxSize(int maxSize)

      this.maxSize = maxSize;
      this.strictMaxSize = new FIFOSemaphore(maxSize);