Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 40375 invoked from network); 23 Apr 2002 13:47:21 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 23 Apr 2002 13:47:21 -0000 Received: (qmail 13877 invoked by uid 97); 23 Apr 2002 13:47:21 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 13861 invoked by uid 97); 23 Apr 2002 13:47:20 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 13850 invoked by uid 97); 23 Apr 2002 13:47:20 -0000 Date: 23 Apr 2002 13:47:17 -0000 Message-ID: <20020423134717.37909.qmail@icarus.apache.org> From: rwaldhoff@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/pool/src/java/org/apache/commons/pool overview.html X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N rwaldhoff 02/04/23 06:47:17 Added: pool/src/java/org/apache/commons/pool overview.html Log: (oops, forgot the add) apply John McNally's javadoc overview patch, and some related changes (see bug 8082 - http://nagoya.apache.org/bugzilla/show_bug.cgi?id=8082) Revision Changes Path 1.1 jakarta-commons/pool/src/java/org/apache/commons/pool/overview.html Index: overview.html =================================================================== Overview of the org.apache.commons.pool component

Generic Object pooling API with several implementations.

The org.apache.commons.pool package defines a simple interface for a pool of object instances, and a handful of base classes that may be useful when creating pool implementations. The api supports pooling of unique objects which can be requested via a key as well as pools where all objects are equivalent.

The org.apache.commons.pool.impl package contains several pool implementations. {@link org.apache.commons.pool.impl.StackObjectPool StackObjectPool} is useful for supporting reuse of a limited number of instances while allowing new instances to be created as needed to support high demand. {@link org.apache.commons.pool.impl.GenericObjectPool GenericObjectPool} has many configuration options and can support a limited set of objects such as would be useful in a database connection pool. {@link org.apache.commons.pool.impl.SoftReferenceObjectPool SoftReferenceObjectPool} has no limit on the number of objects in the pool, but garbage collector can remove idle objects from the pool as needed. There are also keyed versions of the first two.

Here is a simple example of pooling HashMap's. First create an {@link org.apache.commons.pool.ObjectPoolFactory ObjectPoolFactory}

      public class HashMapFactory 
          extends {@link org.apache.commons.pool.BasePoolableObjectFactory BasePoolableObjectFactory}
      {
          /**
           * Creates an instance that can be returned by the pool.
           * @return an instance that can be returned by the pool.
           */
          public Object makeObject() 
              throws Exception
          {
              return new HashMap();
          }
          
          /**
           * Uninitialize an instance to be returned to the pool.
           * @param obj the instance to be passivated
           */
          public void passivateObject(Object obj) 
              throws Exception
          {
              Map map = (Map)obj;
              map.clear();
          }
      }
  

A class that makes frequent use of a Map could then use a pool as shown below:

      public class Foo
      {
          private {@link org.apache.commons.pool.ObjectPool ObjectPool} pool;
          public Foo()
          {
              {@link org.apache.commons.pool.PoolableObjectFactory PoolableObjectFactory} factory = new HashMapFactory();
              pool = new StackObjectPool(factory, 1000);
          }
  
          public doSomething()
          {
              ...
              Map map = null;
              try
              {
                  map = (Map)pool.borrowObject();
                  // use map
                  ...
              }
              finally
              {
                  if (map != null)
                  {
                      pool.returnObject(map);
                  }
              }
              ...
          }
      }
  

The above example shows how one would use an {@link org.apache.commons.pool.ObjectPool ObjectPool}. The other supplied implementations or another special purpose pool would be used similarly.

-- To unsubscribe, e-mail: For additional commands, e-mail: