User: vharcq  
  Date: 01/12/23 11:50:47

  Modified:    src/main/org/jboss/ejb/plugins AbstractInstancePool.java
                        EntityInstancePool.java
                        SingletonStatelessSessionInstancePool.java
  Added:       src/main/org/jboss/ejb/plugins TimedInstancePoolFeeder.java
  Log:
  Add a pool feeder to create asynchronously instances of beans and push them in the 
pool stack.
  Look at jboss.dtd for information on parameters
  
  Revision  Changes    Path
  1.21      +245 -183  jboss/src/main/org/jboss/ejb/plugins/AbstractInstancePool.java
  
  Index: AbstractInstancePool.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/AbstractInstancePool.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- AbstractInstancePool.java 2001/12/19 05:35:47     1.20
  +++ AbstractInstancePool.java 2001/12/23 19:50:46     1.21
  @@ -1,166 +1,200 @@
   /*
  -* JBoss, the OpenSource J2EE webOS
  -*
  -* Distributable under LGPL license.
  -* See terms of license at gnu.org.
  -*/
  + * JBoss, the OpenSource J2EE webOS
  + *
  + * Distributable under LGPL license.
  + * See terms of license at gnu.org.
  + */
   package org.jboss.ejb.plugins;
   
   import java.rmi.RemoteException;
   import java.rmi.ServerException;
  +import java.util.Stack;
  +import java.util.Iterator;
   import java.util.Map;
   import java.util.HashMap;
  -import java.util.Stack;
  +import java.lang.reflect.Constructor;
   
  -
   import org.jboss.ejb.Container;
   import org.jboss.ejb.InstancePool;
   import org.jboss.ejb.EnterpriseContext;
  +import org.jboss.ejb.InstancePoolFeeder;
   
  -import org.w3c.dom.Element;
   import org.jboss.deployment.DeploymentException;
   import org.jboss.metadata.MetaData;
   import org.jboss.metadata.XmlLoadable;
  -
  +import org.jboss.logging.Logger;
   import org.jboss.management.j2ee.CountStatistic;
   
  +import org.w3c.dom.Element;
   
   /**
  -*  <review>
  -*   Abstract Instance Pool class containing the basic logic to create
  -*  an EJB Instance Pool.
  -*  </review>
  -*
  -*   @see <related>
  -*   @author <a href="mailto:[EMAIL PROTECTED]";>Rickard �berg</a>
  -*   @author <a href="mailto:[EMAIL PROTECTED]";>Marc Fleury</a>
  -*  @author <a href="mailto:[EMAIL PROTECTED]";>Andreas Schaefer</a>
  - * @author <a href="mailto:[EMAIL PROTECTED]";>Sacha Labourey</a>
  -*   
  -*  @version $Revision: 1.20 $
  -*
  -*  <p><b>Revisions:</b>
  -*  <p><b>20010704 marcf:</b>
  -*  <ul>
  -*  <li>- Pools if used, do not reuse but restock the pile with fresh instances
  -*  </ul>
  -*  <p><b>20010709 andreas schaefer:</b>
  -*  <ul>
  -*  <li>- Added statistics gathering
  -*  </ul>
  -*  <p><b>20010920 Sacha Labourey:</b>
  -*  <ul>
  -*  <li>- Pooling made optional and only activated in concrete subclasses for SLSB 
and MDB
  -*  </ul>
  -*/
  + *  <review>
  + *   Abstract Instance Pool class containing the basic logic to create
  + *  an EJB Instance Pool.
  + *  </review>
  + *
  + *  @see <related>
  + *
  + *  @author <a href="mailto:[EMAIL PROTECTED]";>Rickard �berg</a>
  + *  @author <a href="mailto:[EMAIL PROTECTED]";>Marc Fleury</a>
  + *  @author <a href="mailto:[EMAIL PROTECTED]";>Andreas Schaefer</a>
  + *  @author <a href="mailto:[EMAIL PROTECTED]";>Sacha Labourey</a>
  + *
  + *  @version $Revision: 1.21 $
  + *
  + *  <p><b>Revisions:</b>
  + *  <p><b>20010704 marcf:</b>
  + *  <ul>
  + *  <li>- Pools if used, do not reuse but restock the pile with fresh instances
  + *  </ul>
  + *  <p><b>20010709 andreas schaefer:</b>
  + *  <ul>
  + *  <li>- Added statistics gathering
  + *  </ul>
  + *  <p><b>20010920 Sacha Labourey:</b>
  + *  <ul>
  + *  <li>- Pooling made optional and only activated in concrete subclasses for SLSB 
and MDB
  + *  </ul>
  + *  <p><b>20011208 Vincent Harcq:</b>
  + *  <ul>
  + *  <li>- A TimedInstancePoolFeeder thread is started at first use of the pool
  + *       and will populate the pool with new instances at a regular period.
  + *  </ul>
  + */
   public abstract class AbstractInstancePool
   implements InstancePool, XmlLoadable
   {
  -    // Constants -----------------------------------------------------
  -    
  -    // Attributes ----------------------------------------------------
  -    private Container container;
  -    
  -    Stack pool = new Stack();
  -    int maxSize = 30;
  -    
  +   // Constants -----------------------------------------------------
  +
  +   // Attributes ----------------------------------------------------
  +
  +   protected Logger log = Logger.getLogger(AbstractInstancePool.class);
  +
  +   protected Container container;
  +
  +   protected Stack pool = new Stack();
  +
  +   /** determine if we reuse EnterpriseContext objects i.e. if we actually do 
pooling */
  +   protected boolean reclaim = false;
  +
  +   protected InstancePoolFeeder poolFeeder;
  +   protected boolean useFeeder = false;
      /** Counter of all the Bean instantiated within the Pool **/
      protected CountStatistic mInstantiate = new CountStatistic( "Instantiation", "", 
"Beans instantiated in Pool" );
      /** Counter of all the Bean destroyed within the Pool **/
      protected CountStatistic mDestroy = new CountStatistic( "Destroy", "", "Beans 
destroyed in Pool" );
      /** Counter of all the ready Beans within the Pool (which are not used now) **/
      protected CountStatistic mReadyBean = new CountStatistic( "ReadyBean", "", 
"Numbers of ready Bean Pool" );
  -   
  -   // determine if we reuse EnterpriseContext objects i.e. if we actually do pooling
  -   protected boolean reclaim = false;
  -   
  -    // Static --------------------------------------------------------
  -    
  -    // Constructors --------------------------------------------------
  -    
  -    // Public --------------------------------------------------------
  -    
  -    /**
  +
  +
  +   // Static --------------------------------------------------------
  +
  +   // Constructors --------------------------------------------------
  +
  +   // Public --------------------------------------------------------
  +
  +   /**
       *   Set the callback to the container. This is for initialization.
       *   The IM may extract the configuration from the container.
       *
       * @param   c
       */
  -    public void setContainer(Container c)
  -    {
  -        this.container = c;
  -    }
  -    
  +   public void setContainer(Container c)
  +   {
  +      this.container = c;
  +   }
  +
      /**
  -   * <review>
  -   * @return Callback to the container which can be null if not set proviously
  -   * </review>
  -   **/
  -    public Container getContainer()
  -    {
  -        return container;
  -    }
  -    
  -    public void create()
  -    throws Exception
  -    {
  -    }
  -    
  -    public void start()
  -    throws Exception
  -    {
  -    }
  -    
  -    public void stop()
  -    {
  -    }
  -    
  -    public void destroy()
  -    {
  -    }
  -
  -    public boolean getReclaim()
  -    {
  -       return reclaim;
  -    }
  -
  -    public void setReclaim(boolean reclaim)
  -    {
  -       this.reclaim = reclaim;
  -    }
  +    * <review>
  +    * @return Callback to the container which can be null if not set proviously
  +    * </review>
  +    */
  +   public Container getContainer()
  +   {
  +      return container;
  +   }
       
  -    /**
  +   public void create()
  +   throws Exception
  +   {
  +   }
  +
  +   public void start()
  +   throws Exception
  +   {
  +   }
  +
  +   public void stop()
  +   {
  +     if (useFeeder && poolFeeder.isStarted()) poolFeeder.stop();
  +     freeAll();
  +   }
  +
  +   public void destroy()
  +   {
  +     if (useFeeder && poolFeeder.isStarted()) poolFeeder.stop();
  +     freeAll();
  +   }
  +
  +   /**
  +    * A pool is reclaim if it push back its dirty instances in its stack.
  +    */
  +   public boolean getReclaim()
  +   {
  +      return reclaim;
  +   }
  +
  +   public void setReclaim(boolean reclaim)
  +   {
  +      this.reclaim = reclaim;
  +   }
  +
  +   /**
  +    * Add a instance in the pool
  +    */
  +   public void add()
  +   throws Exception
  +   {
  +      this.pool.push(create(container.createBeanClassInstance()));
  +   }
  +
  +   /**
       *   Get an instance without identity.
       *   Can be used by finders,create-methods, and activation
       *
       * @return     Context /w instance
       * @exception   RemoteException
       */
  -    public synchronized EnterpriseContext get()
  -    throws Exception
  -    {
  -        //DEBUG      Logger.debug("Get instance "+this);
  -        
  -        if (!pool.empty())
  -        {
  +   public synchronized EnterpriseContext get()
  +   throws Exception
  +   {
  +      //Logger.debug("Get instance 
"+this+"#"+pool.empty()+"#"+getContainer().getBeanClass());
  +
  +      if (!pool.empty())
  +      {
            mReadyBean.remove();
  -            return (EnterpriseContext)pool.pop();
  -        } else
  -        {
  -            try
  -            {
  -                return create(container.createBeanClassInstance());
  -            } catch (InstantiationException e)
  -            {
  -                throw new ServerException("Could not instantiate bean", e);
  -            } catch (IllegalAccessException e)
  -            {
  -                throw new ServerException("Could not instantiate bean", e);
  -            }
  -        }
  -    }
  -    
  -    /**
  +         return (EnterpriseContext)pool.pop();
  +      }
  +      // The Pool feeder should avoid this
  +      else
  +      {
  +         if (useFeeder && poolFeeder.isStarted() && log.isDebugEnabled()) 
log.debug("The Pool for " + container.getBeanClass().getName()
  +               + " has been overloaded.  You should change pool parameters.");
  +         try
  +         {
  +            if (useFeeder && ! poolFeeder.isStarted()) poolFeeder.start();
  +            return create(container.createBeanClassInstance());
  +         } catch (InstantiationException e)
  +         {
  +            throw new ServerException("Could not instantiate bean", e);
  +         } catch (IllegalAccessException e)
  +         {
  +            throw new ServerException("Could not instantiate bean", e);
  +         }
  +      }
  +   }
  +
  +   /**
       *   Return an instance after invocation.
       *
       *   Called in 2 cases:
  @@ -169,60 +203,72 @@
       *
       * @param   ctx
       */
  -    public synchronized void free(EnterpriseContext ctx)
  -    {
  -        // Pool it
  -        //DEBUG      Logger.debug("Free 
instance:"+ctx.getId()+"#"+ctx.getTransaction());
  -        ctx.clear();
  -        
  -        if (pool.size() < maxSize)
  -        {
  -            
  -            // If (!reclaim), we do not reuse but create a brand new instance 
simplifies the design
  -            try {
  -                mReadyBean.add();
  -                if (this.reclaim)
  -                {
  -                   pool.push(ctx);                  
  -                }
  -                else
  -                {
  -                   discard (ctx);
  -                   pool.push(create(container.createBeanClassInstance()));          
        
  -                }
  -            } catch (Exception ignored) {}          
  -            //pool.push(ctx);
  -        } else
  -        {
  -            discard(ctx);
  -        }
  -    }
  -    
  -    public void discard(EnterpriseContext ctx)
  -    {
  -        // Throw away
  -        try
  -        {
  +   public synchronized void free(EnterpriseContext ctx)
  +   {
  +      // Pool it
  +      //DEBUG      Logger.debug("Free 
instance:"+ctx.getId()+"#"+ctx.getTransaction());
  +      ctx.clear();
  +
  +      // If (!reclaim), we do not reuse but create a brand new instance simplifies 
the design
  +      try {
  +         mReadyBean.add();
  +         if (this.reclaim)
  +         {
  +            pool.push(ctx);
  +         }
  +         else
  +         {
  +            discard (ctx);
  +         }
  +      } catch (Exception ignored) {}
  +   }
  +
  +   public void discard(EnterpriseContext ctx)
  +   {
  +      // Throw away, unsetContext()
  +      try
  +      {
            mDestroy.add();
  -            ctx.discard();
  -        } catch (RemoteException e)
  -        {
  -            // DEBUG Logger.exception(e);
  -        }
  -    }
  -    
  -    // Z implementation ----------------------------------------------
  -    
  -    // XmlLoadable implementation
  -    public void importXml(Element element) throws DeploymentException {
  -        String maximumSize = 
MetaData.getElementContent(MetaData.getUniqueChild(element, "MaximumSize"));
  -        try {
  -            maxSize = Integer.parseInt(maximumSize);
  -        } catch (NumberFormatException e) {
  -            throw new DeploymentException("Invalid MaximumSize value for instance 
pool configuration");
  -        }
  -    }
  -    
  +         ctx.discard();
  +      } catch (RemoteException e)
  +      {
  +         // DEBUG Logger.exception(e);
  +      }
  +   }
  +
  +   public int getCurrentSize()
  +   {
  +      return this.pool.size();
  +   }
  +
  +   // Z implementation ----------------------------------------------
  +
  +   /**
  +    * XmlLoadable implementation
  +    */
  +   public void importXml(Element element) throws DeploymentException {
  +      String feederPolicy = 
MetaData.getElementContent(MetaData.getOptionalChild(element, "feeder-policy"));
  +      if (feederPolicy != null)
  +      {
  +         useFeeder = true;
  +         try
  +         {
  +            Class cls = 
Thread.currentThread().getContextClassLoader().loadClass(feederPolicy);
  +            Constructor ctor = cls.getConstructor(new Class[] {});
  +            this.poolFeeder = (InstancePoolFeeder)ctor.newInstance(new Class[] {});
  +            this.poolFeeder.setInstancePool(this);
  +            this.poolFeeder.importXml(element);
  +         } catch (Exception x)
  +         {
  +            throw new DeploymentException("Can't create instance pool feeder", x);
  +         }
  +      }
  +      else
  +      {
  +         useFeeder = false;
  +      }
  +   }
  +
      public Map retrieveStatistic()
      {
         Map lStatistics = new HashMap();
  @@ -231,6 +277,7 @@
         lStatistics.put( "ReadyBeanCount", mReadyBean );
         return lStatistics;
      }
  +
      public void resetStatistic()
      {
         mInstantiate.reset();
  @@ -238,15 +285,30 @@
         mReadyBean.reset();
      }
   
  -    // Package protected ---------------------------------------------
  -    
  -    // Protected -----------------------------------------------------
  -    protected abstract EnterpriseContext create(Object instance)
  -    throws Exception;
  -    
  -    // Private -------------------------------------------------------
  -    
  -    // Inner classes -------------------------------------------------
  +   // Package protected ---------------------------------------------
  +
  +   // Protected -----------------------------------------------------
  +   protected abstract EnterpriseContext create(Object instance)
  +   throws Exception;
  +
  +   // Private -------------------------------------------------------
  +
  +   /**
  +    * At undeployment we want to free completely the pool.
  +    */
  +   private void freeAll()
  +   {
  +      Iterator i = this.pool.iterator();
  +      while (i.hasNext())
  +      {
  +         EnterpriseContext ec = (EnterpriseContext)i.next();
  +         // Clear TX so that still TX entity pools get killed as well
  +         ec.clear();
  +         discard(ec);
  +      }
  +   }
  +
  +   // Inner classes -------------------------------------------------
   
   }
   
  
  
  
  1.17      +18 -12    jboss/src/main/org/jboss/ejb/plugins/EntityInstancePool.java
  
  Index: EntityInstancePool.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/EntityInstancePool.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- EntityInstancePool.java   2001/11/24 20:43:22     1.16
  +++ EntityInstancePool.java   2001/12/23 19:50:47     1.17
  @@ -6,8 +6,6 @@
    */
   package org.jboss.ejb.plugins;
   
  -
  -
   import org.jboss.ejb.EnterpriseContext;
   import org.jboss.ejb.EntityEnterpriseContext;
   
  @@ -19,18 +17,23 @@
    *  @author <a href="mailto:[EMAIL PROTECTED]";>Marc Fleury</a>
    *  @author <a href="mailto:[EMAIL PROTECTED]";>Andreas Schaefer</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Sacha Labourey</a>
  - *   @version $Revision: 1.16 $
  + *   @version $Revision: 1.17 $
    *      
    * <p><b>Revisions:</b>
    * <p><b>20010718 andreas schaefer:</b>
    * <ul>
    * <li>- Added statistics gathering
    * </ul>
  -*  <p><b>20010920 Sacha Labourey:</b>
  -*  <ul>
  -*  <li>- Moved "reclaim" flag (set by Bill Burke) to the AbstractInstancePool level.
  -*        It can now be used for SLSB and MDB (pooling activated)
  -*  </ul>
  + *  <p><b>20010920 Sacha Labourey:</b>
  + *  <ul>
  + *  <li>- Moved "reclaim" flag (set by Bill Burke) to the AbstractInstancePool 
level.
  + *        It can now be used for SLSB and MDB (pooling activated)
  + *  </ul>
  + *  <p><b>20011208 Vincent Harcq:</b>
  + *  <ul>
  + *  <li>- A TimedInstancePoolFeeder thread is started at first use of the pool
  + *       and will populate the pool with new instances at a regular period.
  + *  </ul>
    */
   public class EntityInstancePool
      extends AbstractInstancePool
  @@ -57,10 +60,13 @@
      public synchronized void free(EnterpriseContext ctx)
      {
          // If transaction still present don't do anything (let the instance be GC)
  -       if (ctx.getTransaction() != null) return ;
  -        
  -       // To simplify design we don't reuse the ctx. 
  -       if (reclaim) super.free(ctx);
  +       if (ctx.getTransaction() != null)
  +       {
  +          // log.debug("Can Not FREE Entity Context because a Transaction exists.");
  +          return ;
  +       }
  +
  +       super.free(ctx);
      }
      
      // Z implementation ----------------------------------------------
  
  
  
  1.16      +21 -2     
jboss/src/main/org/jboss/ejb/plugins/SingletonStatelessSessionInstancePool.java
  
  Index: SingletonStatelessSessionInstancePool.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/SingletonStatelessSessionInstancePool.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- SingletonStatelessSessionInstancePool.java        2001/12/19 05:45:37     1.15
  +++ SingletonStatelessSessionInstancePool.java        2001/12/23 19:50:47     1.16
  @@ -29,13 +29,18 @@
    *
    *  @see <related>
    *  @author <a href="mailto:[EMAIL PROTECTED]";>Rickard �berg</a>
  - *  @version $Revision: 1.15 $
  + *  @version $Revision: 1.16 $
    *
    * <p><b>Revisions:</b>
    * <p><b>20010718 andreas schaefer:</b>
    * <ul>
    * <li>- Added Statistics Gathering
    * </ul>
  + *  <p><b>20011208 Vincent Harcq:</b>
  + *  <ul>
  + *  <li>- A TimedInstancePoolFeeder thread is started at first use of the pool
  + *       and will populate the pool with new instances at a regular period.
  + *  </ul>
    */
   public class SingletonStatelessSessionInstancePool
      implements InstancePool, XmlLoadable
  @@ -166,6 +171,20 @@
         this.notifyAll();
      }
   
  +   /**
  +    * Add a instance in the pool
  +    */
  +   public void add()
  +      throws Exception
  +   {
  +      // Empty
  +   }
  +
  +   public int getCurrentSize()
  +   {
  +      return 1;
  +   }
  +
      public Map retrieveStatistic()
      {
         Map lStatistics = new HashMap();
  @@ -187,7 +206,7 @@
       public void importXml(Element element) throws DeploymentException {
           isSynchronized = 
Boolean.valueOf(MetaData.getElementContent(MetaData.getUniqueChild(element, 
"Synchronized"))).booleanValue();
       }
  -    
  +
      // Package protected ---------------------------------------------
   
      // Protected -----------------------------------------------------
  
  
  
  1.1                  
jboss/src/main/org/jboss/ejb/plugins/TimedInstancePoolFeeder.java
  
  Index: TimedInstancePoolFeeder.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.ejb.plugins;
  
  import org.jboss.ejb.InstancePool;
  import org.jboss.ejb.InstancePoolFeeder;
  
  import org.jboss.logging.Logger;
  
  import org.jboss.metadata.XmlLoadable;
  import org.jboss.metadata.MetaData;
  
  import org.jboss.deployment.DeploymentException;
  
  import org.w3c.dom.Element;
  
  import java.util.TimerTask;
  import java.util.Timer;
  
  /**
   * Implementation of Pool feeder that is a java.util.Timer that checks at
   * regular period the size of the pool and bring it closer to its capacity by
   * adding a number of new instances.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Harcq</a>
   *
   * @version $Revision: 1.1 $
   */
  public class TimedInstancePoolFeeder
        extends TimerTask
        implements XmlLoadable, InstancePoolFeeder
  {
  
     // Private -------------------------------------------------------
  
     private InstancePool ip;
  
     private int rate;
     private int capacity;
     private int increment;
  
     private Timer timer;
     private boolean isStarted = false;
  
     // Constructor ---------------------------------------------------
  
     public TimedInstancePoolFeeder()
     {
     }
  
     // Public --------------------------------------------------------
  
     public void run()
     {
        try
        {
           if (ip.getCurrentSize() < capacity)
           {
              for (int i=0 ; i < increment ; i++)
              {
                 ip.add();
              }
           }
        }
        catch(Exception e)
        {
           // ignore here
        }
     }
  
     // InstancePoolFeeder Impl ---------------------------------------
  
     public void start()
     {
        //Logger.debug("Pool Feeder Start()");
        timer = new Timer();
        timer.schedule(this, 0, rate);
        isStarted = true;
     }
  
     public void stop()
     {
        //Logger.debug("Pool Feeder Stop()");
        if (timer != null)
        {
           timer.cancel();
        }
     }
  
     public void setInstancePool(InstancePool ip)
     {
        this.ip = ip;
     }
  
     public boolean isStarted()
     {
        return isStarted;
     }
  
     // XmlLoadable Impl ----------------------------------------------
  
     public void importXml(Element element)
     throws DeploymentException
     {
        try
        {
           Element instancePoolConf = MetaData.getUniqueChild(element, 
"feeder-policy-conf");
           Element tmp;
           tmp = MetaData.getUniqueChild(instancePoolConf,"capacity");
           capacity = Integer.parseInt(MetaData.getElementContent(tmp));
           tmp = MetaData.getUniqueChild(instancePoolConf,"increment");
           increment = Integer.parseInt(MetaData.getElementContent(tmp));
           tmp = MetaData.getUniqueChild(instancePoolConf,"period");
           rate = Integer.parseInt(MetaData.getElementContent(tmp));
        }
        catch (Exception e)
        {
           throw new DeploymentException("Can't read feeder-policy-conf",e);
        }
     }
  
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to